#2452 - server-wide defaultJsonMutation SOURCE is not honored by DbJson scalar types (e.g. List<String>)

This commit is contained in:
Rob Bygrave
2021-12-07 18:13:17 +13:00
parent b627794f8f
commit 9bdcd6c55e
3 changed files with 78 additions and 4 deletions
@@ -337,11 +337,10 @@ public final class DefaultTypeManager implements TypeManager {
Type genericType = prop.getGenericType();
boolean hasJacksonAnnotations = objectMapperPresent && checkJacksonAnnotations(prop);
boolean keepSource = prop.getMutationDetection() == MutationDetection.SOURCE;
if (type.equals(List.class)) {
DocPropertyType docType = getDocType(genericType);
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), keepSource);
return ScalarTypeJsonList.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop));
} else {
return createJsonObjectMapperType(prop, dbType, docType);
}
@@ -349,14 +348,14 @@ public final class DefaultTypeManager implements TypeManager {
if (type.equals(Set.class)) {
DocPropertyType docType = getDocType(genericType);
if (!hasJacksonAnnotations && isValueTypeSimple(genericType)) {
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), keepSource);
return ScalarTypeJsonSet.typeFor(postgres, dbType, docType, prop.isNullable(), jsonManager.keepSource(prop));
} else {
return createJsonObjectMapperType(prop, dbType, docType);
}
}
if (type.equals(Map.class)) {
if (!hasJacksonAnnotations && isMapValueTypeObject(genericType)) {
return ScalarTypeJsonMap.typeFor(postgres, dbType, keepSource);
return ScalarTypeJsonMap.typeFor(postgres, dbType, jsonManager.keepSource(prop));
} else {
return createJsonObjectMapperType(prop, dbType, DocPropertyType.OBJECT);
}
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.ebean.ModifyAwareType;
import io.ebean.annotation.MutationDetection;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
final class TypeJsonManager {
@@ -25,6 +26,17 @@ final class TypeJsonManager {
return objectMapper;
}
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) {
if (postgres) {
switch (dbType) {
@@ -0,0 +1,63 @@
package org.tests.json;
import io.ebean.Database;
import io.ebean.DatabaseFactory;
import io.ebean.ValuePair;
import io.ebean.annotation.ForPlatform;
import io.ebean.annotation.MutationDetection;
import io.ebean.annotation.Platform;
import io.ebean.config.DatabaseConfig;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.tests.model.json.EBasicJsonList;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
class TestJsonSourceDefault {
@Test
@ForPlatform(Platform.H2)
@Disabled
void testDirtyValues_diffSource() {
DatabaseConfig config = new DatabaseConfig();
config.getDataSourceConfig()
.setUsername("sa")
.setPassword("")
.setUrl("jdbc:h2:mem:testJsonSourceDirtyValues");
config.setName("jsonSource");
config.setDefaultServer(false);
config.setRegister(false);
config.setDdlGenerate(true);
config.setDdlRun(true);
config.setDdlExtra(false);
config.addClass(EBasicJsonList.class);
config.setJsonMutationDetection(MutationDetection.SOURCE);
Database db = DatabaseFactory.create(config);
try {
assertThat(db).isNotNull();
EBasicJsonList bean = new EBasicJsonList();
bean.getTags().add("aa");
bean.getTags().add("bb");
db.save(bean);
bean = db.find(EBasicJsonList.class, bean.getId());
bean.getTags().add("cc");
final Map<String, ValuePair> dirtyValues = db.beanState(bean).dirtyValues();
assertThat(dirtyValues).containsOnlyKeys("tags");
final ValuePair diff = dirtyValues.get("tags");
assertThat(diff.getOldValue()).isInstanceOf(List.class).asList().containsExactly("aa", "bb");
assertThat(diff.getNewValue()).isInstanceOf(List.class).asList().containsExactly("aa", "bb", "cc");
} finally {
if (db != null) {
db.shutdown();
}
}
}
}