Skip to content

Commit a25472a

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-37940
2 parents 865203f + 817debb commit a25472a

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
@@ -1161,6 +1161,19 @@ void loadWhenPotentiallyConstructorBoundPropertiesAreImportedUsesJavaBeanBinding
11611161
assertThat(properties.getProp()).isEqualTo("alpha");
11621162
}
11631163

1164+
@Test
1165+
void loadWhenBindingToConstructorParametersWithConversionToCustomListImplementation() {
1166+
load(ConstructorBoundCustomListPropertiesConfiguration.class, "test.values=a,b");
1167+
assertThat(this.context.getBean(ConstructorBoundCustomListProperties.class).getValues()).containsExactly("a",
1168+
"b");
1169+
}
1170+
1171+
@Test
1172+
void loadWhenBindingToJavaBeanWithConversionToCustomListImplementation() {
1173+
load(SetterBoundCustomListPropertiesConfiguration.class, "test.values=a,b");
1174+
assertThat(this.context.getBean(SetterBoundCustomListProperties.class).getValues()).containsExactly("a", "b");
1175+
}
1176+
11641177
private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
11651178
return load(new Class<?>[] { configuration }, inlinedProperties);
11661179
}
@@ -3043,4 +3056,80 @@ void setProp(String prop) {
30433056

30443057
}
30453058

3059+
@EnableConfigurationProperties(ConstructorBoundCustomListProperties.class)
3060+
static class ConstructorBoundCustomListPropertiesConfiguration {
3061+
3062+
@Bean
3063+
@ConfigurationPropertiesBinding
3064+
static Converter<ArrayList<?>, CustomList<?>> arrayListToCustomList() {
3065+
return new Converter<ArrayList<?>, CustomList<?>>() {
3066+
3067+
@Override
3068+
public CustomList<?> convert(ArrayList<?> source) {
3069+
return new CustomList<>(source);
3070+
}
3071+
3072+
};
3073+
3074+
}
3075+
3076+
}
3077+
3078+
@ConfigurationProperties("test")
3079+
static class ConstructorBoundCustomListProperties {
3080+
3081+
private final CustomList<String> values;
3082+
3083+
ConstructorBoundCustomListProperties(CustomList<String> values) {
3084+
this.values = values;
3085+
}
3086+
3087+
CustomList<String> getValues() {
3088+
return this.values;
3089+
}
3090+
3091+
}
3092+
3093+
@EnableConfigurationProperties(SetterBoundCustomListProperties.class)
3094+
static class SetterBoundCustomListPropertiesConfiguration {
3095+
3096+
@Bean
3097+
@ConfigurationPropertiesBinding
3098+
static Converter<ArrayList<?>, CustomList<?>> arrayListToCustomList() {
3099+
return new Converter<ArrayList<?>, CustomList<?>>() {
3100+
3101+
@Override
3102+
public CustomList<?> convert(ArrayList<?> source) {
3103+
return new CustomList<>(source);
3104+
}
3105+
3106+
};
3107+
3108+
}
3109+
3110+
}
3111+
3112+
@ConfigurationProperties("test")
3113+
static class SetterBoundCustomListProperties {
3114+
3115+
private CustomList<String> values;
3116+
3117+
CustomList<String> getValues() {
3118+
return this.values;
3119+
}
3120+
3121+
void setValues(CustomList<String> values) {
3122+
this.values = values;
3123+
}
3124+
3125+
}
3126+
3127+
static final class CustomList<E> extends ArrayList<E> {
3128+
3129+
CustomList(List<E> delegate) {
3130+
super(delegate);
3131+
}
3132+
3133+
}
3134+
30463135
}

0 commit comments

Comments
 (0)