DATAJDBC-516 - Consider simple types and custom conversions in BasicRelationalConverter read/write.

This commit is contained in:
Mark Paluch
2020-03-31 12:32:14 +02:00
parent c0803ddafe
commit 2950a8b1ea
2 changed files with 65 additions and 11 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -177,7 +178,18 @@ public class BasicRelationalConverter implements RelationalConverter {
return null;
}
Class<?> rawType = type.getType();
if (getConversions().isSimpleType(value.getClass())) {
if (ClassTypeInformation.OBJECT != type) {
if (conversionService.canConvert(value.getClass(), type.getType())) {
value = conversionService.convert(value, type.getType());
}
}
return getPotentiallyConvertedSimpleWrite(value);
}
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(value.getClass());
if (persistentEntity != null) {
@@ -186,11 +198,7 @@ public class BasicRelationalConverter implements RelationalConverter {
return writeValue(id, type);
}
if (rawType.isInstance(value)) {
return getPotentiallyConvertedSimpleWrite(value);
}
return conversionService.convert(value, rawType);
return conversionService.convert(value, type.getType());
}
/**

View File

@@ -20,7 +20,14 @@ import static org.assertj.core.api.Assertions.*;
import lombok.Data;
import lombok.Value;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.data.convert.ConverterBuilder;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
@@ -35,7 +42,19 @@ import org.springframework.data.util.ClassTypeInformation;
public class BasicRelationalConverterUnitTests {
RelationalMappingContext context = new RelationalMappingContext();
RelationalConverter converter = new BasicRelationalConverter(context);
RelationalConverter converter;
@Before
public void before() throws Exception {
Set<GenericConverter> converters = ConverterBuilder.writing(MyValue.class, String.class, MyValue::getFoo)
.andReading(MyValue::new).getConverters();
CustomConversions conversions = new CustomConversions(CustomConversions.StoreConversions.NONE, converters);
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
converter = new BasicRelationalConverter(context, conversions);
}
@Test // DATAJDBC-235
@SuppressWarnings("unchecked")
@@ -73,22 +92,49 @@ public class BasicRelationalConverterUnitTests {
@SuppressWarnings("unchecked")
public void shouldCreateInstance() {
RelationalPersistentEntity<MyValue> entity = (RelationalPersistentEntity) context
.getRequiredPersistentEntity(MyValue.class);
RelationalPersistentEntity<WithConstructorCreation> entity = (RelationalPersistentEntity) context
.getRequiredPersistentEntity(WithConstructorCreation.class);
MyValue result = converter.createInstance(entity, it -> "bar");
WithConstructorCreation result = converter.createInstance(entity, it -> "bar");
assertThat(result.getFoo()).isEqualTo("bar");
}
@Test // DATAJDBC-516
public void shouldConsiderWriteConverter() {
Object result = converter.writeValue(new MyValue("hello-world"), ClassTypeInformation.from(MyValue.class));
assertThat(result).isEqualTo("hello-world");
}
@Test // DATAJDBC-516
public void shouldConsiderReadConverter() {
Object result = converter.readValue("hello-world", ClassTypeInformation.from(MyValue.class));
assertThat(result).isEqualTo(new MyValue("hello-world"));
}
@Data
static class MyEntity {
boolean flag;
}
@Value
static class WithConstructorCreation {
String foo;
}
@Value
static class MyValue {
final String foo;
String foo;
}
@Value
static class MyEntityWithConvertibleProperty {
MyValue myValue;
}
enum MyEnum {