fixed collection element conversion using ConversionService (SPR-6950)

This commit is contained in:
Juergen Hoeller 2010-03-25 11:53:41 +00:00
parent f836601e0f
commit d50881d82b
3 changed files with 63 additions and 7 deletions

View File

@ -572,8 +572,7 @@ class TypeConverterDelegate {
if (methodParam != null) { if (methodParam != null) {
methodParam.increaseNestingLevel(); methodParam.increaseNestingLevel();
} }
Object convertedElement = Object convertedElement = convertIfNecessary(indexedPropertyName, null, element, elementType);
convertIfNecessary(indexedPropertyName, null, element, elementType, typeDescriptor);
if (methodParam != null) { if (methodParam != null) {
methodParam.decreaseNestingLevel(); methodParam.decreaseNestingLevel();
} }
@ -652,11 +651,11 @@ class TypeConverterDelegate {
methodParam.increaseNestingLevel(); methodParam.increaseNestingLevel();
methodParam.setTypeIndexForCurrentLevel(0); methodParam.setTypeIndexForCurrentLevel(0);
} }
Object convertedKey = convertIfNecessary(keyedPropertyName, null, key, keyType, typeDescriptor); Object convertedKey = convertIfNecessary(keyedPropertyName, null, key, keyType);
if (methodParam != null) { if (methodParam != null) {
methodParam.setTypeIndexForCurrentLevel(1); methodParam.setTypeIndexForCurrentLevel(1);
} }
Object convertedValue = convertIfNecessary(keyedPropertyName, null, value, valueType, typeDescriptor); Object convertedValue = convertIfNecessary(keyedPropertyName, null, value, valueType);
if (methodParam != null) { if (methodParam != null) {
methodParam.decreaseNestingLevel(); methodParam.decreaseNestingLevel();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,8 @@ public class GenericBean<T> {
private Set<Integer> integerSet; private Set<Integer> integerSet;
private Set<ITestBean> testBeanSet;
private List<Resource> resourceList; private List<Resource> resourceList;
private List<List<Integer>> listOfLists; private List<List<Integer>> listOfLists;
@ -105,6 +107,14 @@ public class GenericBean<T> {
this.integerSet = integerSet; this.integerSet = integerSet;
} }
public Set<ITestBean> getTestBeanSet() {
return testBeanSet;
}
public void setTestBeanSet(Set<ITestBean> testBeanSet) {
this.testBeanSet = testBeanSet;
}
public List<Resource> getResourceList() { public List<Resource> getResourceList() {
return resourceList; return resourceList;
} }

View File

@ -57,6 +57,7 @@ import org.springframework.aop.interceptor.SimpleTraceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.DerivedTestBean; import org.springframework.beans.DerivedTestBean;
import org.springframework.beans.GenericBean;
import org.springframework.beans.ITestBean; import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -66,6 +67,8 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.annotation.AnnotationConfigUtils; import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpInputMessage;
@ -104,6 +107,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.bind.support.WebArgumentResolver; import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.bind.support.WebBindingInitializer; import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
@ -259,6 +263,33 @@ public class ServletAnnotationControllerTests {
assertEquals("foo-bar-/myApp", response.getContentAsString()); assertEquals("foo-bar-/myApp", response.getContentAsString());
} }
@Test
public void typeNestedSetBinding() throws Exception {
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(NestedSetController.class));
RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
csDef.getPropertyValues().add("converters", new TestBeanConverter());
RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
wbiDef.getPropertyValues().add("conversionService", csDef);
RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
request.addParameter("testBeanSet", new String[] {"1", "2"});
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertEquals("[1, 2]-org.springframework.beans.TestBean", response.getContentAsString());
}
@Test @Test
public void methodNotAllowed() throws Exception { public void methodNotAllowed() throws Exception {
initServlet(MethodNotAllowedController.class); initServlet(MethodNotAllowedController.class);
@ -1666,6 +1697,7 @@ public class ServletAnnotationControllerTests {
public String post(@ModelAttribute("object1") Object object1) { public String post(@ModelAttribute("object1") Object object1) {
//do something with object1 //do something with object1
return "myPage"; return "myPage";
} }
} }
@ -2172,6 +2204,23 @@ public class ServletAnnotationControllerTests {
} }
} }
@Controller
public static class NestedSetController {
@RequestMapping("/myPath.do")
public void myHandle(GenericBean gb, HttpServletResponse response) throws Exception {
response.getWriter().write(gb.getTestBeanSet().toString() + "-" +
gb.getTestBeanSet().iterator().next().getClass().getName());
}
}
public static class TestBeanConverter implements Converter<String, ITestBean> {
public ITestBean convert(String source) {
return new TestBean(source);
}
}
@Controller @Controller
public static class MethodNotAllowedController { public static class MethodNotAllowedController {
@ -2568,8 +2617,6 @@ public class ServletAnnotationControllerTests {
public void handle(@RequestParam("map") Map map, Writer writer) throws IOException { public void handle(@RequestParam("map") Map map, Writer writer) throws IOException {
writer.write("test-" + map); writer.write("test-" + map);
} }
} }
public static class CustomMapEditor extends PropertyEditorSupport { public static class CustomMapEditor extends PropertyEditorSupport {