Restore support for custom bind converters in collections

Update the `beansConverterService` introduced in commit f4e05c91c7
so that it can also handle collection based conversions.

Fixes gh-38734
This commit is contained in:
Phillip Webb 2023-12-11 16:41:14 -08:00
parent beba1f176a
commit 0fe7d78732
2 changed files with 42 additions and 0 deletions

View File

@ -29,6 +29,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionService;
@ -63,6 +64,7 @@ class ConversionServiceDeducer {
ConverterBeans converterBeans = new ConverterBeans(applicationContext);
if (!converterBeans.isEmpty()) {
FormattingConversionService beansConverterService = new FormattingConversionService();
DefaultConversionService.addCollectionConverters(beansConverterService);
converterBeans.addTo(beansConverterService);
conversionServices.add(beansConverterService);
}

View File

@ -679,6 +679,20 @@ class ConfigurationPropertiesTests {
assertThat(properties.getAlien().name).isEqualTo("rennaT flA");
}
@Test // gh-38734
void loadWhenBeanFactoryConversionServiceAndConverterBeanCanUseConverterBeanWithCollections() {
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new PersonConverter());
this.context.getBeanFactory().setConversionService(conversionService);
load(new Class<?>[] { AlienConverterConfiguration.class, PersonAndAliensProperties.class },
"test.person=John Smith", "test.aliens=Alf Tanner,Gilbert");
PersonAndAliensProperties properties = this.context.getBean(PersonAndAliensProperties.class);
assertThat(properties.getPerson().firstName).isEqualTo("John");
assertThat(properties.getPerson().lastName).isEqualTo("Smith");
assertThat(properties.getAliens().get(0).name).isEqualTo("rennaT flA");
assertThat(properties.getAliens().get(1).name).isEqualTo("trebliG");
}
@Test
void loadWhenConfigurationConverterIsNotQualifiedShouldNotConvert() {
assertThatExceptionOfType(BeanCreationException.class)
@ -2090,6 +2104,32 @@ class ConfigurationPropertiesTests {
}
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "test")
static class PersonAndAliensProperties {
private Person person;
private List<Alien> aliens;
Person getPerson() {
return this.person;
}
void setPerson(Person person) {
this.person = person;
}
List<Alien> getAliens() {
return this.aliens;
}
void setAliens(List<Alien> aliens) {
this.aliens = aliens;
}
}
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "sample")
static class MapWithNumericKeyProperties {