springdoc-openapi icon indicating copy to clipboard operation
springdoc-openapi copied to clipboard

Unable to set global parameters

Open zhangpan-soft opened this issue 8 months ago • 3 comments

Describe the bug

I want to set a global parameter, but it seems that it doesn't work well

The official document is as follows image

But this option is not available in my API

image

I tried to set it in Components similar to the Security Scheme, but it didn't take effect

How should I set up my global header?

I used Spring Security, and the login address of Spring Security is implicit and cannot be displayed in Apidoc. How can I add the login API to Apidoc?

zhangpan-soft avatar Dec 08 '23 03:12 zhangpan-soft

I have same issue. Below is my sample codes, but it does not take effect, which means I can't see the configured headers on swagger-ui.

@Bean
    fun openAPI(): OpenAPI {
        return OpenAPI()
            .components(
                Components()
                    .addHeaders(
                        "myHeader2",
                        Header().description("myHeader2 header").schema(StringSchema())
                    )

            )
            .info(
                Info().title("Test API").version("0.0.1")
                    .description("Test api!!!")
                    .termsOfService("http://swagger.io/terms/")
            )
    }

ryukato avatar Jan 03 '24 09:01 ryukato

@zhangpan-soft could you try below codes? it looks working. But I am not sure why the way you used does not work.

 @Bean
    fun globalOpenApiCustomizer(): GlobalOpenApiCustomizer {
        val customHeaderParameter = Parameter()
        customHeaderParameter.`in`("header").name("custom header")
      
        return GlobalOpenApiCustomizer { openApi ->
            openApi.paths.values.forEach { pathItem ->
                pathItem.readOperations().forEach { op ->
                    op.addParametersItem(customHeaderParameter)
                }
            }
        }
    }

ryukato avatar Jan 03 '24 10:01 ryukato

The above code works for me. Thank you, @ryukato.

In case someone needs the Java version:

@Bean
public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
    Parameter customHeaderParameter = new Parameter().in("header").name("custom header");
    return openApi -> {
        openApi.getPaths().values().forEach(pathItem -> {
            pathItem.readOperations().forEach(op -> {
                op.addParametersItem(customHeaderParameter);
            });
        });
    };
}

SeiSilver avatar Feb 23 '24 09:02 SeiSilver