0

I have a REST API Controller built using Quarkus RestEasyReactive

@Path("/hello")
public class HelloController {
  @GET
    @Produces({"application/json"})
    public RestResponse<Greeting> greet(@Min(0) @RestQuery int value) {
        return new Greeting("Hello" + value);
    }
}

When I send following request, http://localhost:8080/hello?value=

Quarkus returns 404 Not Found Error.

Investing Quarkus Implementation for RestEasy I found following code in ParameterHandler

if (defaultValue != null
                && (result == null || (isCollection && ((Collection) result).isEmpty()))) {
            result = defaultValue;
        }
        Throwable toThrow = null;
        if (converter != null && ((result != null) || isOptional)) {
            // spec says:
            /*
             * 3.2 Fields and Bean Properties
             * if the field or property is annotated with @MatrixParam, @QueryParam or @PathParam then an implementation
             * MUST generate an instance of NotFoundException (404 status) that wraps the thrown exception and no
             * entity; if the field or property is annotated with @HeaderParam or @CookieParam then an implementation
             * MUST generate an instance of BadRequestException (400 status) that wraps the thrown exception and
             * no entity.
             * 3.3.2 Parameters
             * Exceptions thrown during construction of @FormParam annotated parameter values are treated the same as if
             * the parameter were annotated with @HeaderParam.
             */
            switch (parameterType) {
                case COOKIE:
                case HEADER:
                case FORM:
                    try {
                        result = converter.convert(result);
                    } catch (WebApplicationException x) {
                        toThrow = x;
                    } catch (Throwable x) {
                        log.debug("Unable to handle parameter", x);
                        toThrow = new BadRequestException(x);
                    }
                    break;
                case MATRIX:
                case PATH:
                case QUERY:
                    try {
                        result = converter.convert(result);
                    } catch (WebApplicationException x) {
                        toThrow = x;
                    } catch (Throwable x) {
                        log.debug("Unable to handle parameter", x);
                        toThrow = new NotFoundException(x);
                    }
                    break;

Based on the above code, result variable is blank and the converter which is an Integer converter tries to convert blank value to integer and throws exception.

One of the workaround could be that I convert all my query parameter definition as string and type cast to Integer in the code which doesn't seem to be that efficient from design perspective.

Is there anything in Quarkus to override this behavior or is there something wrong in my understanding in the way this scenario should be handled?

zforgo
  • 2,508
  • 2
  • 14
  • 22
Prafull
  • 73
  • 4
  • I think the behaviour is correct. What happens when you add @NotNull and then make a request with an empty value ? That should result in an ConstraintViolationException. – Serkan Aug 19 '23 at 11:37
  • Try adding a `@Default` to manage missing value – Luca Basso Ricci Aug 21 '23 at 05:52
  • @LucaBassoRicci Adding a ```@Default``` doesn't work because based on the code above, the default value kicks in only when the actual value of the parameter is null – Prafull Aug 24 '23 at 16:06
  • @Serkan Adding ```@NotNull``` doesn't change the behavior. Moreover, the requirement is to have the parameter as optional. I can't make it mandatory. – Prafull Aug 24 '23 at 16:11

0 Answers0