Reuse custom converters from AggregateReferenceConverters.

Let the AggregateReference converters now receive also custom converters as delegates.
Remove the ArrayToObjectConverter which just uses the first element of the array from the DefaultConversion service.

This does NOT solve the underlying problem, of having two DefaultConversionServices at work.
One is in the store conversion, one constructed in the AbstractRelationalConverter.
Although it looks like they get merged, the former contains converters, using that ConversionService as a delegate.

Attempts were made to move AggregateReferenceConverters out of the store converters and just register them directly,
but then no custom read/write targets are detected, leading to failed conversions.
The same problem prevents a registration in CustomConversions as a Function<ConversionService, GenericConverter> or similar, which would allow late registration.
The problem here is that custom read/write targets require the function to get evaluated, before custom read/write targets can be determined.

Closes #1750
Original pull request: #1785
This commit is contained in:
Jens Schauder
2024-04-29 16:57:08 +02:00
committed by Mark Paluch
parent 6ae53f5577
commit 08710d8e16
4 changed files with 105 additions and 24 deletions

View File

@@ -20,7 +20,7 @@ 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;
@@ -52,9 +52,6 @@ public class JdbcCustomConversions extends CustomConversions {
}
private static final StoreConversions STORE_CONVERSIONS = StoreConversions.of(JdbcSimpleTypes.HOLDER,
STORE_CONVERTERS);
/**
* Creates an empty {@link JdbcCustomConversions} object.
*/
@@ -70,11 +67,7 @@ public class JdbcCustomConversions extends CustomConversions {
*/
public JdbcCustomConversions(List<?> converters) {
super(new ConverterConfiguration( //
STORE_CONVERSIONS, //
converters, //
JdbcCustomConversions::excludeConversionsBetweenDateAndJsr310Types //
));
super(constructConverterConfiguration(converters));
}
/**
@@ -103,6 +96,32 @@ public class JdbcCustomConversions extends CustomConversions {
super(converterConfiguration);
}
private static ConverterConfiguration constructConverterConfiguration(List<?> converters) {
StoreConversions storeConversions = storeConversions(converters);
return new ConverterConfiguration( //
storeConversions, //
converters, //
JdbcCustomConversions::excludeConversionsBetweenDateAndJsr310Types //
);
}
private static StoreConversions storeConversions(List<?> userConverters) {
List<Object> converters = new ArrayList<>(Jsr310TimestampBasedConverters.getConvertersToRegister());
DefaultConversionService defaultConversionService = new DefaultConversionService();
for (Object userConverter : userConverters) {
if (userConverter instanceof Converter<?, ?> converter)
defaultConversionService.addConverter(converter);
}
converters.addAll(AggregateReferenceConverters.getConvertersToRegister(defaultConversionService));
return StoreConversions.of(JdbcSimpleTypes.HOLDER, Collections.unmodifiableCollection(converters));
}
/**
* Obtain a read only copy of default store converters.
*