6

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.

Debargha Roy
  • 2,320
  • 1
  • 15
  • 34

2 Answers2

7

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

Debargha Roy
  • 2,320
  • 1
  • 15
  • 34
0

Alternative to add and load OperationCustomizer in the case you declare yours open api groups by properties springdoc.group-configs[0].group= instead definition by Java code in a Spring Configuration GroupedOpenApi.builder().

  @Bean
  public Map<String, GroupedOpenApi> configureGroupedsOpenApi(Map<String, GroupedOpenApi> groupedsOpenApi, OperationCustomizer operationCustomizer) {
    groupedsOpenApi.forEach((id, groupedOpenApi) -> groupedOpenApi.getOperationCustomizers()
                                                                  .add(operationCustomizer));
    return groupedsOpenApi;
  }
jpep1
  • 210
  • 2
  • 8