2

I am migrating from springfox 2.9.0 to springdoc-openapi-ui 1.2.33. I have a requirement to show or hide the PathVariable on swagger ui based on condition. I have two paths like below

  1. String nameIdentifier = "{fisrtName}/{lastName}"

  2. String nameIdentifier = "{fisrtName}"

I am passing one of the above nameIdentifier based on the requirement.

I am using a single controller for the above paths as shown below

@GetMapping(path = "persons/${nameIdentifier}/display")
public List<Person> getPersons(@PathVariable String fisrtName,
    @IgnoreLastName @PathVariable Optional<String> lastName) {

}

In springfox I was able to achieve it using docket.ignoredParameterTypes(IgnoreLastName.class) as shown below.

@Bean
public Docket api() {

    Docket docket;

    docket = new Docket(DocumentationType.SWAGGER_2).select()                
     .apis(RequestHandlerSelectors.basePackage("go.controller")).paths(PathSelectors.any()).build()
                .apiInfo(apiInfo());

        if (!nameIdentifier.contains("lastName")) {
            docket.ignoredParameterTypes(IgnoreLastName.class);
        }
        return docket;
    }

But in springdoc open api I am unable to achieve the same. Your help appreciated in the same. Coding is done in java

Thanks

SSK
  • 3,444
  • 6
  • 32
  • 59

1 Answers1

5

You can use @Hidden swagger annotations or @Parameter(hidden = true).

If you can not pass on parameter level, you can set it globally: You will need to upgrade to v1.3.0 of springdoc-openapi-ui.

static {
    SpringDocUtils.getConfig().addAnnotationsToIgnore(IgnoreLastName.class);
}
brianbro
  • 4,141
  • 2
  • 24
  • 37
  • We can not pass value to hidden programatically **@Parameter(hidden = true/false(based on condition))** – SSK Mar 15 '20 at 04:22
  • Use: SpringDocUtils.getConfig().addAnnotationsToIgnore(IgnoreLastName.class) with v1.2.34 – brianbro Mar 17 '20 at 23:36
  • Hi, @brianbro thanks for your time and solution. I have tried adding the above solution but it is not working as expected. ``` static String nameIdentifier = "/{fisrtName}"; static { if (!nameIdentifier.contains("lastName")) { SpringDocUtils.getConfig().addAnnotationsToIgnore(IgnoreLastName .class); } } ``` but still, it showing lastName in Parameters section on swagger ui as mandatory parameter and not hiding it. I have tried adding above code to my configuration class – SSK Mar 18 '20 at 05:40
  • Hi @Shilanand, I have updated to the right version: v1.3.0. With this version PathVariable can also be ignored if they have the right annotation. For example; SpringDocUtils.getConfig().addAnnotationsToIgnore(IgnoreLastName.class) – brianbro Mar 21 '20 at 14:14
  • Hi, brianbro Thank you very much for your solution and this is what I was looking for. vote up. brianbro if you have release notes link can you provide the same. – SSK Mar 23 '20 at 03:13
  • Found the release notes link https://github.com/springdoc/springdoc-openapi/releases – SSK Jun 11 '20 at 13:39