Skip to content

Convert AggregateReference by converters instead of custom code paths. #993

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 3 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>2.3.0-SNAPSHOT</version>
<version>2.3.0-992-agg-ref-converters-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>2.3.0-SNAPSHOT</version>
<version>2.3.0-992-agg-ref-converters-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>2.3.0-SNAPSHOT</version>
<version>2.3.0-992-agg-ref-converters-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>2.3.0-SNAPSHOT</version>
<version>2.3.0-992-agg-ref-converters-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.convert;

import java.util.Collections;
import java.util.Set;

import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.lang.Nullable;

/**
* Converters for aggregate references. They need a {@link ConversionService} in order to delegate the conversion of the
* content of the {@link AggregateReference}.
*
* @author Jens Schauder
* @since 2.6
*/
class AggregateReferenceConverters {
/**
* Prevent instantiation.
*/
private AggregateReferenceConverters() {}

/**
* Converts from an AggregateReference to its id, leaving the conversion of the id to the ultimate target type to the
* delegate {@link ConversionService}.
*/
@WritingConverter
static class AggregateReferenceToSimpleTypeConverter implements GenericConverter {

private final ConversionService delegate;

AggregateReferenceToSimpleTypeConverter(ConversionService delegate) {
this.delegate = delegate;
}

@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(AggregateReference.class, Object.class));
}

@Override
public Object convert(@Nullable Object source, TypeDescriptor sourceDescriptor, TypeDescriptor targetDescriptor) {

if (source == null) {
return null;
}

// if the target type is an AggregateReference we just going to assume it is of the correct type,
// because it was already converted.
Class<?> objectType = targetDescriptor.getObjectType();
if (objectType.isAssignableFrom(AggregateReference.class)) {
return source;
}

Object id = ((AggregateReference<?, ?>) source).getId();

if (id == null) {
throw new IllegalStateException(
String.format("Aggregate references id must not be null when converting to %s from %s to %s", source,
sourceDescriptor, targetDescriptor));
}

return delegate.convert(id, TypeDescriptor.valueOf(id.getClass()), targetDescriptor);
}
}

/**
* Convert any simple type to an {@link AggregateReference}. If the {@literal targetDescriptor} contains information
* about the generic type id will properly get converted to the desired type by the delegate
* {@link ConversionService}.
*/
@ReadingConverter
static class SimpleTypeToAggregateReferenceConverter implements GenericConverter {

private final ConversionService delegate;

SimpleTypeToAggregateReferenceConverter(ConversionService delegate) {
this.delegate = delegate;
}

@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(Object.class, AggregateReference.class));
}

