【问题标题】:How I can ignore PathVariable conditionally on swagger ui using springdoc openapi如何使用 springdoc openapi 在 swagger ui 上有条件地忽略 PathVariable
【发布时间】:2020-03-21 22:11:56
【问题描述】:

我正在从 springfox 2.9.0 迁移到 springdoc-openapi-ui 1.2.33。 我需要根据条件在 swagger ui 上显示或隐藏 PathVariable。 我有如下两条路径

  1. 字符串名称标识符 = "{fisrtName}/{lastName}"

  2. String nameIdentifier = "{fisrtName}"

我正在根据要求传递上述名称标识符之一。

我对上述路径使用单个控制器,如下所示

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

}

在 springfox 中,我可以使用 docket.ignoredParameterTypes(IgnoreLastName.class) 实现它,如下所示。

@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;
    }

但在 springdoc open api 中我无法实现相同的目标。 您的帮助同样受到赞赏。 编码是在java中完成的

谢谢

【问题讨论】:

    标签: spring-boot swagger swagger-ui springdoc springdoc-openui


    【解决方案1】:

    您可以使用@Hidden swagger 注释或@Parameter(hidden = true)

    如果参数级别不能传递,可以全局设置:需要升级到springdoc-openapi-ui v1.3.0。

    static {
        SpringDocUtils.getConfig().addAnnotationsToIgnore(IgnoreLastName.class);
    }
    

    【讨论】:

    • 我们不能以编程方式将值传递给隐藏 @Parameter(hidden = true/false(基于条件))
    • 使用:SpringDocUtils.getConfig().addAnnotationsToIgnore(IgnoreLastName.class) v1.2.34
    • 嗨,@brianbro 感谢您的时间和解决方案。我已经尝试添加上述解决方案,但它没有按预期工作。 ``` 静态字符串名称标识符 = "/{fisrtName}";静态 { if (!nameIdentifier.contains("lastName")) { SpringDocUtils.getConfig().addAnnotationsToIgnore(IgnoreLastName .class); } } ``` 但它仍然在 swagger ui 的参数部分显示 lastName 作为强制参数,而不是隐藏它。我已经尝试将上面的代码添加到我的配置类中
    • 嗨@Shilanand,我已经更新到正确的版本:v1.3.0。在这个版本中,如果有正确的注释,PathVariable 也可以被忽略。例如; SpringDocUtils.getConfig().addAnnotationsToIgnore(IgnoreLastName.class)
    • 嗨,brianbro 非常感谢您的解决方案,这正是我所寻找的。投票赞成。 brianbro,如果你有发行说明链接,你能提供同样的吗?
    猜你喜欢
    • 2021-03-03
    • 2020-09-09
    • 1970-01-01
    • 2021-06-03
    • 2022-10-25
    • 2021-11-14
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多