Skip to content

Reuse custom converters from AggregateReferenceConverters #1785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1750-custom-converters-for-aggregateref-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1750-custom-converters-for-aggregateref-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1750-custom-converters-for-aggregateref-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1750-custom-converters-for-aggregateref-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Collections;
import java.util.List;

import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.convert.CustomConversions;
Expand All @@ -39,21 +39,8 @@
*/
public class JdbcCustomConversions extends CustomConversions {

private static final Collection<Object> STORE_CONVERTERS;

static {

List<Object> converters = new ArrayList<>(Jsr310TimestampBasedConverters.getConvertersToRegister());

converters
.addAll(AggregateReferenceConverters.getConvertersToRegister(DefaultConversionService.getSharedInstance()));

STORE_CONVERTERS = Collections.unmodifiableCollection(converters);

}

private static final StoreConversions STORE_CONVERSIONS = StoreConversions.of(JdbcSimpleTypes.HOLDER,
STORE_CONVERTERS);
private static final Collection<Object> STORE_CONVERTERS = Collections
.unmodifiableCollection(Jsr310TimestampBasedConverters.getConvertersToRegister());

/**
* Creates an empty {@link JdbcCustomConversions} object.
Expand All @@ -69,12 +56,7 @@ public JdbcCustomConversions() {
* @param converters must not be {@literal null}.
*/
public JdbcCustomConversions(List<?> converters) {

super(new ConverterConfiguration( //
STORE_CONVERSIONS, //
converters, //
JdbcCustomConversions::excludeConversionsBetweenDateAndJsr310Types //
));
super(constructConverterConfiguration(converters));
}

/**
Expand Down Expand Up @@ -103,6 +85,16 @@ public JdbcCustomConversions(ConverterConfiguration converterConfiguration) {
super(converterConfiguration);
}

private static ConverterConfiguration constructConverterConfiguration(List<?> converters) {

return new ConverterConfiguration( //
StoreConversions.of(JdbcSimpleTypes.HOLDER, STORE_CONVERTERS), //
converters, //
JdbcCustomConversions::excludeConversionsBetweenDateAndJsr310Types //
);
}


/**
* Obtain a read only copy of default store converters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.data.jdbc.core.mapping.JdbcValue;
Expand Down Expand Up @@ -91,6 +92,8 @@ public MappingJdbcConverter(RelationalMappingContext context, RelationResolver r

this.typeFactory = JdbcTypeFactory.unsupported();
this.relationResolver = relationResolver;

registerAggregateReferenceConverters();
}

/**
Expand All @@ -110,6 +113,14 @@ public MappingJdbcConverter(RelationalMappingContext context, RelationResolver r

this.typeFactory = typeFactory;
this.relationResolver = relationResolver;

registerAggregateReferenceConverters();
}

private void registerAggregateReferenceConverters() {

ConverterRegistry registry = (ConverterRegistry) getConversionService();
AggregateReferenceConverters.getConvertersToRegister(getConversionService()).forEach(registry::addConverter);
}

@Nullable
Expand Down Expand Up @@ -327,7 +338,8 @@ private ResolvingRelationalPropertyValueProvider(AggregatePathValueProvider dele
this.accessor = accessor;
this.context = context;
this.identifier = path.isEntity()
? potentiallyAppendIdentifier(identifier, path.getRequiredLeafEntity(), property -> delegate.getValue(path.append(property)))
? potentiallyAppendIdentifier(identifier, path.getRequiredLeafEntity(),
property -> delegate.getValue(path.append(property)))
: identifier;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.assertj.core.api.SoftAssertions.*;
import static org.mockito.Mockito.*;

import java.nio.ByteBuffer;
import java.sql.Array;
import java.sql.Timestamp;
import java.time.Instant;
Expand All @@ -28,13 +29,16 @@
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
Expand All @@ -50,22 +54,28 @@
* Unit tests for {@link MappingJdbcConverter}.
*
* @author Mark Paluch
* @author Jens Schauder
*/
public class MappingJdbcConverterUnitTests {
class MappingJdbcConverterUnitTests {

JdbcMappingContext context = new JdbcMappingContext();
StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory();
MappingJdbcConverter converter = new MappingJdbcConverter( //
private static final UUID UUID = java.util.UUID.fromString("87a48aa8-a071-705e-54a9-e52fe3a012f1");
private static final byte[] BYTES_REPRESENTING_UUID = { -121, -92, -118, -88, -96, 113, 112, 94, 84, -87, -27, 47,
-29,
-96, 18, -15 };

private JdbcMappingContext context = new JdbcMappingContext();
private StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory();
private MappingJdbcConverter converter = new MappingJdbcConverter( //
context, //
(identifier, path) -> {
throw new UnsupportedOperationException();
}, //
new JdbcCustomConversions(), //
typeFactory //
typeFactory //
);

@Test // DATAJDBC-104, DATAJDBC-1384
public void testTargetTypesForPropertyType() {
void testTargetTypesForPropertyType() {

RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(DummyEntity.class);

Expand All @@ -86,7 +96,7 @@ public void testTargetTypesForPropertyType() {
}

@Test // DATAJDBC-259
public void classificationOfCollectionLikeProperties() {
void classificationOfCollectionLikeProperties() {

RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(DummyEntity.class);

Expand All @@ -102,7 +112,7 @@ public void classificationOfCollectionLikeProperties() {
}

@Test // DATAJDBC-221
public void referencesAreNotEntitiesAndGetStoredAsTheirId() {
void referencesAreNotEntitiesAndGetStoredAsTheirId() {

RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(DummyEntity.class);

Expand Down Expand Up @@ -152,6 +162,39 @@ void accessesCorrectValuesForOneToOneRelationshipWithIdenticallyNamedIdPropertie
assertThat(result).isEqualTo(new WithOneToOne("one", new Referenced(23L)));
}

@Test // GH-1750
void readByteArrayToNestedUuidWithCustomConverter() {

JdbcMappingContext context = new JdbcMappingContext();
StubbedJdbcTypeFactory typeFactory = new StubbedJdbcTypeFactory();
Converter<byte[], UUID> customConverter = new ByteArrayToUuid();
MappingJdbcConverter converter = new MappingJdbcConverter( //
context, //
(identifier, path) -> {
throw new UnsupportedOperationException();
}, //
new JdbcCustomConversions(Collections.singletonList(customConverter)), //
typeFactory //
);

assertSoftly(softly -> {
checkReadConversion(softly, converter, "uuidRef", AggregateReference.to(UUID));
checkReadConversion(softly, converter, "uuid", UUID);
checkReadConversion(softly, converter, "optionalUuid", Optional.of(UUID));
});

}

private static void checkReadConversion(SoftAssertions softly, MappingJdbcConverter converter, String propertyName,
Object expected) {

RelationalPersistentProperty property = converter.getMappingContext().getRequiredPersistentEntity(DummyEntity.class)
.getRequiredPersistentProperty(propertyName);
Object value = converter.readValue(BYTES_REPRESENTING_UUID, property.getTypeInformation() //
);

softly.assertThat(value).isEqualTo(expected);
}

private void checkConversionToTimestampAndBack(SoftAssertions softly, RelationalPersistentEntity<?> persistentEntity,
String propertyName, Object value) {
Expand Down Expand Up @@ -187,6 +230,8 @@ private static class DummyEntity {
private final Timestamp timestamp;
private final AggregateReference<DummyEntity, Long> reference;
private final UUID uuid;
private final AggregateReference<ReferencedByUuid, UUID> uuidRef;
private final Optional<UUID> optionalUuid;

// DATAJDBC-259
private final List<String> listOfString;
Expand All @@ -195,9 +240,10 @@ private static class DummyEntity {
private final OtherEntity[] arrayOfEntity;

private DummyEntity(Long id, SomeEnum someEnum, LocalDateTime localDateTime, LocalDate localDate,
LocalTime localTime, ZonedDateTime zonedDateTime, OffsetDateTime offsetDateTime, Instant instant, Date date,
Timestamp timestamp, AggregateReference<DummyEntity, Long> reference, UUID uuid, List<String> listOfString,
String[] arrayOfString, List<OtherEntity> listOfEntity, OtherEntity[] arrayOfEntity) {
LocalTime localTime, ZonedDateTime zonedDateTime, OffsetDateTime offsetDateTime, Instant instant, Date date,
Timestamp timestamp, AggregateReference<DummyEntity, Long> reference, UUID uuid,
AggregateReference<ReferencedByUuid, UUID> uuidRef, Optional<java.util.UUID> optionalUUID, List<String> listOfString, String[] arrayOfString,
List<OtherEntity> listOfEntity, OtherEntity[] arrayOfEntity) {
this.id = id;
this.someEnum = someEnum;
this.localDateTime = localDateTime;
Expand All @@ -210,6 +256,8 @@ private DummyEntity(Long id, SomeEnum someEnum, LocalDateTime localDateTime, Loc
this.timestamp = timestamp;
this.reference = reference;
this.uuid = uuid;
this.uuidRef = uuidRef;
this.optionalUuid = optionalUUID;
this.listOfString = listOfString;
this.arrayOfString = arrayOfString;
this.listOfEntity = listOfEntity;
Expand Down Expand Up @@ -290,7 +338,7 @@ private enum SomeEnum {
private static class OtherEntity {}

private static class StubbedJdbcTypeFactory implements JdbcTypeFactory {
public Object[] arraySource;
Object[] arraySource;

@Override
public Array createArray(Object[] value) {
Expand All @@ -299,9 +347,23 @@ public Array createArray(Object[] value) {
}
}

record WithOneToOne(@Id String id,@MappedCollection(idColumn = "renamed") Referenced referenced){}
private record WithOneToOne(@Id String id, @MappedCollection(idColumn = "renamed") Referenced referenced) {
}

private record Referenced(@Id Long id) {
}

record Referenced(@Id Long id) {
private record ReferencedByUuid(@Id UUID id) {
}

static class ByteArrayToUuid implements Converter<byte[], UUID> {
@Override
public UUID convert(byte[] source) {

ByteBuffer byteBuffer = ByteBuffer.wrap(source);
long high = byteBuffer.getLong();
long low = byteBuffer.getLong();
return new UUID(high, low);
}
}
}
4 changes: 2 additions & 2 deletions spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-r2dbc</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1750-custom-converters-for-aggregateref-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1750-custom-converters-for-aggregateref-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1750-custom-converters-for-aggregateref-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-1750-custom-converters-for-aggregateref-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Loading