Skip to content

Commit 2950a8b

Browse files
committed
DATAJDBC-516 - Consider simple types and custom conversions in BasicRelationalConverter read/write.
1 parent c0803dd commit 2950a8b

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/BasicRelationalConverter.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.data.mapping.model.SimpleTypeHolder;
3838
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
3939
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
40+
import org.springframework.data.util.ClassTypeInformation;
4041
import org.springframework.data.util.TypeInformation;
4142
import org.springframework.lang.Nullable;
4243
import org.springframework.util.Assert;
@@ -177,7 +178,18 @@ public Object writeValue(@Nullable Object value, TypeInformation<?> type) {
177178
return null;
178179
}
179180

180-
Class<?> rawType = type.getType();
181+
if (getConversions().isSimpleType(value.getClass())) {
182+
183+
if (ClassTypeInformation.OBJECT != type) {
184+
185+
if (conversionService.canConvert(value.getClass(), type.getType())) {
186+
value = conversionService.convert(value, type.getType());
187+
}
188+
}
189+
190+
return getPotentiallyConvertedSimpleWrite(value);
191+
}
192+
181193
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(value.getClass());
182194

183195
if (persistentEntity != null) {
@@ -186,11 +198,7 @@ public Object writeValue(@Nullable Object value, TypeInformation<?> type) {
186198
return writeValue(id, type);
187199
}
188200

189-
if (rawType.isInstance(value)) {
190-
return getPotentiallyConvertedSimpleWrite(value);
191-
}
192-
193-
return conversionService.convert(value, rawType);
201+
return conversionService.convert(value, type.getType());
194202
}
195203

196204
/**

spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/BasicRelationalConverterUnitTests.java

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
import lombok.Data;
2121
import lombok.Value;
2222

23+
import java.util.Set;
24+
25+
import org.junit.Before;
2326
import org.junit.Test;
27+
28+
import org.springframework.core.convert.converter.GenericConverter;
29+
import org.springframework.data.convert.ConverterBuilder;
30+
import org.springframework.data.convert.CustomConversions;
2431
import org.springframework.data.mapping.PersistentPropertyAccessor;
2532
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
2633
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
@@ -35,7 +42,19 @@
3542
public class BasicRelationalConverterUnitTests {
3643

3744
RelationalMappingContext context = new RelationalMappingContext();
38-
RelationalConverter converter = new BasicRelationalConverter(context);
45+
RelationalConverter converter;
46+
47+
@Before
48+
public void before() throws Exception {
49+
50+
Set<GenericConverter> converters = ConverterBuilder.writing(MyValue.class, String.class, MyValue::getFoo)
51+
.andReading(MyValue::new).getConverters();
52+
53+
CustomConversions conversions = new CustomConversions(CustomConversions.StoreConversions.NONE, converters);
54+
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
55+
56+
converter = new BasicRelationalConverter(context, conversions);
57+
}
3958

4059
@Test // DATAJDBC-235
4160
@SuppressWarnings("unchecked")
@@ -73,22 +92,49 @@ public void shouldConvertStringToEnum() {
7392
@SuppressWarnings("unchecked")
7493
public void shouldCreateInstance() {
7594

76-
RelationalPersistentEntity<MyValue> entity = (RelationalPersistentEntity) context
77-
.getRequiredPersistentEntity(MyValue.class);
95+
RelationalPersistentEntity<WithConstructorCreation> entity = (RelationalPersistentEntity) context
96+
.getRequiredPersistentEntity(WithConstructorCreation.class);
7897

79-
MyValue result = converter.createInstance(entity, it -> "bar");
98+
WithConstructorCreation result = converter.createInstance(entity, it -> "bar");
8099

81100
assertThat(result.getFoo()).isEqualTo("bar");
82101
}
83102

103+
@Test // DATAJDBC-516
104+
public void shouldConsiderWriteConverter() {
105+
106+
Object result = converter.writeValue(new MyValue("hello-world"), ClassTypeInformation.from(MyValue.class));
107+
108+
assertThat(result).isEqualTo("hello-world");
109+
}
110+
111+
@Test // DATAJDBC-516
112+
public void shouldConsiderReadConverter() {
113+
114+
Object result = converter.readValue("hello-world", ClassTypeInformation.from(MyValue.class));
115+
116+
assertThat(result).isEqualTo(new MyValue("hello-world"));
117+
}
118+
84119
@Data
85120
static class MyEntity {
86121
boolean flag;
87122
}
88123

124+
@Value
125+
static class WithConstructorCreation {
126+
String foo;
127+
}
128+
89129
@Value
90130
static class MyValue {
91-
final String foo;
131+
String foo;
132+
}
133+
134+
@Value
135+
static class MyEntityWithConvertibleProperty {
136+
137+
MyValue myValue;
92138
}
93139

94140
enum MyEnum {

0 commit comments

Comments
 (0)