Skip to content

Commit af2e363

Browse files
committed
Merge branch '3.1.x'
Closes gh-37942
2 parents 8d006f8 + 2262210 commit af2e363

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
4040
@Override
4141
protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> target,
4242
AggregateElementBinder elementBinder) {
43-
Class<?> collectionType = (target.getValue() != null) ? List.class : target.getType().resolve(Object.class);
4443
ResolvableType aggregateType = ResolvableType.forClassWithGenerics(List.class,
4544
target.getType().asCollection().getGenerics());
4645
ResolvableType elementType = target.getType().asCollection().getGeneric();
4746
IndexedCollectionSupplier result = new IndexedCollectionSupplier(
48-
() -> CollectionFactory.createCollection(collectionType, elementType.resolve(), 0));
47+
() -> CollectionFactory.createCollection(List.class, elementType.resolve(), 0));
4948
bindIndexed(name, target, elementBinder, aggregateType, elementType, result);
5049
if (result.wasSupplied()) {
5150
return result.get();

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,19 @@ void loadWhenBindingClasspathPatternToResourceCollectionShouldBindMultipleValues
11901190
assertThat(properties.getResources()).hasSizeGreaterThan(1);
11911191
}
11921192

1193+
@Test
1194+
void loadWhenBindingToConstructorParametersWithConversionToCustomListImplementation() {
1195+
load(ConstructorBoundCustomListPropertiesConfiguration.class, "test.values=a,b");
1196+
assertThat(this.context.getBean(ConstructorBoundCustomListProperties.class).getValues()).containsExactly("a",
1197+
"b");
1198+
}
1199+
1200+
@Test
1201+
void loadWhenBindingToJavaBeanWithConversionToCustomListImplementation() {
1202+
load(SetterBoundCustomListPropertiesConfiguration.class, "test.values=a,b");
1203+
assertThat(this.context.getBean(SetterBoundCustomListProperties.class).getValues()).containsExactly("a", "b");
1204+
}
1205+
11931206
private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
11941207
return load(new Class<?>[] { configuration }, inlinedProperties);
11951208
}
@@ -3115,4 +3128,80 @@ void setResources(Collection<Resource> resources) {
31153128

31163129
}
31173130

3131+
@EnableConfigurationProperties(ConstructorBoundCustomListProperties.class)
3132+
static class ConstructorBoundCustomListPropertiesConfiguration {
3133+
3134+
@Bean
3135+
@ConfigurationPropertiesBinding
3136+
static Converter<ArrayList<?>, CustomList<?>> arrayListToCustomList() {
3137+
return new Converter<>() {
3138+
3139+
@Override
3140+
public CustomList<?> convert(ArrayList<?> source) {
3141+
return new CustomList<>(source);
3142+
}
3143+
3144+
};
3145+
3146+
}
3147+
3148+
}
3149+
3150+
@ConfigurationProperties("test")
3151+
static class ConstructorBoundCustomListProperties {
3152+
3153+
private final CustomList<String> values;
3154+
3155+
ConstructorBoundCustomListProperties(CustomList<String> values) {
3156+
this.values = values;
3157+
}
3158+
3159+
CustomList<String> getValues() {
3160+
return this.values;
3161+
}
3162+
3163+
}
3164+
3165+
@EnableConfigurationProperties(SetterBoundCustomListProperties.class)
3166+
static class SetterBoundCustomListPropertiesConfiguration {
3167+
3168+
@Bean
3169+
@ConfigurationPropertiesBinding
3170+
static Converter<ArrayList<?>, CustomList<?>> arrayListToCustomList() {
3171+
return new Converter<>() {
3172+
3173+
@Override
3174+
public CustomList<?> convert(ArrayList<?> source) {
3175+
return new CustomList<>(source);
3176+
}
3177+
3178+
};
3179+
3180+
}
3181+
3182+
}
3183+
3184+
@ConfigurationProperties("test")
3185+
static class SetterBoundCustomListProperties {
3186+
3187+
private CustomList<String> values;
3188+
3189+
CustomList<String> getValues() {
3190+
return this.values;
3191+
}
3192+
3193+
void setValues(CustomList<String> values) {
3194+
this.values = values;
3195+
}
3196+
3197+
}
3198+
3199+
static final class CustomList<E> extends ArrayList<E> {
3200+
3201+
CustomList(List<E> delegate) {
3202+
super(delegate);
3203+
}
3204+
3205+
}
3206+
31183207
}

0 commit comments

Comments
 (0)