Skip to content

Commit cc91efe

Browse files
author
Keith Donald
committed
Fixed bugs in bean wrapper related to nesting levels on method parameters
1 parent 3536b81 commit cc91efe

File tree

2 files changed

+68
-15
lines changed
  • org.springframework.beans/src/main/java/org/springframework/beans
  • org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation

2 files changed

+68
-15
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,8 @@ else if (value instanceof Map) {
787787
Class<?> mapKeyType = GenericCollectionTypeResolver.getMapKeyReturnType(pd.getReadMethod(), i + 1);
788788
// IMPORTANT: Do not pass full property name in here - property editors
789789
// must not kick in for map keys but rather only for map values.
790-
Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType,
791-
new PropertyTypeDescriptor(mapKeyType, new MethodParameter(pd.getReadMethod(), -1), pd));
790+
TypeDescriptor typeDescriptor = mapKeyType != null ? TypeDescriptor.valueOf(mapKeyType) : TypeDescriptor.valueOf(Object.class);
791+
Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor);
792792
value = map.get(convertedMapKey);
793793
}
794794
else {
@@ -935,8 +935,10 @@ else if (propValue.getClass().isArray()) {
935935
if (isExtractOldValueForEditor()) {
936936
oldValue = Array.get(propValue, arrayIndex);
937937
}
938+
MethodParameter methodParameter = new MethodParameter(pd.getReadMethod(), -1);
939+
methodParameter.increaseNestingLevel();
938940
Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType,
939-
new PropertyTypeDescriptor(requiredType, new MethodParameter(pd.getReadMethod(), -1), pd));
941+
new PropertyTypeDescriptor(requiredType, methodParameter, pd));
940942
Array.set(propValue, arrayIndex, convertedValue);
941943
}
942944
catch (IndexOutOfBoundsException ex) {
@@ -954,8 +956,10 @@ else if (propValue instanceof List) {
954956
if (isExtractOldValueForEditor() && index < list.size()) {
955957
oldValue = list.get(index);
956958
}
959+
MethodParameter methodParameter = new MethodParameter(pd.getReadMethod(), -1);
960+
methodParameter.increaseNestingLevel();
957961
Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType,
958-
new PropertyTypeDescriptor(requiredType, new MethodParameter(pd.getReadMethod(), -1), pd));
962+
new PropertyTypeDescriptor(requiredType, methodParameter, pd));
959963
if (index < list.size()) {
960964
list.set(index, convertedValue);
961965
}
@@ -983,17 +987,19 @@ else if (propValue instanceof Map) {
983987
Map map = (Map) propValue;
984988
// IMPORTANT: Do not pass full property name in here - property editors
985989
// must not kick in for map keys but rather only for map values.
986-
Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType,
987-
new PropertyTypeDescriptor(mapKeyType, new MethodParameter(pd.getReadMethod(), -1), pd));
990+
TypeDescriptor typeDescriptor = mapKeyType != null ? TypeDescriptor.valueOf(mapKeyType) : TypeDescriptor.valueOf(Object.class);
991+
Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, typeDescriptor);
988992
Object oldValue = null;
989993
if (isExtractOldValueForEditor()) {
990994
oldValue = map.get(convertedMapKey);
991995
}
992996
// Pass full property name and old value in here, since we want full
993997
// conversion ability for map values.
998+
MethodParameter methodParameter = new MethodParameter(pd.getReadMethod(), -1);
999+
methodParameter.increaseNestingLevel();
9941000
Object convertedMapValue = convertIfNecessary(
9951001
propertyName, oldValue, pv.getValue(), mapValueType,
996-
new TypeDescriptor(new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length + 1)));
1002+
new PropertyTypeDescriptor(mapValueType, methodParameter, pd));
9971003
map.put(convertedMapKey, convertedMapValue);
9981004
}
9991005
else {

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/Spr7839Tests.java

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import java.util.ArrayList;
56
import java.util.HashMap;
67
import java.util.List;
78
import java.util.Map;
@@ -70,7 +71,6 @@ public void listElementAutogrowObject() throws Exception {
7071
}
7172

7273
@Test
73-
@Ignore
7474
public void listOfLists() throws Exception {
7575
request.setRequestURI("/nested/listOfLists");
7676
request.addParameter("nested.listOfLists[0]", "Nested1,Nested2");
@@ -91,6 +91,15 @@ public void listOfListsElementAutogrowObject() throws Exception {
9191
adapter.handle(request, response, controller);
9292
}
9393

94+
@Test
95+
@Ignore
96+
public void arrayOfLists() throws Exception {
97+
// TODO two issues here: no autogrow for array properties, and GenericCollectionResolver not capable of accessing nested element type
98+
request.setRequestURI("/nested/arrayOfLists");
99+
request.addParameter("nested.arrayOfLists[0]", "Nested1,Nested2");
100+
adapter.handle(request, response, controller);
101+
}
102+
94103
@Test
95104
@Ignore
96105
public void map() throws Exception {
@@ -99,6 +108,13 @@ public void map() throws Exception {
99108
adapter.handle(request, response, controller);
100109
}
101110

111+
@Test
112+
public void mapOfLists() throws Exception {
113+
request.setRequestURI("/nested/mapOfLists");
114+
request.addParameter("nested.mapOfLists['apples'][0]", "1");
115+
adapter.handle(request, response, controller);
116+
}
117+
102118
@Controller
103119
public static class Spr7839Controller {
104120

@@ -112,11 +128,6 @@ public void handlerList(JavaBean bean) {
112128
assertEquals("Nested2", bean.nested.list.get(1).foo);
113129
}
114130

115-
@RequestMapping("/nested/map")
116-
public void handlerMap(JavaBean bean) {
117-
assertEquals("bar", bean.nested.map.get("apple").foo);
118-
}
119-
120131
@RequestMapping("/nested/listElement")
121132
public void handlerListElement(JavaBean bean) {
122133
assertEquals("Nested", bean.nested.list.get(0).foo);
@@ -132,6 +143,22 @@ public void handlerListOfListsElement(JavaBean bean) {
132143
assertEquals("Nested", bean.nested.listOfLists.get(0).get(0).foo);
133144
}
134145

146+
@RequestMapping("/nested/arrayOfLists")
147+
public void handlerArrayOfLists(JavaBean bean) {
148+
System.out.println(bean.nested.arrayOfLists[0].get(1).getClass());
149+
assertEquals("Nested2", bean.nested.arrayOfLists[0].get(1).foo);
150+
}
151+
152+
@RequestMapping("/nested/map")
153+
public void handlerMap(JavaBean bean) {
154+
assertEquals("bar", bean.nested.map.get("apple").foo);
155+
}
156+
157+
@RequestMapping("/nested/mapOfLists")
158+
public void handlerMapOfLists(JavaBean bean) {
159+
assertEquals(new Integer(1), bean.nested.mapOfLists.get("apples").get(0));
160+
}
161+
135162
}
136163

137164
public static class JavaBean {
@@ -157,10 +184,14 @@ public static class NestedBean {
157184

158185
private List<List<NestedBean>> listOfLists;
159186

187+
private List<NestedBean>[] arrayOfLists;
188+
160189
private Map<String, NestedBean> map = new HashMap<String, NestedBean>();
161190

191+
private Map<String, List<Integer>> mapOfLists = new HashMap<String, List<Integer>>();
192+
162193
public NestedBean() {
163-
194+
mapOfLists.put("apples", new ArrayList<Integer>());
164195
}
165196

166197
public NestedBean(String foo) {
@@ -198,7 +229,23 @@ public List<List<NestedBean>> getListOfLists() {
198229
public void setListOfLists(List<List<NestedBean>> listOfLists) {
199230
this.listOfLists = listOfLists;
200231
}
201-
232+
233+
public List<NestedBean>[] getArrayOfLists() {
234+
return arrayOfLists;
235+
}
236+
237+
public void setArrayOfLists(List<NestedBean>[] arrayOfLists) {
238+
this.arrayOfLists = arrayOfLists;
239+
}
240+
241+
public Map<String, List<Integer>> getMapOfLists() {
242+
return mapOfLists;
243+
}
244+
245+
public void setMapOfLists(Map<String, List<Integer>> mapOfLists) {
246+
this.mapOfLists = mapOfLists;
247+
}
248+
202249
}
203250

204251
}

0 commit comments

Comments
 (0)