-
Notifications
You must be signed in to change notification settings - Fork 357
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
Inconsistent handling of invalid and empty strings for numeric parameters #4790
Comments
This is happening because of org.glassfish.jersey.internal.inject.ParamConverters#fromString There is special treatment for empty strings:
The execution is catch in the section There is a check for empty values:
It returns null, but when value is not empty and it is not a number it re-throws an exception. I have to check whether this is fine or not. |
It looks it was introduced in 2012 in this commit: I cannot find that issue 1220 to get more context about this change, but it looks this is the correct behavior so we can close this @joschi |
@jbescos Thanks for looking into this! As @dsvensson mentioned #4555 (comment), this behavior makes sense for strings but not for other types which have to be converted from strings to the actual type, such as numeric types or UUIDs. I stand by the statement that any implementation which will return |
@jbescos Is there a way to override the providers/ |
I think it makes sense that only applies for Strings. But it is strange that it only enters in that catch if it is not String. So, if I want to modify that behavior, I need to literally remove that part that was added in this commit javaee/jersey@7652780 I guess it was added because of one reason. I can try to remove it and run all the tests, to see what happens. |
There are some tests failing:
I focused in one that expects BigDecimals as input query parameters. When empty string is sent, it is expected to receive the request in the It looks the way it is working now it is the expected behavior. |
Should this issue have a milestone? This issue blocks version updates in Dropwizard. Vulnerability checkers therefore correctly detect existing vulnerabilities. Would assigning a milestone help with improved focus? |
Colleagues, I wonder if any work is done on it? I can probably take a look otherwise. |
I created a PR with the suggested changes, but that made some tests to fail. See my comment: #4790 (comment) It looks the current behavior is correct. You can check out my PR if you want to try: #4850 |
Thank you @jbescos, will check it out. |
Any updates on this? The mentioned PR #4850 was closed in the mean time. |
@easbar, the conclusion right now is that the current behavior is correct, because there are already tests verifying it. Unless somebody finds in the spec that it should work in other way, I don't expect changes here. |
That's a non sequitur, isn't it? The tests can also verify incorrect behavior. 😉 The Javadoc for
The last part ("Throws") means that the behavior of the current implementation is incorrect since the empty string is not a valid integer number and thus cannot be parsed and should result in the mentioned exception which would cause an HTTP error response. Instead of failing, the current implementation constructs a |
…ters eclipse-ee4j#4790 Signed-off-by: Jorge Bescos Gascon <[email protected]>
I removed that PR and I created a new one that looks better: |
@joschi
The second sends null to optional:
What makes you think that optional cannot be null? |
Why is it
The semantics of the The purpose of If Jersey doesn't guarantee that a variable of type |
I agree with what you say, Optional should not be null. I meant it differently, and I was not clear: Why do you think the query argument cannot be an empty String "", i.e. handled as null (So that the Optional is empty, but the return status would not be 404? |
@jansupol Thanks for your clarification! ❤️ You have to see this in context: The empty string is an invalid number and cannot be converted to a numeric type, so it should have the same result as the string "invalid-number". If the query parameter was not provided at all, the value should be Does that make sense? I'm just looking for consistency in the API and Jersey's implementation here. 😅 |
The thing is that the URI request and query parameters are always a text stream. so the empty string is at the same time an empty number, i.e. The other point is that for the Jersey framework general functionality it seems better to match the endpoint with null number (empty optional) than return 404. When the endpoint is reached, it can programatically react on an empty query paramater by any response, 404 included if needed. |
This is a follow-up of the discussion with @jbescos in #4651 (comment).
The handling of invalid strings and empty strings is inconsistent for numeric query parameters.
Intuitively, I would expect the strings
""
(empty string) and"not-a-number"
(invalid string when trying to parse a numeric value) to lead to the same result, but Jersey currently handles them differently.This also leads to an unidiomatic use of the
Optional<T>
container for which support was introduced just recently in Jersey 2.34.The following example (using the Jersey testing framework) illustrates the issue:
The text was updated successfully, but these errors were encountered: