Merge pull request #2828 from ebean-orm/feature/jackson-mapper-module

Extract jackson mapper module
This commit is contained in:
Rob Bygrave authored and GitHub committed 2022-10-14 12:39:43 +13:00
commit 19bbe6e860
20 files changed
+288 -116

No files matched your search

@@ -152,7 +152,6 @@ public class DeployBeanDescriptor<T> {
private DocStoreMode docStoreDelete;
private DeployBeanProperty idProperty;
private TableJoin primaryKeyJoin;
private Object jacksonAnnotatedClass;
/**
* 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;
}
}
@@ -1,29 +0,0 @@
package io.ebeaninternal.server.deploy.meta;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
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.
*/
final class DeployBeanObtainJackson<T> {
private final DatabaseConfig config;
private final Class<T> beanType;
DeployBeanObtainJackson(DatabaseConfig config, Class<T> beanType) {
this.config = config;
this.beanType = beanType;
}
/**
* Return the Jackson AnnotatedClass for the given bean type.
*/
Object obtain() {
ObjectMapper objectMapper = (ObjectMapper) config.getObjectMapper();
JavaType javaType = objectMapper.getTypeFactory().constructType(beanType);
return AnnotatedClassResolver.resolve(objectMapper.getDeserializationConfig(), javaType, objectMapper.getDeserializationConfig());
}
}
@@ -1042,20 +1042,6 @@ public class DeployBeanProperty {
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) {
metaAnnotations = AnnotationUtil.metaFindAllFor(field, metaAnnotationsFilter);
}
@@ -65,13 +65,14 @@ public final class DefaultTypeManager implements TypeManager {
private final Object objectMapper;
private final boolean objectMapperPresent;
private final boolean postgres;
private final TypeJsonManager jsonManager;
private final ScalarJsonManager jsonManager;
private final boolean offlineMigrationGeneration;
private final EnumType defaultEnumType;
private final DatabasePlatform databasePlatform;
private final PlatformArrayTypeFactory arrayTypeListFactory;
private final PlatformArrayTypeFactory arrayTypeSetFactory;
private final ScalarJsonMapper jsonMapper;
private GeoTypeBinder geoTypeBinder;
/**
@@ -94,6 +95,9 @@ public final class DefaultTypeManager implements TypeManager {
this.offlineMigrationGeneration = DbOffline.isGenerateMigration();
this.defaultEnumType = config.getDefaultEnumType();
ServiceLoader<ScalarJsonMapper> mappers = ServiceLoader.load(ScalarJsonMapper.class);
jsonMapper = mappers.findFirst().orElse(null);
initialiseStandard(config);
initialiseJavaTimeTypes(config);
loadTypesFromProviders(config, objectMapper);
@@ -293,34 +297,24 @@ public final class DefaultTypeManager implements TypeManager {
@Override
public ScalarType<?> dbJsonType(DeployBeanProperty prop, int dbType, int dbLength) {
Class<?> type = prop.getPropertyType();
Type genericType = prop.getGenericType();
boolean hasJacksonAnnotations = objectMapperPresent && checkJacksonAnnotations(prop);
if (type.equals(String.class)) {
return ScalarTypeJsonString.typeFor(postgres, dbType);
}
if (type.equals(List.class)) {
DocPropertyType docType = docType(genericType);
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop));
} else {
return createJsonObjectMapperType(prop, dbType, docType);
if (jsonMapper != null) {
var markerAnnotation = jsonMapper.markerAnnotation();
if (markerAnnotation != null && !prop.getMetaAnnotations(markerAnnotation).isEmpty()) {
return createJsonObjectMapperType(prop, dbType, docPropertyType(prop, type));
}
}
if (type.equals(Set.class)) {
DocPropertyType docType = docType(genericType);
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop));
} else {
return createJsonObjectMapperType(prop, dbType, docType);
}
Type genericType = prop.getGenericType();
if (type.equals(List.class) && isValueTypeSimple(genericType)) {
return ScalarTypeJsonList.typeFor(postgres, dbType, docType(genericType), prop.isNullable(), keepSource(prop));
}
if (type.equals(Map.class)) {
if (!hasJacksonAnnotations && isMapValueTypeObject(genericType)) {
return ScalarTypeJsonMap.typeFor(postgres, dbType, jsonManager.keepSource(prop));
} else {
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
}
if (type.equals(Set.class) && isValueTypeSimple(genericType)) {
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType(genericType), prop.isNullable(), keepSource(prop));
}
if (type.equals(Map.class) && isMapValueTypeObject(genericType)) {
return ScalarTypeJsonMap.typeFor(postgres, dbType, keepSource(prop));
}
if (objectMapperPresent && prop.getMutationDetection() == MutationDetection.DEFAULT) {
ScalarTypeSet<?> typeSet = typeSets.get(type);
@@ -331,12 +325,15 @@ public final class DefaultTypeManager implements TypeManager {
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
}
/**
* 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 boolean keepSource(DeployBeanProperty prop) {
if (prop.getMutationDetection() == MutationDetection.DEFAULT) {
prop.setMutationDetection(jsonManager.mutationDetection());
}
return prop.getMutationDetection() == MutationDetection.SOURCE;
}
private DocPropertyType docPropertyType(DeployBeanProperty prop, Class<?> type) {
return type.equals(List.class) || type.equals(Set.class) ? docType(prop.getGenericType()) : DocPropertyType.OBJECT;
}
private DocPropertyType docType(Type genericType) {
@@ -370,11 +367,14 @@ public final class DefaultTypeManager implements TypeManager {
}
private ScalarType<?> createJsonObjectMapperType(DeployBeanProperty prop, int dbType, DocPropertyType docType) {
Class<?> type = prop.getPropertyType();
if (objectMapper == null) {
throw new IllegalArgumentException("Type [" + type + "] unsupported for @DbJson mapping - Jackson ObjectMapper not present");
if (jsonMapper == null) {
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 jsonMapper.createType(req);
}
/**
@@ -1,46 +0,0 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
final class JacksonTypeHelper {
private final AnnotatedField field;
private final ObjectMapper objectMapper;
private final JavaType javaType;
private final DeserializationConfig deserConfig;
private final AnnotationIntrospector ai;
JacksonTypeHelper(AnnotatedField field, ObjectMapper objectMapper) {
this.field = field;
this.objectMapper = objectMapper;
this.javaType = field.getType();
this.deserConfig = objectMapper.getDeserializationConfig();
this.ai = deserConfig.getAnnotationIntrospector();
}
JavaType type() {
if (ai == null || javaType == null || javaType.hasRawClass(Object.class)) {
return javaType;
} else {
try {
return ai.refineDeserializationType(deserConfig, field, javaType);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
}
}
}
ObjectWriter objectWriter() {
if (ai == null || javaType == null || javaType.hasRawClass(Object.class)) {
return objectMapper.writerFor(javaType);
} else {
try {
JavaType serType = ai.refineSerializationType(objectMapper.getSerializationConfig(), field, javaType);
return objectMapper.writerFor(serType);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
}
}
}
}
@@ -1,38 +0,0 @@
package io.ebeaninternal.server.type;
/**
* Helper that removes whitespace from JSON. Used to normalise Postgres JSONB content.
*/
final class JsonTrim {
/**
* Return JSON with whitespace trimmed.
*/
static String trim(String json) {
if (json == null) {
return null;
}
int len = json.length();
StringBuilder builder = new StringBuilder(len);
boolean escaped = false;
boolean quoted = false;
for (int i = 0; i < len; i++) {
char c = json.charAt(i);
if (c == '\"') {
if (!escaped) {
quoted = !quoted;
} else {
escaped = false;
}
} else if (escaped) {
escaped = false;
} else if (quoted && c == '\\') {
escaped = true;
}
if (quoted || !Character.isWhitespace(c)) {
builder.append(c);
}
}
return builder.toString();
}
}
@@ -1,230 +0,0 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import io.ebean.annotation.MutationDetection;
import io.ebean.core.type.*;
import io.ebean.text.TextException;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
import javax.persistence.PersistenceException;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;
/**
* Supports @DbJson properties using Jackson ObjectMapper.
*/
final class ScalarTypeJsonObjectMapper {
/**
* Create and return the appropriate ScalarType.
*/
static ScalarType<?> createTypeFor(TypeJsonManager jsonManager, DeployBeanProperty prop, int dbType, DocPropertyType docType) {
AnnotatedField field = (AnnotatedField) prop.getJacksonField();
MutationDetection mode = prop.getMutationDetection();
if (mode == MutationDetection.NONE) {
return new NoMutationDetection(jsonManager, field, dbType, docType);
} else if (mode != MutationDetection.DEFAULT) {
return new GenericObject(jsonManager, field, dbType, docType);
}
// using the global default MutationDetection mode (defaults to HASH)
final MutationDetection defaultMode = jsonManager.mutationDetection();
prop.setMutationDetection(defaultMode);
if (MutationDetection.NONE == defaultMode) {
return new NoMutationDetection(jsonManager, field, dbType, docType);
}
return new GenericObject(jsonManager, field, dbType, docType);
}
/**
* No mutation detection on this json property.
*/
private static final class NoMutationDetection extends Base<Object> {
NoMutationDetection(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
super(Object.class, jsonManager, field, dbType, docType);
}
}
/**
* Supports HASH and SOURCE dirty detection modes.
*/
private static final class GenericObject extends Base<Object> {
private final boolean jsonb;
GenericObject(TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
super(Object.class, jsonManager, field, dbType, docType);
this.jsonb = "jsonb".equals(pgType);
}
@Override
public boolean mutable() {
return true;
}
@Override
public boolean jsonMapper() {
return true;
}
@Override
public Object read(DataReader reader) throws SQLException {
String json = reader.getString();
if (jsonb) {
json = JsonTrim.trim(json);
}
// pushJson such that we MD5 and store on EntityBeanIntercept later
reader.pushJson(json);
if (json == null || json.isEmpty()) {
return null;
}
try {
return objectReader.readValue(json, deserType);
} catch (IOException e) {
throw new TextException("Failed to parse JSON [{}] as " + deserType, json, e);
}
}
@Override
public void bind(DataBinder binder, Object value) throws SQLException {
// popJson as dirty detection already converted to json string
String rawJson = binder.popJson();
if (rawJson == null && value != null) {
rawJson = formatValue(value); // not expected, need to check?
}
if (pgType != null) {
binder.setObject(PostgresHelper.asObject(pgType, rawJson));
} else {
if (value == null) {
// use varchar, otherwise SqlServer/db2 will fail with 'Invalid JDBC data type 5.001.'
binder.setNull(Types.VARCHAR);
} else {
binder.setString(rawJson);
}
}
}
}
/**
* ScalarType that uses Jackson ObjectMapper to marshall/unmarshall to/from JSON
* and storing them in one of JSON, JSONB, VARCHAR, CLOB or BLOB.
*/
private static abstract class Base<T> extends ScalarTypeBase<T> {
protected final ObjectWriter objectWriter;
protected final ObjectMapper objectReader;
protected final JavaType deserType;
protected final String pgType;
private final DocPropertyType docType;
Base(Class<T> cls, TypeJsonManager jsonManager, AnnotatedField field, int dbType, DocPropertyType docType) {
super(cls, false, dbType);
this.objectReader = jsonManager.objectMapper();
this.pgType = jsonManager.postgresType(dbType);
this.docType = docType;
final JacksonTypeHelper helper = new JacksonTypeHelper(field, objectReader);
this.deserType = helper.type();
this.objectWriter = helper.objectWriter();
}
@Override
public T read(DataReader reader) throws SQLException {
String json = reader.getString();
if (json == null || json.isEmpty()) {
return null;
}
try {
return objectReader.readValue(json, deserType);
} catch (IOException e) {
throw new TextException("Failed to parse JSON [{}] as " + deserType, json, e);
}
}
@Override
public void bind(DataBinder binder, T value) throws SQLException {
if (pgType != null) {
String rawJson = (value == null) ? null : formatValue(value);
binder.setObject(PostgresHelper.asObject(pgType, rawJson));
} else {
if (value == null) {
binder.setNull(Types.VARCHAR); // use varchar, otherwise SqlServer/db2 will fail with 'Invalid JDBC data type 5.001.'
} else {
binder.setString(formatValue(value));
}
}
}
@Override
public final Object toJdbcType(Object value) {
// no type conversion supported
return value;
}
@Override
@SuppressWarnings("unchecked")
public final T toBeanType(Object value) {
// no type conversion supported
return (T) value;
}
@Override
public final String formatValue(T value) {
try {
return objectWriter.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new PersistenceException("Unable to create JSON", e);
}
}
@Override
public final T parse(String value) {
try {
return objectReader.readValue(value, deserType);
} catch (IOException e) {
throw new TextException("Failed to parse JSON [{}] as " + deserType, value, e);
}
}
@Override
public final DocPropertyType docType() {
return docType;
}
@Override
public final T jsonRead(JsonParser parser) throws IOException {
return objectReader.readValue(parser, deserType);
}
@Override
public final void jsonWrite(JsonGenerator writer, T value) throws IOException {
objectWriter.writeValue(writer, value);
}
@Override
public final T readData(DataInput dataInput) throws IOException {
if (!dataInput.readBoolean()) {
return null;
} else {
return parse(dataInput.readUTF());
}
}
@Override
public final void writeData(DataOutput dataOutput, T value) throws IOException {
if (value == null) {
dataOutput.writeBoolean(false);
} else {
ScalarHelp.writeUTF(dataOutput, format(value));
}
}
}
}
@@ -1,44 +1,35 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ebean.ModifyAwareType;
import io.ebean.annotation.MutationDetection;
import io.ebean.config.dbplatform.DbPlatformType;
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 ObjectMapper objectMapper;
private final Object mapper;
private final MutationDetection mutationDetection;
TypeJsonManager(boolean postgres, Object objectMapper, MutationDetection mutationDetection) {
TypeJsonManager(boolean postgres, Object mapper, MutationDetection mutationDetection) {
this.postgres = postgres;
this.objectMapper = (ObjectMapper) objectMapper;
this.mapper = mapper;
this.mutationDetection = mutationDetection;
}
MutationDetection mutationDetection() {
@Override
public MutationDetection mutationDetection() {
return mutationDetection;
}
ObjectMapper objectMapper() {
return objectMapper;
@Override
public Object mapper() {
return mapper;
}
boolean keepSource(DeployBeanProperty prop) {
if (prop.getMutationDetection() == MutationDetection.SOURCE) {
return true;
} else if (prop.getMutationDetection() == MutationDetection.DEFAULT) {
prop.setMutationDetection(mutationDetection);
return mutationDetection == MutationDetection.SOURCE;
} else {
return false;
}
}
String postgresType(int dbType) {
@Override
public String postgresType(int dbType) {
if (postgres) {
switch (dbType) {
case DbPlatformType.JSON:
@@ -9,6 +9,7 @@ module io.ebean.core {
uses io.ebean.datasource.DataSourceAlertFactory;
uses io.ebean.core.type.ExtraTypeFactory;
uses io.ebean.core.type.ScalarTypeSetFactory;
uses io.ebean.core.type.ScalarJsonMapper;
uses io.ebeanservice.docstore.api.DocStoreFactory;
uses io.ebean.migration.auto.AutoMigrationRunner;
uses io.avaje.classpath.scanner.ClassPathScannerFactory;