Extract ebean-jackson-mapper module - initial step

This commit is contained in:
Rob Bygrave committed 2022-09-08 21:39:00 +12:00
1 parent c53ca6c16e
commit d5f753d110
9 files changed
+163 -79

No files matched your search

@@ -0,0 +1,24 @@
package io.ebean.core.type;
import io.ebean.annotation.MutationDetection;
/**
* Holds the configured mapper and default mutation detection mode to be used.
*/
public interface ScalarJsonManager {
/**
* Return the default mutation detection mode.
*/
MutationDetection mutationDetection();
/**
* Return the object mapper supplied in DatabaseConfig.
*/
Object mapper();
/**
* Return the Postgres type given the jdbc type.
*/
String postgresType(int dbType);
}
@@ -0,0 +1,4 @@
package io.ebean.core.type;
public interface ScalarJsonMapper {
}
@@ -0,0 +1,67 @@
package io.ebean.core.type;
import io.ebean.annotation.MutationDetection;
/**
* A request to create a ScalarType for a bean property (typically annotated with {@code @DbJson}).
*/
public final class ScalarJsonRequest {
private final ScalarJsonManager manager;
private final int dbType;
private final DocPropertyType docType;
private final Class<?> beanType;
private final MutationDetection mode;
private final String name;
public ScalarJsonRequest(ScalarJsonManager manager, int dbType, DocPropertyType docType, Class<?> beanType, MutationDetection mode, String name) {
this.manager = manager;
this.dbType = dbType;
this.docType = docType;
this.beanType = beanType;
this.mode = mode;
this.name = name;
}
/**
* Return the manager.
*/
public ScalarJsonManager manager() {
return manager;
}
/**
* Return the jdbc type this property maps to (e.g. JSON, JSONB etc).
*/
public int dbType() {
return dbType;
}
/**
* Return the document store docType.
*/
public DocPropertyType docType() {
return docType;
}
/**
* Return the type of the bean that this property belongs to.
*/
public Class<?> beanType() {
return beanType;
}
/**
* Return the mutation detection mode the property should use.
*/
public MutationDetection mode() {
return mode;
}
/**
* Return the property name.
*/
public String name() {
return name;
}
}
@@ -152,7 +152,6 @@ public class DeployBeanDescriptor<T> {
private DocStoreMode docStoreDelete; private DocStoreMode docStoreDelete;
private DeployBeanProperty idProperty; private DeployBeanProperty idProperty;
private TableJoin primaryKeyJoin; private TableJoin primaryKeyJoin;
private Object jacksonAnnotatedClass;
/** /**
* Construct the BeanDescriptor. * Construct the BeanDescriptor.
@@ -1130,14 +1129,4 @@ public class DeployBeanDescriptor<T> {
} }
} }
/**
* Returns the jackson annotated class, if jackson is present.
*/
@SuppressWarnings("unchecked")
Object /*AnnotatedClass*/ getJacksonAnnotatedClass() {
if (jacksonAnnotatedClass == null) {
jacksonAnnotatedClass = new DeployBeanObtainJackson(config, beanType).obtain();
}
return jacksonAnnotatedClass;
}
} }
@@ -1042,20 +1042,6 @@ public class DeployBeanProperty {
this.elementProperty = true; this.elementProperty = true;
} }
/**
* Returns the jackson annotated field, if jackson is present.
*/
public Object /*AnnotatedField*/ getJacksonField() {
com.fasterxml.jackson.databind.introspect.AnnotatedClass jac =
(com.fasterxml.jackson.databind.introspect.AnnotatedClass) getDesc().getJacksonAnnotatedClass();
for (com.fasterxml.jackson.databind.introspect.AnnotatedField candidate : jac.fields()) {
if (candidate.getName().equals(getName())) {
return candidate;
}
}
return null;
}
public void initMetaAnnotations(Set<Class<?>> metaAnnotationsFilter) { public void initMetaAnnotations(Set<Class<?>> metaAnnotationsFilter) {
metaAnnotations = AnnotationUtil.metaFindAllFor(field, metaAnnotationsFilter); metaAnnotations = AnnotationUtil.metaFindAllFor(field, metaAnnotationsFilter);
} }
@@ -65,7 +65,7 @@ public final class DefaultTypeManager implements TypeManager {
private final Object objectMapper; private final Object objectMapper;
private final boolean objectMapperPresent; private final boolean objectMapperPresent;
private final boolean postgres; private final boolean postgres;
private final TypeJsonManager jsonManager; private final ScalarJsonManager jsonManager;
private final boolean offlineMigrationGeneration; private final boolean offlineMigrationGeneration;
private final EnumType defaultEnumType; private final EnumType defaultEnumType;
private final DatabasePlatform databasePlatform; private final DatabasePlatform databasePlatform;
@@ -302,7 +302,7 @@ public final class DefaultTypeManager implements TypeManager {
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)) {
return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop)); return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), keepSource(prop));
} else { } else {
return createJsonObjectMapperType(prop, dbType, docType); return createJsonObjectMapperType(prop, dbType, docType);
} }
@@ -310,14 +310,14 @@ public final class DefaultTypeManager implements TypeManager {
if (type.equals(Set.class)) { if (type.equals(Set.class)) {
DocPropertyType docType = docType(genericType); DocPropertyType docType = docType(genericType);
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) { if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop)); return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), keepSource(prop));
} else { } else {
return createJsonObjectMapperType(prop, dbType, docType); return createJsonObjectMapperType(prop, dbType, docType);
} }
} }
if (type.equals(Map.class)) { if (type.equals(Map.class)) {
if (!hasJacksonAnnotations && isMapValueTypeObject(genericType)) { if (!hasJacksonAnnotations && isMapValueTypeObject(genericType)) {
return ScalarTypeJsonMap.typeFor(postgres, dbType, jsonManager.keepSource(prop)); return ScalarTypeJsonMap.typeFor(postgres, dbType, keepSource(prop));
} else { } else {
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT); return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
} }
@@ -331,6 +331,13 @@ public final class DefaultTypeManager implements TypeManager {
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT); return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
} }
private boolean keepSource(DeployBeanProperty prop) {
if (prop.getMutationDetection() == MutationDetection.DEFAULT) {
prop.setMutationDetection(jsonManager.mutationDetection());
}
return prop.getMutationDetection() == MutationDetection.SOURCE;
}
/** /**
* Returns TRUE, if there is any jackson annotation on that property. All jackson annotations * 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. * are annotated with the &#64;JacksonAnnotation meta annotation. So detection is easy.
@@ -370,11 +377,14 @@ public final class DefaultTypeManager implements TypeManager {
} }
private ScalarType<?> createJsonObjectMapperType(DeployBeanProperty prop, int dbType, DocPropertyType docType) { private ScalarType<?> createJsonObjectMapperType(DeployBeanProperty prop, int dbType, DocPropertyType docType) {
Class<?> type = prop.getPropertyType();
if (objectMapper == null) { if (objectMapper == null) {
throw new IllegalArgumentException("Type [" + type + "] unsupported for @DbJson mapping - Jackson ObjectMapper not present"); throw new IllegalArgumentException("Unsupported @DbJson mapping - Jackson ObjectMapper not present for " + prop);
} }
return ScalarTypeJsonObjectMapper.createTypeFor(jsonManager, prop, dbType, docType); if (MutationDetection.DEFAULT == prop.getMutationDetection()) {
prop.setMutationDetection(jsonManager.mutationDetection());
}
var req = new ScalarJsonRequest(jsonManager, dbType, docType, prop.getDesc().getBeanType(), prop.getMutationDetection(), prop.getName());
return ScalarTypeJsonObjectMapper.createTypeFor(req);
} }
/** /**
@@ -1,28 +1,27 @@
package io.ebeaninternal.server.deploy.meta; package io.ebeaninternal.server.type;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver; import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver;
import io.ebean.config.DatabaseConfig;
/** /**
* Used to obtain the Jackson AnnotatedClass for a given bean type utlimately to obtain field level Jackson annotations. * Used to obtain the Jackson AnnotatedClass for a given bean type utlimately to obtain field level Jackson annotations.
*/ */
final class DeployBeanObtainJackson<T> { final class DeployBeanObtainJackson<T> {
private final DatabaseConfig config; private final ObjectMapper objectMapper;
private final Class<T> beanType; private final Class<T> beanType;
DeployBeanObtainJackson(DatabaseConfig config, Class<T> beanType) { DeployBeanObtainJackson(ObjectMapper objectMapper, Class<T> beanType) {
this.config = config; this.objectMapper = objectMapper;
this.beanType = beanType; this.beanType = beanType;
} }
/** /**
* Return the Jackson AnnotatedClass for the given bean type. * Return the Jackson AnnotatedClass for the given bean type.
*/ */
Object obtain() { AnnotatedClass obtain() {
ObjectMapper objectMapper = (ObjectMapper) config.getObjectMapper();
JavaType javaType = objectMapper.getTypeFactory().constructType(beanType); JavaType javaType = objectMapper.getTypeFactory().constructType(beanType);
return AnnotatedClassResolver.resolve(objectMapper.getDeserializationConfig(), javaType, objectMapper.getDeserializationConfig()); return AnnotatedClassResolver.resolve(objectMapper.getDeserializationConfig(), javaType, objectMapper.getDeserializationConfig());
} }
@@ -6,11 +6,11 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.AnnotatedField; import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import io.ebean.annotation.MutationDetection; import io.ebean.annotation.MutationDetection;
import io.ebean.core.type.*; import io.ebean.core.type.*;
import io.ebean.text.TextException; import io.ebean.text.TextException;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
import java.io.DataInput; import java.io.DataInput;
@@ -18,30 +18,43 @@ import java.io.DataOutput;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types; import java.sql.Types;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* Supports @DbJson properties using Jackson ObjectMapper. * Supports @DbJson properties using Jackson ObjectMapper.
*/ */
final class ScalarTypeJsonObjectMapper { final class ScalarTypeJsonObjectMapper {
static final Map<Class<?>, AnnotatedClass> jacksonClasses = new ConcurrentHashMap<>();
/** /**
* Create and return the appropriate ScalarType. * Create and return the appropriate ScalarType.
*/ */
static ScalarType<?> createTypeFor(TypeJsonManager jsonManager, DeployBeanProperty prop, int dbType, DocPropertyType docType) { static ScalarType<?> createTypeFor(ScalarJsonRequest req) {
AnnotatedField field = (AnnotatedField) prop.getJacksonField(); AnnotatedClass annotatedClass = annotatedClass(req);
MutationDetection mode = prop.getMutationDetection(); AnnotatedField field = annotatedField(annotatedClass, req);
if (mode == MutationDetection.NONE) { if (req.mode() == MutationDetection.NONE) {
return new NoMutationDetection(jsonManager, field, dbType, docType); return new NoMutationDetection(req.manager(), field, req.dbType(), req.docType());
} else if (mode != MutationDetection.DEFAULT) { } else {
return new GenericObject(jsonManager, field, dbType, docType); return new GenericObject(req.manager(), field, req.dbType(), req.docType());
} }
// using the global default MutationDetection mode (defaults to HASH) }
final MutationDetection defaultMode = jsonManager.mutationDetection();
prop.setMutationDetection(defaultMode); private static AnnotatedField annotatedField(AnnotatedClass annotatedClass, ScalarJsonRequest req) {
if (MutationDetection.NONE == defaultMode) { for (AnnotatedField field : annotatedClass.fields()) {
return new NoMutationDetection(jsonManager, field, dbType, docType); if (field.getName().equals(req.name())) {
return field;
}
} }
return new GenericObject(jsonManager, field, dbType, docType); throw new IllegalStateException("AnnotatedField not found to match " + req.name());
}
private static AnnotatedClass annotatedClass(ScalarJsonRequest req) {
return jacksonClasses.computeIfAbsent(req.beanType(), key -> {
ObjectMapper mapper = (ObjectMapper) req.manager().mapper();
return new DeployBeanObtainJackson(mapper, req.beanType()).obtain();
});
} }
/** /**
@@ -49,7 +62,7 @@ final class ScalarTypeJsonObjectMapper {
*/ */
private static final class NoMutationDetection extends Base<Object> { private static final class NoMutationDetection extends Base<Object> {
NoMutationDetection(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { NoMutationDetection(ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
super(Object.class, jsonManager, field, dbType, docType); super(Object.class, jsonManager, field, dbType, docType);
} }
} }
@@ -61,7 +74,7 @@ final class ScalarTypeJsonObjectMapper {
private final boolean jsonb; private final boolean jsonb;
GenericObject(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { GenericObject(ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
super(Object.class, jsonManager, field, dbType, docType); super(Object.class, jsonManager, field, dbType, docType);
this.jsonb = "jsonb".equals(pgType); this.jsonb = "jsonb".equals(pgType);
} }
@@ -126,9 +139,9 @@ final class ScalarTypeJsonObjectMapper {
protected final String pgType; protected final String pgType;
private final DocPropertyType docType; private final DocPropertyType docType;
Base(Class<T> cls, TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) { Base(Class<T> cls, ScalarJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
super(cls, false, dbType); super(cls, false, dbType);
this.objectReader = jsonManager.objectMapper(); this.objectReader = (ObjectMapper)jsonManager.mapper();
this.pgType = jsonManager.postgresType(dbType); this.pgType = jsonManager.postgresType(dbType);
this.docType = docType; this.docType = docType;
final JacksonTypeHelper helper = new JacksonTypeHelper(field, objectReader); final JacksonTypeHelper helper = new JacksonTypeHelper(field, objectReader);
@@ -223,7 +236,8 @@ final class ScalarTypeJsonObjectMapper {
if (value == null) { if (value == null) {
dataOutput.writeBoolean(false); dataOutput.writeBoolean(false);
} else { } else {
ScalarHelp.writeUTF(dataOutput, format(value)); dataOutput.writeBoolean(true);
dataOutput.writeUTF(format(value));
} }
} }
} }
@@ -1,44 +1,35 @@
package io.ebeaninternal.server.type; package io.ebeaninternal.server.type;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ebean.ModifyAwareType; import io.ebean.ModifyAwareType;
import io.ebean.annotation.MutationDetection; import io.ebean.annotation.MutationDetection;
import io.ebean.config.dbplatform.DbPlatformType; import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.core.type.PostgresHelper; import io.ebean.core.type.PostgresHelper;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty; import io.ebean.core.type.ScalarJsonManager;
final class TypeJsonManager { final class TypeJsonManager implements ScalarJsonManager {
private final boolean postgres; private final boolean postgres;
private final ObjectMapper objectMapper; private final Object mapper;
private final MutationDetection mutationDetection; private final MutationDetection mutationDetection;
TypeJsonManager(boolean postgres, Object objectMapper, MutationDetection mutationDetection) { TypeJsonManager(boolean postgres, Object mapper, MutationDetection mutationDetection) {
this.postgres = postgres; this.postgres = postgres;
this.objectMapper = (ObjectMapper) objectMapper; this.mapper = mapper;
this.mutationDetection = mutationDetection; this.mutationDetection = mutationDetection;
} }
MutationDetection mutationDetection() { @Override
public MutationDetection mutationDetection() {
return mutationDetection; return mutationDetection;
} }
ObjectMapper objectMapper() { @Override
return objectMapper; public Object mapper() {
return mapper;
} }
boolean keepSource(DeployBeanProperty prop) { @Override
if (prop.getMutationDetection() == MutationDetection.SOURCE) { public String postgresType(int dbType) {
return true;
} else if (prop.getMutationDetection() == MutationDetection.DEFAULT) {
prop.setMutationDetection(mutationDetection);
return mutationDetection == MutationDetection.SOURCE;
} else {
return false;
}
}
String postgresType(int dbType) {
if (postgres) { if (postgres) {
switch (dbType) { switch (dbType) {
case DbPlatformType.JSON: case DbPlatformType.JSON: