|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.*;
|
19 | 19 |
|
20 |
| -import java.util.Arrays; |
21 | 20 | import java.util.Collections;
|
22 | 21 | import java.util.Date;
|
23 | 22 | import java.util.EnumSet;
|
|
26 | 25 | import java.util.Objects;
|
27 | 26 | import java.util.Set;
|
28 | 27 |
|
| 28 | +import java.util.UUID; |
29 | 29 | import org.junit.jupiter.api.Test;
|
30 | 30 |
|
31 | 31 | import org.springframework.beans.factory.annotation.Value;
|
| 32 | +import org.springframework.core.convert.TypeDescriptor; |
32 | 33 | import org.springframework.core.convert.converter.Converter;
|
| 34 | +import org.springframework.core.convert.converter.GenericConverter; |
33 | 35 | import org.springframework.data.annotation.Id;
|
34 | 36 | import org.springframework.data.annotation.PersistenceCreator;
|
35 | 37 | import org.springframework.data.convert.ConverterBuilder;
|
36 | 38 | import org.springframework.data.convert.ConverterBuilder.ConverterAware;
|
37 | 39 | import org.springframework.data.convert.CustomConversions;
|
38 | 40 | import org.springframework.data.convert.CustomConversions.StoreConversions;
|
39 | 41 | import org.springframework.data.convert.ReadingConverter;
|
| 42 | +import org.springframework.data.convert.WritingConverter; |
40 | 43 | import org.springframework.data.mapping.model.SimpleTypeHolder;
|
41 | 44 | import org.springframework.data.projection.EntityProjection;
|
42 | 45 | import org.springframework.data.relational.core.mapping.Column;
|
43 | 46 | import org.springframework.data.relational.core.mapping.Embedded;
|
44 | 47 | import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
45 | 48 | import org.springframework.data.relational.domain.RowDocument;
|
| 49 | +import org.springframework.data.util.TypeInformation; |
46 | 50 |
|
47 | 51 | /**
|
48 | 52 | * Unit tests for {@link MappingRelationalConverter}.
|
@@ -210,6 +214,27 @@ void projectShouldReadProjectionWithNestedEntity() {
|
210 | 214 | assertThat(person.getAddresses()).extracting(Address::getStreet).hasSize(1).containsOnly("hwy");
|
211 | 215 | }
|
212 | 216 |
|
| 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 | + |
213 | 238 | static class SimpleType {
|
214 | 239 |
|
215 | 240 | @Id String id;
|
@@ -365,4 +390,31 @@ public MyEnum convert(String source) {
|
365 | 390 |
|
366 | 391 | }
|
367 | 392 |
|
| 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 | + |
368 | 420 | }
|
0 commit comments