Browse Source

DATACMNS-1743 - Fix lazy repository lookups in DomainClassConverter.

We now eagerly register DomainClassConverter as converter with the ConversionService in the web setup. We also refrain from explicitly registering the traget converters with the ConversionService as that might trigger a ConcurrentModificationException if called during an iteration over the converters in the first place. As DomainClassConverter registers itself for all ConvertiblePairs and delegates to the individual converters anyway.
2.2.x
Oliver Drotbohm 6 years ago
parent
commit
cf41b9711f
No known key found for this signature in database
GPG Key ID: 6E42B5787543F690
  1. 4
      src/main/java/org/springframework/data/repository/support/DomainClassConverter.java
  2. 11
      src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java

4
src/main/java/org/springframework/data/repository/support/DomainClassConverter.java

@ -62,6 +62,7 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr @@ -62,6 +62,7 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
Assert.notNull(conversionService, "ConversionService must not be null!");
this.conversionService = conversionService;
this.conversionService.addConverter(this);
}
/*
@ -112,10 +113,7 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr @@ -112,10 +113,7 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
Repositories repositories = new Repositories(context);
this.toEntityConverter = Optional.of(new ToEntityConverter(repositories, conversionService));
this.toEntityConverter.ifPresent(it -> conversionService.addConverter(it));
this.toIdConverter = Optional.of(new ToIdConverter(repositories, conversionService));
this.toIdConverter.ifPresent(it -> conversionService.addConverter(it));
return repositories;
});

11
src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java

@ -34,6 +34,7 @@ import org.springframework.context.ApplicationContext; @@ -34,6 +34,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.support.DummyRepositoryFactoryBean;
@ -189,6 +190,16 @@ public class DomainClassConverterUnitTests { @@ -189,6 +190,16 @@ public class DomainClassConverterUnitTests {
assertThat(toIdConverter).map(it -> it.matches(SUB_USER_TYPE, target)).hasValue(false);
}
@Test // DATACMNS-1743
public void registersConvertersOnConversionService() {
ConfigurableConversionService conversionService = new DefaultConversionService();
DomainClassConverter<?> converter = new DomainClassConverter<>(conversionService);
converter.setApplicationContext(initContextWithRepo());
assertThat(conversionService.canConvert(String.class, User.class)).isTrue();
}
private ApplicationContext initContextWithRepo() {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);

Loading…
Cancel
Save