Skip to content

Commit 5b76259

Browse files
ngocnhan-tran1996schauder
authored andcommitted
Consider target type for custom converter determination.
Closes #1842 Original pull request #1876
1 parent 033ac1f commit 5b76259

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,9 @@ public Object writeValue(@Nullable Object value, TypeInformation<?> type) {
693693

694694
if (getConversions().isSimpleType(value.getClass())) {
695695

696-
Optional<Class<?>> customWriteTarget = getConversions().getCustomWriteTarget(type.getType());
696+
Optional<Class<?>> customWriteTarget = getConversions().hasCustomWriteTarget(value.getClass(), type.getType())
697+
? getConversions().getCustomWriteTarget(value.getClass(), type.getType())
698+
: getConversions().getCustomWriteTarget(type.getType());
697699
if (customWriteTarget.isPresent()) {
698700
return getConversionService().convert(value, customWriteTarget.get());
699701
}
@@ -725,12 +727,15 @@ public Object writeValue(@Nullable Object value, TypeInformation<?> type) {
725727
return writeCollection((Iterable<?>) value, type);
726728
}
727729

728-
RelationalPersistentEntity<?> persistentEntity = getMappingContext().getPersistentEntity(value.getClass());
730+
if (getMappingContext().hasPersistentEntityFor(value.getClass())) {
729731

730-
if (persistentEntity != null) {
732+
RelationalPersistentEntity<?> persistentEntity = getMappingContext().getPersistentEntity(value.getClass());
731733

732-
Object id = persistentEntity.getIdentifierAccessor(value).getIdentifier();
733-
return writeValue(id, type);
734+
if (persistentEntity != null) {
735+
736+
Object id = persistentEntity.getIdentifierAccessor(value).getIdentifier();
737+
return writeValue(id, type);
738+
}
734739
}
735740

736741
return

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

+53-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import java.util.Arrays;
2120
import java.util.Collections;
2221
import java.util.Date;
2322
import java.util.EnumSet;
@@ -26,23 +25,28 @@
2625
import java.util.Objects;
2726
import java.util.Set;
2827

28+
import java.util.UUID;
2929
import org.junit.jupiter.api.Test;
3030

3131
import org.springframework.beans.factory.annotation.Value;
32+
import org.springframework.core.convert.TypeDescriptor;
3233
import org.springframework.core.convert.converter.Converter;
34+
import org.springframework.core.convert.converter.GenericConverter;
3335
import org.springframework.data.annotation.Id;
3436
import org.springframework.data.annotation.PersistenceCreator;
3537
import org.springframework.data.convert.ConverterBuilder;
3638
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
3739
import org.springframework.data.convert.CustomConversions;
3840
import org.springframework.data.convert.CustomConversions.StoreConversions;
3941
import org.springframework.data.convert.ReadingConverter;
42+
import org.springframework.data.convert.WritingConverter;
4043
import org.springframework.data.mapping.model.SimpleTypeHolder;
4144
import org.springframework.data.projection.EntityProjection;
4245
import org.springframework.data.relational.core.mapping.Column;
4346
import org.springframework.data.relational.core.mapping.Embedded;
4447
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
4548
import org.springframework.data.relational.domain.RowDocument;
49+
import org.springframework.data.util.TypeInformation;
4650

4751
/**
4852
* Unit tests for {@link MappingRelationalConverter}.
@@ -210,6 +214,27 @@ void projectShouldReadProjectionWithNestedEntity() {
210214
assertThat(person.getAddresses()).extracting(Address::getStreet).hasSize(1).containsOnly("hwy");
211215
}
212216

217+
@SuppressWarnings("unchecked")
218+
@Test
219+
void shouldApplyGenericTypeConverter() {
220+
221+
converter = new MappingRelationalConverter(converter.getMappingContext(),
222+
new CustomConversions(StoreConversions.NONE, List.of(GenericTypeConverter.INSTANCE)));
223+
224+
var stringResult = (GenericClass<String>) converter.writeValue("test", TypeInformation.of(GenericClass.class));
225+
var uuidResult = (GenericClass<UUID>) converter.writeValue(UUID.fromString("1234567-8910-1112-1314-151617181920"), TypeInformation.of(GenericClass.class));
226+
227+
var stringGeneric = new GenericClass<>("test");
228+
var stringGenericResult = (String) converter.writeValue(stringGeneric, TypeInformation.of(String.class));
229+
var uuidGeneric = new GenericClass<>(UUID.fromString("1234567-8910-1112-1314-151617181920"));
230+
var uuidGenericResult = (UUID) converter.writeValue(uuidGeneric, TypeInformation.of(UUID.class));
231+
232+
assertThat(stringResult.value()).isEqualTo("test");
233+
assertThat(uuidResult.value()).isEqualTo(UUID.fromString("1234567-8910-1112-1314-151617181920"));
234+
assertThat(stringGenericResult).isEqualTo("test");
235+
assertThat(uuidGenericResult).isEqualTo(UUID.fromString("1234567-8910-1112-1314-151617181920"));
236+
}
237+
213238
static class SimpleType {
214239

215240
@Id String id;
@@ -365,4 +390,31 @@ public MyEnum convert(String source) {
365390

366391
}
367392

393+
@WritingConverter
394+
enum GenericTypeConverter implements GenericConverter {
395+
396+
INSTANCE;
397+
398+
@Override
399+
public Set<ConvertiblePair> getConvertibleTypes() {
400+
return Set.of(
401+
new ConvertiblePair(String.class, GenericClass.class),
402+
new ConvertiblePair(UUID.class, GenericClass.class),
403+
new ConvertiblePair(GenericClass.class, String.class),
404+
new ConvertiblePair(GenericClass.class, UUID.class)
405+
);
406+
}
407+
408+
@Override
409+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
410+
if (targetType.getType() == GenericClass.class)
411+
return new GenericClass<>(source);
412+
413+
return ((GenericClass<?>) source).value();
414+
}
415+
416+
}
417+
418+
public record GenericClass<T>(T value) { }
419+
368420
}

0 commit comments

Comments
 (0)