3

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

SSK
  • 3,444
  • 6
  • 32
  • 59
  • 3
    In OpenAPI, path parameters are [always required](https://stackoverflow.com/q/35011192/113116). To have a path with an optional param parameter, you need to define 2 paths - with and without that parameter. – Helen Jul 27 '20 at 18:46
  • @Helen I wrote 'GetMapping(path = {"/descriptive","/{periodName}/descriptive"})' but still have same problem! – Omid Ashouri Aug 02 '20 at 10:35
  • @OmidAshouri, in my case it works with your code structure. I have in my controller: ```@GetMapping(path = {"/{mailId}", ""})``` ```ResponseEntity> getMails(@PathVariable(required = false) String mailId);``` with SpringFox 3.0.1. Then SwaggerUI shows 2 endpoints, one with PathVariable, one without it. – mindOf_L Dec 05 '20 at 23:22

1 Answers1

4

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) {
    
}
SSK
  • 3,444
  • 6
  • 32
  • 59