Search code examples
javaswagger-uiopenapispringfox

PathVariable as optional in swagger SpringFox(3.0.0)


I have upgraded to the latest version of SpringFox(3.0.0) recently.
PathVariable marked as required = false is showing as mandatory.

Below is my controller method code

@GetMapping(path = { "/persons", "/persons/{id} })
public List<Person> getPerson(@PathVariable(required = false) String id) {
    
}

I have tried adding @ApiParam, by default it is false. But still, on swagger it is showing as mandatory.

Previously with SpringFox(2.9.0) it was working fine, on swagger it was marked as optional

Any help in this will be appreciated.
Thank you


Solution

  • Path parameters are always required. If we are having an optional path variable then we need to define the two separate end poinds.

    I have added two endpoints to solve my problem as shown below.

    @GetMapping(path = { "/persons })
    public List<Person> getAllPerson() {
        
    }
    
    @GetMapping(path = {"/persons/{id} })
    public List<Person> getPersonById(@PathVariable String id) {
        
    }