Search code examples
javaspringswagger

Swagger @ApiParam annotation makes parameter annotated with @PathVariable non-required in Swagger UI


I have the following code, that is the art of the API of my

    import org.springframework.web.bind.annotation.PathVariable;
    import io.swagger.annotations.*;

    @GetMapping(value = "/{userId}")
        public String getUserHistory(ApiParam(value = "The user id") 
                    @PathVariable("userId") Integer userId, Model model) throws NotFoundException {
            // fetch user info
            model.addAttribute("userInfo", userInfo);
            return "userHistory";
        }

If I have the @ApiParam annotation, the @PathVariable becomes non-required, so if I do not enter the userId and make the request through Swagger UI, the request still goes to the server, which causes unneeded server errors. The parameter "required" of @PathVariable is true by default (so, the default is @PathVariable(name="userId", required = true)) and works fine without @ApiParam on that very parameter. These annotations should not change each other's behaviour, as far as I am concerned. Is that a Swagger bug or just a misuse?


Solution

  • The parameter "required" of @ApiParam is false by default so you just have to change that to true for it to be required through the Swagger UI.

    @ApiParam(value = "The user id", required = true) @PathVariable("userId") Integer userId