Skip to content

Commit 18af209

Browse files
committed
Binding to a Map with EnumSet values fails with "Cannot create EnumSet for unknown element type" spring-projects#15539
1 parent 41a7195 commit 18af209

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-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
@@ -30,6 +30,7 @@
3030
*
3131
* @author Phillip Webb
3232
* @author Madhura Bhave
33+
* @author Nishant Raut
3334
*/
3435
class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
3536

@@ -46,7 +47,7 @@ protected Object bindAggregate(ConfigurationPropertyName name, Bindable<?> targe
4647
target.getType().asCollection().getGenerics());
4748
ResolvableType elementType = target.getType().asCollection().getGeneric();
4849
IndexedCollectionSupplier result = new IndexedCollectionSupplier(
49-
() -> CollectionFactory.createCollection(collectionType, 0));
50+
() -> CollectionFactory.createCollection(collectionType, elementType.getClass(), 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: 30 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;
@@ -43,6 +44,7 @@
4344
*
4445
* @author Phillip Webb
4546
* @author Madhura Bhave
47+
* @author Nishant Raut
4648
*/
4749
public class CollectionBinderTests {
4850

@@ -447,6 +449,16 @@ public void bindToBeanWithExceptionInGetterForExistingValue() {
447449
.bind("foo", Bindable.of(BeanWithGetterException.class)).get();
448450
assertThat(result.getValues()).containsExactly("a", "b", "c");
449451
}
452+
453+
@Test
454+
public void bindToBeanWithEnumSet() {
455+
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
456+
source.put("foo.values", "A,B");
457+
this.sources.add(source);
458+
Bindable<EnumSetBean> target = Bindable.of(EnumSetBean.class);
459+
EnumSetBean bean = this.binder.bind("foo", target).get();
460+
assertThat(bean.getValues()).containsExactly(ExampleEnum.A, ExampleEnum.B);
461+
}
450462

451463
public static class ExampleCollectionBean {
452464

@@ -566,4 +578,22 @@ public List<String> getValues() {
566578

567579
}
568580

581+
public static class EnumSetBean {
582+
583+
private EnumSet<ExampleEnum> values;
584+
585+
public void setValues(EnumSet<ExampleEnum> values) {
586+
this.values = values;
587+
}
588+
589+
public EnumSet<ExampleEnum> getValues() {
590+
return EnumSet.copyOf(this.values);
591+
}
592+
593+
}
594+
595+
public static enum ExampleEnum {
596+
A, B
597+
}
598+
569599
}

0 commit comments

Comments
 (0)