Allow binding=false on @ModelAttribute

Issue: SPR-13402
This commit is contained in:
Rossen Stoyanchev
2016-01-26 21:12:21 -05:00
parent 806e79b14b
commit 2e7470b27f
8 changed files with 164 additions and 10 deletions

View File

@@ -62,6 +62,7 @@ public class ModelAttributeMethodProcessorTests {
private MethodParameter paramErrors;
private MethodParameter paramInt;
private MethodParameter paramModelAttr;
private MethodParameter paramBindingDisabledAttr;
private MethodParameter paramNonSimpleType;
private MethodParameter returnParamNamedModelAttr;
@@ -75,13 +76,15 @@ public class ModelAttributeMethodProcessorTests {
this.processor = new ModelAttributeMethodProcessor(false);
Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute",
TestBean.class, Errors.class, int.class, TestBean.class, TestBean.class);
TestBean.class, Errors.class, int.class, TestBean.class,
TestBean.class, TestBean.class);
this.paramNamedValidModelAttr = new SynthesizingMethodParameter(method, 0);
this.paramErrors = new SynthesizingMethodParameter(method, 1);
this.paramInt = new SynthesizingMethodParameter(method, 2);
this.paramModelAttr = new SynthesizingMethodParameter(method, 3);
this.paramNonSimpleType = new SynthesizingMethodParameter(method, 4);
this.paramBindingDisabledAttr = new SynthesizingMethodParameter(method, 4);
this.paramNonSimpleType = new SynthesizingMethodParameter(method, 5);
method = getClass().getDeclaredMethod("annotatedReturnValue");
this.returnParamNamedModelAttr = new MethodParameter(method, -1);
@@ -167,6 +170,41 @@ public class ModelAttributeMethodProcessorTests {
assertTrue(dataBinder.isValidateInvoked());
}
@Test
public void resolveArgumentBindingDisabledPreviously() throws Exception {
String name = "attrName";
Object target = new TestBean();
this.container.addAttribute(name, target);
// Declare binding disabled (e.g. via @ModelAttribute method)
this.container.setBindingDisabled(name);
StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
given(factory.createBinder(this.request, target, name)).willReturn(dataBinder);
this.processor.resolveArgument(this.paramNamedValidModelAttr, this.container, this.request, factory);
assertFalse(dataBinder.isBindInvoked());
assertTrue(dataBinder.isValidateInvoked());
}
@Test
public void resolveArgumentBindingDisabled() throws Exception {
String name = "noBindAttr";
Object target = new TestBean();
this.container.addAttribute(name, target);
StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
given(factory.createBinder(this.request, target, name)).willReturn(dataBinder);
this.processor.resolveArgument(this.paramBindingDisabledAttr, this.container, this.request, factory);
assertFalse(dataBinder.isBindInvoked());
assertTrue(dataBinder.isValidateInvoked());
}
@Test(expected = BindException.class)
public void resolveArgumentBindException() throws Exception {
String name = "testBean";
@@ -281,6 +319,7 @@ public class ModelAttributeMethodProcessorTests {
Errors errors,
int intArg,
@ModelAttribute TestBean defaultNameAttr,
@ModelAttribute(name="noBindAttr", binding=false) @Valid TestBean noBindAttr,
TestBean notAnnotatedAttr) {
}
}

View File

@@ -115,6 +115,30 @@ public class ModelFactoryTests {
assertNull(this.mavContainer.getModel().get("name"));
}
@Test
public void modelAttributeWithBindingDisabled() throws Exception {
ModelFactory modelFactory = createModelFactory("modelAttrWithBindingDisabled");
HandlerMethod handlerMethod = createHandlerMethod("handle");
modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod);
assertTrue(this.mavContainer.containsAttribute("foo"));
assertTrue(this.mavContainer.isBindingDisabled("foo"));
}
@Test
public void modelAttributeFromSessionWithBindingDisabled() throws Exception {
Foo foo = new Foo();
this.attributeStore.storeAttribute(this.webRequest, "foo", foo);
ModelFactory modelFactory = createModelFactory("modelAttrWithBindingDisabled");
HandlerMethod handlerMethod = createHandlerMethod("handle");
modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod);
assertTrue(this.mavContainer.containsAttribute("foo"));
assertSame(foo, this.mavContainer.getModel().get("foo"));
assertTrue(this.mavContainer.isBindingDisabled("foo"));
}
@Test
public void sessionAttribute() throws Exception {
this.attributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue");
@@ -250,7 +274,7 @@ public class ModelFactoryTests {
}
@SessionAttributes("sessionAttr") @SuppressWarnings("unused")
@SessionAttributes({"sessionAttr", "foo"}) @SuppressWarnings("unused")
private static class TestController {
@ModelAttribute
@@ -273,6 +297,11 @@ public class ModelFactoryTests {
return null;
}
@ModelAttribute(name="foo", binding=false)
public Foo modelAttrWithBindingDisabled() {
return new Foo();
}
public void handle() {
}
@@ -280,4 +309,7 @@ public class ModelFactoryTests {
}
}
private static class Foo {
}
}