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

12,874

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
Share:
12,874
lopushen
Author by

lopushen

Software developer with 7 years of experience. I have worked in teams of various sizes, both local and distributed. Mostly worked with Agile and Scrum. Able to communicate with the customer, show demos, negotiate requirements etc. Worked on all steps of software development lifecycle: both on projects from scratch and support. Experienced in troubleshooting, including production issues. Always eager to learn and share knowledge among co-workers. Aimed to develop a high-quality product and looking for suitable opportunities. I also have experience in leading teams and participating in the business development process.

Updated on December 07, 2022

Comments

  • lopushen
    lopushen over 1 year

    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?

    • lealceldeiro
      lealceldeiro over 5 years
      Swagger version?
    • buræquete
      buræquete over 5 years
      from this sounds really stupid, isn't it? > Also remember to add required: true, because path parameters are always required.
    • lopushen
      lopushen over 5 years
      @lealceldeiro 2.7.0