Skip to content

Commit

Permalink
Restore support for custom bind converters in collections
Browse files Browse the repository at this point in the history
Update the `beansConverterService` introduced in commit f4e05c9
so that it can also handle collection based conversions.

Fixes gh-38734
  • Loading branch information
philwebb committed Dec 12, 2023
1 parent beba1f1 commit 0fe7d78
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,6 +64,7 @@ private List<ConversionService> getConversionServices(ConfigurableApplicationCon
ConverterBeans converterBeans = new ConverterBeans(applicationContext);
if (!converterBeans.isEmpty()) {
FormattingConversionService beansConverterService = new FormattingConversionService();
DefaultConversionService.addCollectionConverters(beansConverterService);
converterBeans.addTo(beansConverterService);
conversionServices.add(beansConverterService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -2090,6 +2104,32 @@ void setAlien(Alien alien) {

}

@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 {
Expand Down

0 comments on commit 0fe7d78

Please sign in to comment.