Support binding to collection with EnumSet values

Fixes gh-15539
This commit is contained in:
Madhura Bhave 2019-01-02 10:52:31 -08:00
parent d000f3bcd0
commit d1e1a82b32
2 changed files with 29 additions and 1 deletions

View File

@ -46,7 +46,8 @@ class CollectionBinder extends IndexedElementsBinder<Collection<Object>> {
target.getType().asCollection().getGenerics());
ResolvableType elementType = target.getType().asCollection().getGeneric();
IndexedCollectionSupplier result = new IndexedCollectionSupplier(
() -> CollectionFactory.createCollection(collectionType, 0));
() -> CollectionFactory.createCollection(collectionType,
elementType.resolve(), 0));
bindIndexed(name, target, elementBinder, aggregateType, elementType, result);
if (result.wasSupplied()) {
return result.get();

View File

@ -18,6 +18,7 @@ package org.springframework.boot.context.properties.bind;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@ -27,6 +28,7 @@ import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.context.properties.bind.BinderTests.ExampleEnum;
import org.springframework.boot.context.properties.bind.BinderTests.JavaBean;
import org.springframework.boot.context.properties.source.ConfigurationProperty;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
@ -448,6 +450,17 @@ public class CollectionBinderTests {
assertThat(result.getValues()).containsExactly("a", "b", "c");
}
@Test
public void bindToBeanWithEnumSetCollection() {
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
source.put("foo.values[0]", "foo-bar,bar-baz");
this.sources.add(source);
BeanWithEnumsetCollection result = this.binder
.bind("foo", Bindable.of(BeanWithEnumsetCollection.class)).get();
assertThat(result.getValues().get(0)).containsExactly(ExampleEnum.FOO_BAR,
ExampleEnum.BAR_BAZ);
}
public static class ExampleCollectionBean {
private List<String> items = new ArrayList<>();
@ -566,4 +579,18 @@ public class CollectionBinderTests {
}
public static class BeanWithEnumsetCollection {
private List<EnumSet<ExampleEnum>> values;
public void setValues(List<EnumSet<ExampleEnum>> values) {
this.values = values;
}
public List<EnumSet<ExampleEnum>> getValues() {
return this.values;
}
}
}