Skip to content

Commit d1e1a82

Browse files
committed
Support binding to collection with EnumSet values
Fixes gh-15539
1 parent d000f3b commit d1e1a82

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> targe
4646
target.getType().asCollection().getGenerics());
4747
ResolvableType elementType = target.getType().asCollection().getGeneric();
4848
IndexedCollectionSupplier result = new IndexedCollectionSupplier(
49-
() -> CollectionFactory.createCollection(collectionType, 0));
49+
() -> CollectionFactory.createCollection(collectionType,
50+
elementType.resolve(), 0));
5051
bindIndexed(name, target, elementBinder, aggregateType, elementType, result);
5152
if (result.wasSupplied()) {
5253
return result.get();

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Collections;
21+
import java.util.EnumSet;
2122
import java.util.HashSet;
2223
import java.util.LinkedList;
2324
import java.util.List;
@@ -27,6 +28,7 @@
2728
import org.junit.Before;
2829
import org.junit.Test;
2930

31+
import org.springframework.boot.context.properties.bind.BinderTests.ExampleEnum;
3032
import org.springframework.boot.context.properties.bind.BinderTests.JavaBean;
3133
import org.springframework.boot.context.properties.source.ConfigurationProperty;
3234
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
@@ -448,6 +450,17 @@ public void bindToBeanWithExceptionInGetterForExistingValue() {
448450
assertThat(result.getValues()).containsExactly("a", "b", "c");
449451
}
450452

453+
@Test
454+
public void bindToBeanWithEnumSetCollection() {
455+
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
456+
source.put("foo.values[0]", "foo-bar,bar-baz");
457+
this.sources.add(source);
458+
BeanWithEnumsetCollection result = this.binder
459+
.bind("foo", Bindable.of(BeanWithEnumsetCollection.class)).get();
460+
assertThat(result.getValues().get(0)).containsExactly(ExampleEnum.FOO_BAR,
461+
ExampleEnum.BAR_BAZ);
462+
}
463+
451464
public static class ExampleCollectionBean {
452465

453466
private List<String> items = new ArrayList<>();
@@ -566,4 +579,18 @@ public List<String> getValues() {
566579

567580
}
568581

582+
public static class BeanWithEnumsetCollection {
583+
584+
private List<EnumSet<ExampleEnum>> values;
585+
586+
public void setValues(List<EnumSet<ExampleEnum>> values) {
587+
this.values = values;
588+
}
589+
590+
public List<EnumSet<ExampleEnum>> getValues() {
591+
return this.values;
592+
}
593+
594+
}
595+
569596
}

0 commit comments

Comments
 (0)