-
Notifications
You must be signed in to change notification settings - Fork 38.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request @RequestParam not enforced with empty values [SPR-10402] #15035
Comments
Phil Webb commented I believe that this is by design. The The
Rossen Stoyanchev or Arjen Poutsma can probably confirm this. |
Michael Osipov commented Phil, how can this be by design? My request param is required thus no value has been provided by |
Phil Webb commented Hi Micheal, Exceptions are raised only when the parameter is missing. In your case the parameter is present and the value is treated as an empty string:
This will not throw a
This will throw a This is probably not the most intuitive behavior and the javadoc could be improved but unfortunately I don't see an easy way to change the current design without breaking existing applications that may depend on this behavior. |
Michael Osipov commented Unfortunately, I do not consider an empty value a valid value. I have reported a similar issue already #14813 which was fixed in 3.2.1. I think this one fall under the same edge case. Never tested. Do you really think that people really care whether the value is not provided or an empty string? I don't think so. |
Rossen Stoyanchev commented Obviously in your case you don't care about empty vs null values. That's not necessarily the case for everyone. The key here is that the "" is passed through type conversion, which by default turns it to null. We don't want to take away the option to treat "" as something else. However, if the value is still null after type conversion and the argument is marked as required, we can treat it as a missing parameter at that point. |
Michael Osipov commented Rossen, that sounds fair enough. Looking forward to 3.2.3. Thanks. |
Rossen Stoyanchev commented This is now resolved so that if the request parameter is "" and following type conversion it is null, it is treated as a missing value. Note however, that the ConversionService leaves empty strings alone by default. So you must choose to treat them the same null by registering a converter or property editor that does that. See for example StringTrimmerEditor and also #12201 is relevant. |
Alfred Staflinger commented Today, I have downloaded Spring Framework 3.2.3. Now, handleMissingValue is called, even if the IMHO, this new behaviour is suboptimal. We have many cases, where an empty string parameter value (which has been converted to null by the property editor) should not be handled as missing parameter (even if required = true)! And I guess that many others who relied on the prior behaviour will be confused, too. |
Rossen Stoyanchev commented Apologies for the regression.
That was not intended and can be fixed.
I can see that semantically an empty string satisfies required=true. However, if then converted to null effectively you don't require it. At least from the controller method's perspective, the logic must accommodate the possibility of null. The distinction that there was a "" is gone. So just trying to understand what is the reason for doing this? |
Rossen Stoyanchev commented I have opened #15207 and am closing this one since 3.2.3 is already out. |
Rossen Stoyanchev commented As much as I dislike doing so, I'm afraid I'll need to roll this change back as it introduced a regression that is not acceptable. Michael Osipov, that means you'll probably need to find another way to treat empty values as invalid input. You could do so by registering a PropertyEditor or Converter that rejects empty values by throwing an exception. |
Michael Osipov commented Rossen, fine. Can we postpone this to 4.x with a refined logic to fill gaps like these? I will evaluate the |
Michael Osipov commented Unfortunately, situation remains as-is. I deploy a workaround in my controller: // Workaround for https://jira.springsource.org/browse/SPR-10402
if (StringUtils.isEmpty(query))
throw new MissingServletRequestParameterException("q", "String");
if (ArrayUtils.isEmpty(attributes))
throw new MissingServletRequestParameterException("attributes", "String[]"); |
Stefan Gheorghiu commented This bug is still there in 3.2.3.RELEASE. Rolling back to 3.2.2 helps. Please fix it in 3.2.4. |
Rossen Stoyanchev commented I'm guessing you're referring to the bug as described in #15213? It was fixed in 3.2.4. |
Stefan Gheorghiu commented Exactly. I just used a wrong browser tab. Sorry for that. |
Michael Osipov opened SPR-10402 and commented
I have defined this method signature:
When I invoke this method either with:
or
The query variable is always passed as empty string/null although its defined as required.
MissingServletRequestParameterException
is not thrown.I have attached a screenshot of the debugging session where one can inspect that case. I think the else if in lines 95 to 97 are incomplete.
Affects: 3.2.2
Attachments:
Issue Links:
Referenced from: commits 2d8315f, e39fe18, abfb439, d3eda09
0 votes, 7 watchers
The text was updated successfully, but these errors were encountered: