Skip to content

Commit 5f9b444

Browse files
committed
bean properties of type enum array/collection can be populated with comma-separated String (SPR-6547)
1 parent b497f6c commit 5f9b444

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

+10
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ private <T> T convertIfNecessary(String propertyName, Object oldValue, Object ne
202202

203203
// Value not of required type?
204204
if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) {
205+
if (requiredType != null && Collection.class.isAssignableFrom(requiredType) &&
206+
convertedValue instanceof String && typeDescriptor.getMethodParameter() != null) {
207+
Class elementType = GenericCollectionTypeResolver.getCollectionParameterType(typeDescriptor.getMethodParameter());
208+
if (elementType != null && Enum.class.isAssignableFrom(elementType)) {
209+
convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
210+
}
211+
}
205212
if (editor == null) {
206213
editor = findDefaultEditor(requiredType, typeDescriptor);
207214
}
@@ -214,6 +221,9 @@ private <T> T convertIfNecessary(String propertyName, Object oldValue, Object ne
214221
if (convertedValue != null) {
215222
if (requiredType.isArray()) {
216223
// Array required -> apply appropriate conversion of elements.
224+
if (convertedValue instanceof String && Enum.class.isAssignableFrom(requiredType.getComponentType())) {
225+
convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
226+
}
217227
return (T) convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType());
218228
}
219229
else if (convertedValue instanceof Collection) {

org.springframework.beans/src/test/java/org/springframework/beans/BeanWrapperEnumTests.java

+59-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,9 +17,7 @@
1717
package org.springframework.beans;
1818

1919
import static org.junit.Assert.*;
20-
2120
import org.junit.Test;
22-
2321
import test.beans.CustomEnum;
2422
import test.beans.GenericBean;
2523

@@ -53,4 +51,62 @@ public void testCustomEnumWithEmptyString() {
5351
assertEquals(null, gb.getCustomEnum());
5452
}
5553

54+
@Test
55+
public void testCustomEnumArrayWithSingleValue() {
56+
GenericBean<?> gb = new GenericBean<Object>();
57+
BeanWrapper bw = new BeanWrapperImpl(gb);
58+
bw.setPropertyValue("customEnumArray", "VALUE_1");
59+
assertEquals(1, gb.getCustomEnumArray().length);
60+
assertEquals(CustomEnum.VALUE_1, gb.getCustomEnumArray()[0]);
61+
}
62+
63+
@Test
64+
public void testCustomEnumArrayWithMultipleValues() {
65+
GenericBean<?> gb = new GenericBean<Object>();
66+
BeanWrapper bw = new BeanWrapperImpl(gb);
67+
bw.setPropertyValue("customEnumArray", new String[] {"VALUE_1", "VALUE_2"});
68+
assertEquals(2, gb.getCustomEnumArray().length);
69+
assertEquals(CustomEnum.VALUE_1, gb.getCustomEnumArray()[0]);
70+
assertEquals(CustomEnum.VALUE_2, gb.getCustomEnumArray()[1]);
71+
}
72+
73+
@Test
74+
public void testCustomEnumArrayWithMultipleValuesAsCsv() {
75+
GenericBean<?> gb = new GenericBean<Object>();
76+
BeanWrapper bw = new BeanWrapperImpl(gb);
77+
bw.setPropertyValue("customEnumArray", "VALUE_1,VALUE_2");
78+
assertEquals(2, gb.getCustomEnumArray().length);
79+
assertEquals(CustomEnum.VALUE_1, gb.getCustomEnumArray()[0]);
80+
assertEquals(CustomEnum.VALUE_2, gb.getCustomEnumArray()[1]);
81+
}
82+
83+
@Test
84+
public void testCustomEnumSetWithSingleValue() {
85+
GenericBean<?> gb = new GenericBean<Object>();
86+
BeanWrapper bw = new BeanWrapperImpl(gb);
87+
bw.setPropertyValue("customEnumSet", "VALUE_1");
88+
assertEquals(1, gb.getCustomEnumSet().size());
89+
assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1));
90+
}
91+
92+
@Test
93+
public void testCustomEnumSetWithMultipleValues() {
94+
GenericBean<?> gb = new GenericBean<Object>();
95+
BeanWrapper bw = new BeanWrapperImpl(gb);
96+
bw.setPropertyValue("customEnumSet", new String[] {"VALUE_1", "VALUE_2"});
97+
assertEquals(2, gb.getCustomEnumSet().size());
98+
assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1));
99+
assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2));
100+
}
101+
102+
@Test
103+
public void testCustomEnumSetWithMultipleValuesAsCsv() {
104+
GenericBean<?> gb = new GenericBean<Object>();
105+
BeanWrapper bw = new BeanWrapperImpl(gb);
106+
bw.setPropertyValue("customEnumSet", "VALUE_1,VALUE_2");
107+
assertEquals(2, gb.getCustomEnumSet().size());
108+
assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_1));
109+
assertTrue(gb.getCustomEnumSet().contains(CustomEnum.VALUE_2));
110+
}
111+
56112
}

org.springframework.beans/src/test/java/test/beans/GenericBean.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ public class GenericBean<T> {
6060

6161
private CustomEnum customEnum;
6262

63+
private CustomEnum[] customEnumArray;
64+
65+
private Set<CustomEnum> customEnumSet;
66+
6367
private T genericProperty;
6468

6569
private List<T> genericListProperty;
6670

67-
6871
public GenericBean() {
6972
}
7073

@@ -225,6 +228,22 @@ public void setCustomEnum(CustomEnum customEnum) {
225228
this.customEnum = customEnum;
226229
}
227230

231+
public CustomEnum[] getCustomEnumArray() {
232+
return customEnumArray;
233+
}
234+
235+
public void setCustomEnumArray(CustomEnum[] customEnum) {
236+
this.customEnumArray = customEnum;
237+
}
238+
239+
public Set<CustomEnum> getCustomEnumSet() {
240+
return customEnumSet;
241+
}
242+
243+
public void setCustomEnumSet(Set<CustomEnum> customEnumSet) {
244+
this.customEnumSet = customEnumSet;
245+
}
246+
228247
public static GenericBean createInstance(Set<Integer> integerSet) {
229248
return new GenericBean(integerSet);
230249
}

0 commit comments

Comments
 (0)