Skip to content

Commit 0fe7d78

Browse files
committed
Restore support for custom bind converters in collections
Update the `beansConverterService` introduced in commit f4e05c9 so that it can also handle collection based conversions. Fixes gh-38734
1 parent beba1f1 commit 0fe7d78

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.core.convert.ConversionService;
3030
import org.springframework.core.convert.converter.Converter;
3131
import org.springframework.core.convert.converter.GenericConverter;
32+
import org.springframework.core.convert.support.DefaultConversionService;
3233
import org.springframework.format.Formatter;
3334
import org.springframework.format.FormatterRegistry;
3435
import org.springframework.format.support.FormattingConversionService;
@@ -63,6 +64,7 @@ private List<ConversionService> getConversionServices(ConfigurableApplicationCon
6364
ConverterBeans converterBeans = new ConverterBeans(applicationContext);
6465
if (!converterBeans.isEmpty()) {
6566
FormattingConversionService beansConverterService = new FormattingConversionService();
67+
DefaultConversionService.addCollectionConverters(beansConverterService);
6668
converterBeans.addTo(beansConverterService);
6769
conversionServices.add(beansConverterService);
6870
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,20 @@ void loadWhenBeanFactoryConversionServiceAndConverterBeanCanUseConverterBean() {
679679
assertThat(properties.getAlien().name).isEqualTo("rennaT flA");
680680
}
681681

682+
@Test // gh-38734
683+
void loadWhenBeanFactoryConversionServiceAndConverterBeanCanUseConverterBeanWithCollections() {
684+
DefaultConversionService conversionService = new DefaultConversionService();
685+
conversionService.addConverter(new PersonConverter());
686+
this.context.getBeanFactory().setConversionService(conversionService);
687+
load(new Class<?>[] { AlienConverterConfiguration.class, PersonAndAliensProperties.class },
688+
"test.person=John Smith", "test.aliens=Alf Tanner,Gilbert");
689+
PersonAndAliensProperties properties = this.context.getBean(PersonAndAliensProperties.class);
690+
assertThat(properties.getPerson().firstName).isEqualTo("John");
691+
assertThat(properties.getPerson().lastName).isEqualTo("Smith");
692+
assertThat(properties.getAliens().get(0).name).isEqualTo("rennaT flA");
693+
assertThat(properties.getAliens().get(1).name).isEqualTo("trebliG");
694+
}
695+
682696
@Test
683697
void loadWhenConfigurationConverterIsNotQualifiedShouldNotConvert() {
684698
assertThatExceptionOfType(BeanCreationException.class)
@@ -2090,6 +2104,32 @@ void setAlien(Alien alien) {
20902104

20912105
}
20922106

2107+
@EnableConfigurationProperties
2108+
@ConfigurationProperties(prefix = "test")
2109+
static class PersonAndAliensProperties {
2110+
2111+
private Person person;
2112+
2113+
private List<Alien> aliens;
2114+
2115+
Person getPerson() {
2116+
return this.person;
2117+
}
2118+
2119+
void setPerson(Person person) {
2120+
this.person = person;
2121+
}
2122+
2123+
List<Alien> getAliens() {
2124+
return this.aliens;
2125+
}
2126+
2127+
void setAliens(List<Alien> aliens) {
2128+
this.aliens = aliens;
2129+
}
2130+
2131+
}
2132+
20932133
@EnableConfigurationProperties
20942134
@ConfigurationProperties(prefix = "sample")
20952135
static class MapWithNumericKeyProperties {

0 commit comments

Comments
 (0)