Search code examples
spring-bootswaggerswagger-uispringdocspringdoc-openui

How I can ignore PathVariable conditionally on swagger ui using springdoc openapi


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


Solution

  • 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);
    }