Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

spring boot - Springdoc GroupedOpenApi not following global parameters set with OperationCustomizer

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

Actual Output

Actual Output

Expected Output

Expected Output

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

Workaround

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Edit: Answer updated with reference to @Value not providing values from application properties Spring Boot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...