When POST
ing array data to a Spring MVC annotated controller method, I get inconsistent results when I use a square-bracket notation.
The POST
data looks like this:
foo[0] = 1
foo[1] = 2
We're intentionally using the bracket notation so that we can build multi-demensional arrays (but this bug does not require multi-dimensional arrays to reproduce). The bug does not occur if you drop the brackets and just send duplicate URI parameter names.
When I bind it to a controller method that looks like the following, the resulting variable is null which is not expected:
@PostMapping
public void myController(Long[] foo) {
// foo == null
}
But when I bind it inside a more complex bean then it works as expected; the array is not null and populated with values:
class MyBean {
private Long[] foo;
}
@PostMapping
public void myController(MyBean bean) {
// bean.foo !== null && bean.foo.length == 2
}
After some digging around, I read this line in the documentation regarding how method arguments are handled:
If a method argument is not matched to any of the earlier values in this table and it is a simple type (as determined by BeanUtils#isSimpleProperty, it is a resolved as a @RequestParam. Otherwise, it is resolved as a @ModelAttribute.
- I think in the first scenario where it does not work as expected, there is an implicit
@RequestParam
annotation on the argument andRequestParamMethodArgumentResolver
fails to find the parameter values (because of the brackets) and therefore constructs a null array. - I think in the second scenario there is an implicit
@ModelAttribute
annotation, I'm assuming the code path is a little different (I'm guessing it goes throughServletModelAttributeMethodProcessor
?) and therefore it works.
Ultimately, I'm guessing the solution (if I'm understanding the codebase well enough) is to beef up RequestParamMethodArgumentResolver
to handle the bracket notation?
Comment From: snicoll
Thanks for the report. I think we'd need a small sample to be sure of what you're talking about. You can share it with us by attaching a zip to this issue or by pushing the code to a separate GitHub repository.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.