Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 494
Debargha Roy Avatar asked Jul 08 '20 10:07

Debargha Roy


1 Answers

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

like image 160
Debargha Roy Avatar answered Nov 17 '22 05:11

Debargha Roy