Extract ebean-jackson-mapper module - Change checkJacksonAnnotations() to instead use ScalarJsonMapper.markerAnnotation()

This commit is contained in:
Rob Bygrave committed 2022-09-08 22:38:10 +12:00
1 parent 7a887d219f
commit 610b3f7dc8
3 files changed
+25 -11

No files matched your search

@@ -1,5 +1,9 @@
package io.ebean.core.type; package io.ebean.core.type;
import io.avaje.lang.Nullable;
import java.lang.annotation.Annotation;
/** /**
* Supports JSON bean properties that use something like Jackson ObjectMapper to convert between * Supports JSON bean properties that use something like Jackson ObjectMapper to convert between
* JSON content and the logical bean property value. * JSON content and the logical bean property value.
@@ -10,4 +14,10 @@ public interface ScalarJsonMapper {
* Return a ScalarType for the given JSON property. * Return a ScalarType for the given JSON property.
*/ */
ScalarType<?> createType(ScalarJsonRequest request); ScalarType<?> createType(ScalarJsonRequest request);
/**
* Return a marker annotation to detect when the JSON mapper should be used explicitly.
*/
@Nullable
<A extends Annotation> Class<A> markerAnnotation();
} }
@@ -297,12 +297,17 @@ public final class DefaultTypeManager implements TypeManager {
@Override @Override
public ScalarType<?> dbJsonType(DeployBeanProperty prop, int dbType, int dbLength) { public ScalarType<?> dbJsonType(DeployBeanProperty prop, int dbType, int dbLength) {
Class<?> type = prop.getPropertyType(); Class<?> type = prop.getPropertyType();
Type genericType = prop.getGenericType();
boolean hasJacksonAnnotations = objectMapperPresent && checkJacksonAnnotations(prop);
if (type.equals(String.class)) { if (type.equals(String.class)) {
return ScalarTypeJsonString.typeFor(postgres, dbType); return ScalarTypeJsonString.typeFor(postgres, dbType);
} }
boolean hasJacksonAnnotations = false;
if (jsonMapper != null) {
var markerAnnotation = jsonMapper.markerAnnotation();
if (markerAnnotation != null) {
hasJacksonAnnotations = !prop.getMetaAnnotations(markerAnnotation).isEmpty();
}
}
Type genericType = prop.getGenericType();
if (type.equals(List.class)) { if (type.equals(List.class)) {
DocPropertyType docType = docType(genericType); DocPropertyType docType = docType(genericType);
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) { if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
@@ -342,14 +347,6 @@ public final class DefaultTypeManager implements TypeManager {
return prop.getMutationDetection() == MutationDetection.SOURCE; return prop.getMutationDetection() == MutationDetection.SOURCE;
} }
/**
* Returns TRUE, if there is any jackson annotation on that property. All jackson annotations
* are annotated with the &#64;JacksonAnnotation meta annotation. So detection is easy.
*/
private boolean checkJacksonAnnotations(DeployBeanProperty prop) {
return prop.getMetaAnnotation(com.fasterxml.jackson.annotation.JacksonAnnotation.class) != null;
}
private DocPropertyType docType(Type genericType) { private DocPropertyType docType(Type genericType) {
if (genericType instanceof Class<?>) { if (genericType instanceof Class<?>) {
ScalarType<?> found = type((Class<?>) genericType); ScalarType<?> found = type((Class<?>) genericType);
@@ -16,6 +16,7 @@ import javax.persistence.PersistenceException;
import java.io.DataInput; import java.io.DataInput;
import java.io.DataOutput; import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.Map; import java.util.Map;
@@ -28,6 +29,12 @@ public final class ScalarJsonJacksonMapper implements ScalarJsonMapper {
private final Map<Class<?>, AnnotatedClass> jacksonClasses = new ConcurrentHashMap<>(); private final Map<Class<?>, AnnotatedClass> jacksonClasses = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked")
@Override
public <A extends Annotation> Class<A> markerAnnotation() {
return (Class<A>)com.fasterxml.jackson.annotation.JacksonAnnotation.class;
}
/** /**
* Create and return the appropriate ScalarType. * Create and return the appropriate ScalarType.
*/ */