@Override
public Object convert(@Nullable Object source, TypeDescriptor sourceDescriptor, TypeDescriptor targetDescriptor) {

if (source == null) {
return null;
}

ResolvableType componentType = targetDescriptor.getResolvableType().getGenerics()[1];
TypeDescriptor targetType = TypeDescriptor.valueOf(componentType.resolve());
Object convertedId = delegate.convert(source, TypeDescriptor.valueOf(source.getClass()), targetType);

return AggregateReference.to(convertedId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
Expand Down Expand Up @@ -220,16 +222,12 @@ public Object readValue(@Nullable Object value, TypeInformation<?> type) {
}

if (getConversions().hasCustomReadTarget(value.getClass(), type.getType())) {
return getConversionService().convert(value, type.getType());
}

if (AggregateReference.class.isAssignableFrom(type.getType())) {

if (type.getType().isAssignableFrom(value.getClass())) {
return value;
}
TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(value.getClass());
TypeDescriptor targetDescriptor = typeInformationToTypeDescriptor(type);

return readAggregateReference(value, type);
return getConversionService().convert(value, sourceDescriptor,
targetDescriptor);
}

if (value instanceof Array) {
Expand All @@ -243,12 +241,11 @@ public Object readValue(@Nullable Object value, TypeInformation<?> type) {
return super.readValue(value, type);
}

@SuppressWarnings("ConstantConditions")
private Object readAggregateReference(@Nullable Object value, TypeInformation<?> type) {
private static TypeDescriptor typeInformationToTypeDescriptor(TypeInformation<?> type) {

TypeInformation<?> idType = type.getSuperTypeInformation(AggregateReference.class).getTypeArguments().get(1);
Class<?>[] generics = type.getTypeArguments().stream().map(TypeInformation::getType).toArray(Class[]::new);

return AggregateReference.to(readValue(value, idType));
return new TypeDescriptor(ResolvableType.forClassWithGenerics(type.getType(), generics), null, null);
}

/*
Expand All @@ -263,10 +260,6 @@ public Object writeValue(@Nullable Object value, TypeInformation<?> type) {
return null;
}

if (AggregateReference.class.isAssignableFrom(value.getClass())) {
return writeValue(((AggregateReference) value).getId(), type);
}

return super.writeValue(value, type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package org.springframework.data.jdbc.core.convert;

import java.util.ArrayList;
import java.util.Collection;
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;
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;

Expand All @@ -36,8 +40,19 @@
*/
public class JdbcCustomConversions extends CustomConversions {

private static final Collection<Object> STORE_CONVERTERS = Collections
.unmodifiableCollection(Jsr310TimestampBasedConverters.getConvertersToRegister());
private static final Collection<Object> STORE_CONVERTERS;

static {

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

ConversionService conversionService = DefaultConversionService.getSharedInstance();
converters.add(new AggregateReferenceConverters.AggregateReferenceToSimpleTypeConverter(conversionService));
converters.add(new AggregateReferenceConverters.SimpleTypeToAggregateReferenceConverter(conversionService));

STORE_CONVERTERS = Collections.unmodifiableCollection(converters);

}
private static final StoreConversions STORE_CONVERSIONS = StoreConversions.of(JdbcSimpleTypes.HOLDER,
STORE_CONVERTERS);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.core.convert;

import org.junit.jupiter.api.Test;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.jdbc.core.mapping.AggregateReference;

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

/**
* Tests for converters from an to {@link org.springframework.data.jdbc.core.mapping.AggregateReference}.
*
* @author Jens Schauder
*/
class AggregateReferenceConvertersUnitTests {

AggregateReferenceConverters.SimpleTypeToAggregateReferenceConverter simpleToAggregate = new AggregateReferenceConverters.SimpleTypeToAggregateReferenceConverter(DefaultConversionService.getSharedInstance());
AggregateReferenceConverters.AggregateReferenceToSimpleTypeConverter aggregateToSimple = new AggregateReferenceConverters.AggregateReferenceToSimpleTypeConverter(DefaultConversionService.getSharedInstance());

@Test // #992
void convertsFromSimpleValue() {

ResolvableType aggregateReferenceWithIdTypeInteger = ResolvableType.forClassWithGenerics(AggregateReference.class, String.class, Integer.class);
final Object converted = simpleToAggregate.convert(23, TypeDescriptor.forObject(23), new TypeDescriptor(aggregateReferenceWithIdTypeInteger, null, null));

assertThat(converted).isEqualTo(AggregateReference.to(23));
}

@Test // #992
void convertsFromSimpleValueThatNeedsSeparateConversion() {

ResolvableType aggregateReferenceWithIdTypeInteger = ResolvableType.forClassWithGenerics(AggregateReference.class, String.class, Long.class);
final Object converted = simpleToAggregate.convert(23, TypeDescriptor.forObject(23), new TypeDescriptor(aggregateReferenceWithIdTypeInteger, null, null));

assertThat(converted).isEqualTo(AggregateReference.to(23L));
}

@Test // #992
void convertsFromSimpleValueWithMissingTypeInformation() {

final Object converted = simpleToAggregate.convert(23, TypeDescriptor.forObject(23), TypeDescriptor.valueOf(AggregateReference.class));

assertThat(converted).isEqualTo(AggregateReference.to(23));
}

@Test // #992
void convertsToSimpleValue() {

final AggregateReference<Object, Integer> source = AggregateReference.to(23);

final Object converted = aggregateToSimple.convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(Integer.class));

assertThat(converted).isEqualTo(23);
}

@Test // #992
void convertsToSimpleValueThatNeedsSeparateConversion() {

final AggregateReference<Object, Integer> source = AggregateReference.to(23);

final Object converted = aggregateToSimple.convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(Long.class));

assertThat(converted).isEqualTo(23L);
}


}
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>2.3.0-SNAPSHOT</version>
<version>2.3.0-992-agg-ref-converters-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>2.3.0-SNAPSHOT</version>
<version>2.3.0-992-agg-ref-converters-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.util.Optional;
import java.util.function.Function;

import org.springframework.core.ResolvableType;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.convert.CustomConversions;
Expand Down