Springdoc GroupedOpenApi 不遵循使用 OperationCustomizer 设置的全局参数 [英] Springdoc GroupedOpenApi not following global parameters set with OperationCustomizer

查看:41
本文介绍了Springdoc GroupedOpenApi 不遵循使用 OperationCustomizer 设置的全局参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 GroupedOpenApi 定义 API 组时,参数列表中不存在添加到每个端点的通用参数集.下面是各自的代码

When using GroupedOpenApi to define an API group, the common set of parameters that are added to every endpoint is not present in the parameters list. Below are the respective codes

@Bean
public GroupedOpenApi v1Apis() {
    return GroupedOpenApi.builder().group("v1 APIs")
            // hide all v2 APIs
            .pathsToExclude("/api/v2/**", "/v2/**")
            // show all v1 APIs
            .pathsToMatch("/api/v1/**", "/v1/**")
            .build();
}

以及将标准头添加到所有端点的类

And the class to add the Standard Headers to all the endpoints

@Component
public class GlobalHeaderAdder implements OperationCustomizer {
    @Override
    public Operation customize(Operation operation, HandlerMethod handlerMethod) {
        operation.addParametersItem(new Parameter().$ref("#/components/parameters/ClientID"));
        operation.addSecurityItem(new SecurityRequirement().addList("Authorization"));
        List<Parameter> parameterList = operation.getParameters();
        if (parameterList!=null && !parameterList.isEmpty()) {
            Collections.rotate(parameterList, 1);
        }
        return operation;
    }
}

实际输出

预期输出

要重现该问题,请克隆 https://github.com/debargharoy/springdoc-test

To reproduce the issue, clone https://github.com/debargharoy/springdoc-test

解决方法

在应用程序属性文件中添加要包含/排除的路径解决了错误.但是代码级别的东西将不胜感激.

Adding the paths to be included/excluded in the application properties file solves the error. But something at the code level will be much appreciated.

推荐答案

在构建 Api Group 时附加所需的 OperationCustomizer 对象.

Attach the required OperationCustomizerobject while building the Api Group.

@Bean
public GroupedOpenApi v1Apis(GlobalHeaderAdder globalHeaderAdder) {
    return GroupedOpenApi.builder().group("v1 APIs")
            // hide all v2 APIs
            .pathsToExclude("/api/v2/**", "/v2/**")
            // show all v1 APIs
            .pathsToMatch("/api/v1/**", "/v1/**")
            .addOperationCustomizer(globalHeaderAdded) 
            .build();
}

参考 @Value 不提供应用程序属性 Spring Boot 中的值

这篇关于Springdoc GroupedOpenApi 不遵循使用 OperationCustomizer 设置的全局参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