Fix issue with resolving Errors controller argument
The ErrorsMethodArgumentResolver expects the preceding @ModelAttribute in the controller method signature to be the last one added in the model -- an assumption that can break if a model attribute is added earlier (e.g. through a @ModelAttribute method) and more attributes are added as well. This fix ensures when an @ModelAttribute is resolved as a controller method argument it has the highest index in the model. Issue: SPR-9378
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,6 +27,7 @@ import org.springframework.beans.TestBean;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
@@ -46,13 +47,13 @@ import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link ModelAttributeMethodProcessor}.
|
||||
*
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class ModelAttributeMethodProcessorTests {
|
||||
|
||||
private ModelAttributeMethodProcessor processor;
|
||||
|
||||
|
||||
private MethodParameter paramNamedValidModelAttr;
|
||||
|
||||
private MethodParameter paramErrors;
|
||||
@@ -62,31 +63,31 @@ public class ModelAttributeMethodProcessorTests {
|
||||
private MethodParameter paramModelAttr;
|
||||
|
||||
private MethodParameter paramNonSimpleType;
|
||||
|
||||
|
||||
private MethodParameter returnParamNamedModelAttr;
|
||||
|
||||
|
||||
private MethodParameter returnParamNonSimpleType;
|
||||
|
||||
private ModelAndViewContainer mavContainer;
|
||||
|
||||
private NativeWebRequest webRequest;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
processor = new ModelAttributeMethodProcessor(false);
|
||||
|
||||
Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute",
|
||||
Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute",
|
||||
TestBean.class, Errors.class, int.class, TestBean.class, TestBean.class);
|
||||
|
||||
|
||||
paramNamedValidModelAttr = new MethodParameter(method, 0);
|
||||
paramErrors = new MethodParameter(method, 1);
|
||||
paramInt = new MethodParameter(method, 2);
|
||||
paramModelAttr = new MethodParameter(method, 3);
|
||||
paramNonSimpleType = new MethodParameter(method, 4);
|
||||
|
||||
|
||||
returnParamNamedModelAttr = new MethodParameter(getClass().getDeclaredMethod("annotatedReturnValue"), -1);
|
||||
returnParamNonSimpleType = new MethodParameter(getClass().getDeclaredMethod("notAnnotatedReturnValue"), -1);
|
||||
|
||||
|
||||
mavContainer = new ModelAndViewContainer();
|
||||
|
||||
webRequest = new ServletWebRequest(new MockHttpServletRequest());
|
||||
@@ -97,16 +98,16 @@ public class ModelAttributeMethodProcessorTests {
|
||||
// Only @ModelAttribute arguments
|
||||
assertTrue(processor.supportsParameter(paramNamedValidModelAttr));
|
||||
assertTrue(processor.supportsParameter(paramModelAttr));
|
||||
|
||||
|
||||
assertFalse(processor.supportsParameter(paramErrors));
|
||||
assertFalse(processor.supportsParameter(paramInt));
|
||||
assertFalse(processor.supportsParameter(paramNonSimpleType));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void supportedParametersInDefaultResolutionMode() throws Exception {
|
||||
processor = new ModelAttributeMethodProcessor(true);
|
||||
|
||||
|
||||
// Only non-simple types, even if not annotated
|
||||
assertTrue(processor.supportsParameter(paramNamedValidModelAttr));
|
||||
assertTrue(processor.supportsParameter(paramErrors));
|
||||
@@ -115,21 +116,21 @@ public class ModelAttributeMethodProcessorTests {
|
||||
|
||||
assertFalse(processor.supportsParameter(paramInt));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void supportedReturnTypes() throws Exception {
|
||||
processor = new ModelAttributeMethodProcessor(false);
|
||||
assertTrue(processor.supportsReturnType(returnParamNamedModelAttr));
|
||||
assertFalse(processor.supportsReturnType(returnParamNonSimpleType));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void supportedReturnTypesInDefaultResolutionMode() throws Exception {
|
||||
processor = new ModelAttributeMethodProcessor(true);
|
||||
assertTrue(processor.supportsReturnType(returnParamNamedModelAttr));
|
||||
assertTrue(processor.supportsReturnType(returnParamNonSimpleType));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void bindExceptionRequired() throws Exception {
|
||||
assertTrue(processor.isBindExceptionRequired(null, paramNonSimpleType));
|
||||
@@ -141,13 +142,13 @@ public class ModelAttributeMethodProcessorTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAttributeFromModel() throws Exception {
|
||||
testGetAttributeFromModel("attrName", paramNamedValidModelAttr);
|
||||
testGetAttributeFromModel("testBean", paramModelAttr);
|
||||
testGetAttributeFromModel("testBean", paramNonSimpleType);
|
||||
public void resovleArgumentFromModel() throws Exception {
|
||||
getAttributeFromModel("attrName", paramNamedValidModelAttr);
|
||||
getAttributeFromModel("testBean", paramModelAttr);
|
||||
getAttributeFromModel("testBean", paramNonSimpleType);
|
||||
}
|
||||
|
||||
private void testGetAttributeFromModel(String expectedAttributeName, MethodParameter param) throws Exception {
|
||||
private void getAttributeFromModel(String expectedAttributeName, MethodParameter param) throws Exception {
|
||||
Object target = new TestBean();
|
||||
mavContainer.addAttribute(expectedAttributeName, target);
|
||||
|
||||
@@ -155,56 +156,83 @@ public class ModelAttributeMethodProcessorTests {
|
||||
WebDataBinderFactory factory = createMock(WebDataBinderFactory.class);
|
||||
expect(factory.createBinder(webRequest, target, expectedAttributeName)).andReturn(dataBinder);
|
||||
replay(factory);
|
||||
|
||||
|
||||
processor.resolveArgument(param, mavContainer, webRequest, factory);
|
||||
|
||||
|
||||
verify(factory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAttribute() throws Exception {
|
||||
public void resovleArgumentViaDefaultConstructor() throws Exception {
|
||||
WebDataBinder dataBinder = new WebRequestDataBinder(null);
|
||||
|
||||
WebDataBinderFactory factory = createMock(WebDataBinderFactory.class);
|
||||
expect(factory.createBinder((NativeWebRequest) anyObject(), notNull(), eq("attrName"))).andReturn(dataBinder);
|
||||
replay(factory);
|
||||
|
||||
|
||||
processor.resolveArgument(paramNamedValidModelAttr, mavContainer, webRequest, factory);
|
||||
|
||||
|
||||
verify(factory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void automaticValidation() throws Exception {
|
||||
public void resovleArgumentValidation() throws Exception {
|
||||
String name = "attrName";
|
||||
Object target = new TestBean();
|
||||
mavContainer.addAttribute("attrName", target);
|
||||
|
||||
StubRequestDataBinder dataBinder = new StubRequestDataBinder(target);
|
||||
mavContainer.addAttribute(name, target);
|
||||
|
||||
StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
|
||||
WebDataBinderFactory binderFactory = createMock(WebDataBinderFactory.class);
|
||||
expect(binderFactory.createBinder(webRequest, target, "attrName")).andReturn(dataBinder);
|
||||
expect(binderFactory.createBinder(webRequest, target, name)).andReturn(dataBinder);
|
||||
replay(binderFactory);
|
||||
|
||||
|
||||
processor.resolveArgument(paramNamedValidModelAttr, mavContainer, webRequest, binderFactory);
|
||||
|
||||
assertTrue(dataBinder.isBindInvoked());
|
||||
assertTrue(dataBinder.isValidateInvoked());
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=BindException.class)
|
||||
public void bindException() throws Exception {
|
||||
public void resovleArgumentBindException() throws Exception {
|
||||
String name = "testBean";
|
||||
Object target = new TestBean();
|
||||
mavContainer.getModel().addAttribute(target);
|
||||
|
||||
StubRequestDataBinder dataBinder = new StubRequestDataBinder(target);
|
||||
StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
|
||||
dataBinder.getBindingResult().reject("error");
|
||||
|
||||
WebDataBinderFactory binderFactory = createMock(WebDataBinderFactory.class);
|
||||
expect(binderFactory.createBinder(webRequest, target, "testBean")).andReturn(dataBinder);
|
||||
expect(binderFactory.createBinder(webRequest, target, name)).andReturn(dataBinder);
|
||||
replay(binderFactory);
|
||||
|
||||
|
||||
processor.resolveArgument(paramNonSimpleType, mavContainer, webRequest, binderFactory);
|
||||
}
|
||||
|
||||
|
||||
// SPR-9378
|
||||
|
||||
@Test
|
||||
public void resolveArgumentOrdering() throws Exception {
|
||||
String name = "testBean";
|
||||
Object testBean = new TestBean(name);
|
||||
mavContainer.addAttribute(name, testBean);
|
||||
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, testBean);
|
||||
|
||||
Object anotherTestBean = new TestBean();
|
||||
mavContainer.addAttribute("anotherTestBean", anotherTestBean);
|
||||
|
||||
StubRequestDataBinder dataBinder = new StubRequestDataBinder(testBean, name);
|
||||
WebDataBinderFactory binderFactory = createMock(WebDataBinderFactory.class);
|
||||
expect(binderFactory.createBinder(webRequest, testBean, name)).andReturn(dataBinder);
|
||||
replay(binderFactory);
|
||||
|
||||
processor.resolveArgument(paramModelAttr, mavContainer, webRequest, binderFactory);
|
||||
|
||||
assertSame("Resolved attribute should be updated to be last in the order",
|
||||
testBean, mavContainer.getModel().values().toArray()[1]);
|
||||
assertSame("BindingResult of resolved attribute should be last in the order",
|
||||
dataBinder.getBindingResult(), mavContainer.getModel().values().toArray()[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleAnnotatedReturnValue() throws Exception {
|
||||
processor.handleReturnValue("expected", returnParamNamedModelAttr, mavContainer, webRequest);
|
||||
@@ -215,18 +243,18 @@ public class ModelAttributeMethodProcessorTests {
|
||||
public void handleNotAnnotatedReturnValue() throws Exception {
|
||||
TestBean testBean = new TestBean("expected");
|
||||
processor.handleReturnValue(testBean, returnParamNonSimpleType, mavContainer, webRequest);
|
||||
|
||||
|
||||
assertSame(testBean, mavContainer.getModel().get("testBean"));
|
||||
}
|
||||
|
||||
|
||||
private static class StubRequestDataBinder extends WebRequestDataBinder {
|
||||
|
||||
|
||||
private boolean bindInvoked;
|
||||
|
||||
|
||||
private boolean validateInvoked;
|
||||
|
||||
public StubRequestDataBinder(Object target) {
|
||||
super(target);
|
||||
public StubRequestDataBinder(Object target, String objectName) {
|
||||
super(target, objectName);
|
||||
}
|
||||
|
||||
public boolean isBindInvoked() {
|
||||
@@ -248,6 +276,8 @@ public class ModelAttributeMethodProcessorTests {
|
||||
public void validate(Object... validationHints) {
|
||||
validateInvoked = true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Target({ METHOD, FIELD, CONSTRUCTOR, PARAMETER })
|
||||
@@ -258,7 +288,7 @@ public class ModelAttributeMethodProcessorTests {
|
||||
@SessionAttributes(types=TestBean.class)
|
||||
private static class ModelAttributeHandler {
|
||||
@SuppressWarnings("unused")
|
||||
public void modelAttribute(@ModelAttribute("attrName") @Valid TestBean annotatedAttr,
|
||||
public void modelAttribute(@ModelAttribute("attrName") @Valid TestBean annotatedAttr,
|
||||
Errors errors,
|
||||
int intArg,
|
||||
@ModelAttribute TestBean defaultNameAttr,
|
||||
@@ -276,5 +306,5 @@ public class ModelAttributeMethodProcessorTests {
|
||||
private TestBean notAnnotatedReturnValue() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user