From d1e1a82b325dd2a98c3ebbb2dccaef6f87bfc594 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 2 Jan 2019 10:52:31 -0800 Subject: [PATCH] Support binding to collection with EnumSet values Fixes gh-15539 --- .../properties/bind/CollectionBinder.java | 3 ++- .../bind/CollectionBinderTests.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java index 4213abd7da..99d0582a26 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/CollectionBinder.java @@ -46,7 +46,8 @@ class CollectionBinder extends IndexedElementsBinder> { 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(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java index 2bd86d5ea2..8e03fecfdd 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java @@ -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 items = new ArrayList<>(); @@ -566,4 +579,18 @@ public class CollectionBinderTests { } + public static class BeanWithEnumsetCollection { + + private List> values; + + public void setValues(List> values) { + this.values = values; + } + + public List> getValues() { + return this.values; + } + + } + }