Springdoc-openapi: Global headers aren't appearing in the Swagger UI

Created on 3 Mar 2020  路  2Comments  路  Source: springdoc/springdoc-openapi

Describe the bug
Following the steps in the FAQ and here to add a global header. The header isn't added to every endpoint.

To Reproduce

  • Spring boot version: 2.2.2.RELEASE
  • Springdoc OpenApi UI version: 1.2.32

Enter the following somewhere Spring Boot will pick it up:

@Bean
public OpenAPI openAPI() {
    return new OpenAPI()
            .components(new Components()
                    .addSecuritySchemes("spring_oauth", new SecurityScheme()
                            .type(SecurityScheme.Type.OAUTH2)
                            .description("Oauth2 flow")
                            .flows(new OAuthFlows()
                                    .authorizationCode(new OAuthFlow()
                                            .authorizationUrl(authUrl + "/auth")
                                            .refreshUrl(authUrl + "/token")
                                            .tokenUrl(authUrl + "/token")
                                            .scopes(new Scopes())
                                    )))
                    .addSecuritySchemes("api_key", new SecurityScheme()
                            .type(SecurityScheme.Type.APIKEY)
                            .description("Api Key access")
                            .in(SecurityScheme.In.HEADER)
                            .name("API-KEY")
                    )
                    .addHeaders("Version", new Header()
                            .description("version header")
                            .required(true)
                            .schema(new StringSchema())))
            .security(Arrays.asList(
                    new SecurityRequirement().addList("spring_oauth"),
                    new SecurityRequirement().addList("api_key")));
}

Expected behavior
Header Version should appear in the Swagger UI under every endpoint, but it does not.

Additional context
I could be misunderstanding the FAQ, if so I apologize.

Most helpful comment

Hi @keith-miller,

Your first part of configuration is correct.
If you want your Header Version to appear in the Swagger UI under every endpoint, you still need to add:

@Bean
public OpenApiCustomiser customerGlobalHeaderOpenApiCustomiser() {
    return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream())
            .forEach(operation -> operation.addParametersItem(new HeaderParameter().$ref("#/components/headers/Version")));
}

You have also a sample Test here:

All 2 comments

Hi @keith-miller,

Your first part of configuration is correct.
If you want your Header Version to appear in the Swagger UI under every endpoint, you still need to add:

@Bean
public OpenApiCustomiser customerGlobalHeaderOpenApiCustomiser() {
    return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream())
            .forEach(operation -> operation.addParametersItem(new HeaderParameter().$ref("#/components/headers/Version")));
}

You have also a sample Test here:

Thanks!

Was this page helpful?
0 / 5 - 0 ratings