Merge rather than add URI vars to data binding values
As of Spring 3.1 URI variables can be used for data binding purposes in
addition to request parameters (including query string and form params)
In some cases URI variables and request params can overlap (e.g. form
contains a child entity with an entityId as hidden form input while the
URI contains the entityId of the parent entity) and that can lead to
surprises if the application already exists.
This change ensures that request parameters are used first and URI
vars are added only if they don't overlap. Ideally however an
application should not use the same uri variable name as the name of
a request parameter where they don't refer to the same value.
Issue: SPR-9349
Backport-Issue: SPR-9432
Backport-Commit: 4027b38903
This commit is contained in:
@@ -37,19 +37,19 @@ import org.springframework.web.servlet.HandlerMapping;
|
||||
public class ExtendedServletRequestDataBinderTests {
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.request = new MockHttpServletRequest();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void createBinder() throws Exception {
|
||||
Map<String, String> uriTemplateVars = new HashMap<String, String>();
|
||||
uriTemplateVars.put("name", "nameValue");
|
||||
uriTemplateVars.put("age", "25");
|
||||
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
|
||||
|
||||
|
||||
TestBean target = new TestBean();
|
||||
WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
|
||||
((ServletRequestDataBinder) binder).bind(request);
|
||||
@@ -61,18 +61,18 @@ public class ExtendedServletRequestDataBinderTests {
|
||||
@Test
|
||||
public void uriTemplateVarAndRequestParam() throws Exception {
|
||||
request.addParameter("age", "35");
|
||||
|
||||
|
||||
Map<String, String> uriTemplateVars = new HashMap<String, String>();
|
||||
uriTemplateVars.put("name", "nameValue");
|
||||
uriTemplateVars.put("age", "25");
|
||||
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
|
||||
|
||||
|
||||
TestBean target = new TestBean();
|
||||
WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
|
||||
((ServletRequestDataBinder) binder).bind(request);
|
||||
|
||||
assertEquals("nameValue", target.getName());
|
||||
assertEquals(25, target.getAge());
|
||||
assertEquals(35, target.getAge());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user