From 0fe7d78732b54de599aa6c974a105653cb058be4 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 11 Dec 2023 16:41:14 -0800 Subject: [PATCH] Restore support for custom bind converters in collections Update the `beansConverterService` introduced in commit f4e05c91c74e so that it can also handle collection based conversions. Fixes gh-38734 --- .../properties/ConversionServiceDeducer.java | 2 + .../ConfigurationPropertiesTests.java | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java index 775680c794ae..a80535000f6f 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConversionServiceDeducer.java @@ -29,6 +29,7 @@ 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 @@ private List getConversionServices(ConfigurableApplicationCon ConverterBeans converterBeans = new ConverterBeans(applicationContext); if (!converterBeans.isEmpty()) { FormattingConversionService beansConverterService = new FormattingConversionService(); + DefaultConversionService.addCollectionConverters(beansConverterService); converterBeans.addTo(beansConverterService); conversionServices.add(beansConverterService); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index 2e2c22377472..ee5b68be1656 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -679,6 +679,20 @@ void loadWhenBeanFactoryConversionServiceAndConverterBeanCanUseConverterBean() { 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 @@ void setAlien(Alien alien) { } + @EnableConfigurationProperties + @ConfigurationProperties(prefix = "test") + static class PersonAndAliensProperties { + + private Person person; + + private List aliens; + + Person getPerson() { + return this.person; + } + + void setPerson(Person person) { + this.person = person; + } + + List getAliens() { + return this.aliens; + } + + void setAliens(List aliens) { + this.aliens = aliens; + } + + } + @EnableConfigurationProperties @ConfigurationProperties(prefix = "sample") static class MapWithNumericKeyProperties {