You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@QueryParam and OptionalInt are not working as documented #3622
In this part of core documentation I found the following:
A @QueryParam("count")-annotated OptionalInt parameter takes the first count value from the request’s query string and passes it as a String to OptionalInt’s constructor. OptionalInt parses the string as an Integer, returning a 400 Bad Request if the value is malformed.
The most important part for me is the last sentence. I read it as "if someone calls your endpoint with something like ?count=qwe he will get 400 Bad Request because we explicitly expecting count to be an integer". I have tried it myself and found a different behavior. Instead of returning 400 Bad Request I got empty OptionalInt in the annotated method parameter.
Example code:
@Slf4j@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
publicclassGreetingResource {
@GETpublicvoidgetLastRegistered(@QueryParam("count") OptionalIntcount) {
// according to docs app should return 400 Bad Request before even stepping into the method if passed count=qweif (!count.isPresent()) {
log.error("But instead it makes count an empty OptionalInt and continues");
}
}
}
Of course, we can check for it and throw WebApplicationException with 400 Bad Request explicitly in this case but that should be mentioned in the docs, right?
First sentence of mentioned block is also strange. It says:
A @QueryParam("count")-annotated OptionalInt parameter takes the first count value from the request’s query string and passes it as a String to OptionalInt’s constructor.
In fact, OptionalInt doesn't have constructor with String parameter.
Something is definitely converting String to OptionalInt but differently.
P.S. I tried to replace method parameter type OptionalInt with just int. In this case endpoint returns 404 Not Found which is odd too. If the requestor failed composing a vaild request by passing qwe into integer field he should get 400 Bad Request, not 404 Not Found. But I guess that's a question to Jersey.
Activity
github-actions commentedon Dec 24, 2021
This issue is stale because it has been open 90 days with no activity. Remove the "stale" label or comment or this will be closed in 14 days.