Skip to content

@QueryParam and OptionalInt are not working as documented #3622

Closed

Description

@muRn

Hi!

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)
public class GreetingResource {
    @GET
    public void getLastRegistered(@QueryParam("count") OptionalInt count) {
        // according to docs app should return 400 Bad Request before even stepping into the method if passed count=qwe
        if (!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

  1. added
    stale
    on Jun 26, 2021
  2. removed
    stale
    on Jun 26, 2021
  3. github-actions commented on Dec 24, 2021

    @github-actions

    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.

  4. added
    stale
    on Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions