简体   繁体   中英

How to change swagger-ui endpoint that generates configuration (/v3/api-docs/swagger-config)

I have a service that creates a swagger-ui endpoint. I want this endpoint to be hosted on a path different than default (due to Kubernetes ingress rules). This is easily achieved on springdoc swagger by using

springdoc.swagger-ui.path=/myPath/swagger-ui/index.html

However it is trying to access configuration from a default url

/v3/api-docs/swagger-config

Unfortunately I also need this url to be on specific path. I know there is a setting to specify a path to look for swagger-config, namely:

springdoc.swagger-ui.configUrl=/myPath/v3/api-docs/swagger-config

However this isn't what I'm looking for. This setting lets you specify a different source of configuration and then you need to create a resource on specified path or the resource will not be found. If I understand correctly, the default path /v3/api-docs/swagger-config is some kind of endpoint that is automatically creates/generates the resource without requiring user to create it.

What I am looking for is a way to have access to this automatically generated config on a different path. Something that would say "Generate and return me your default configuration if I access /myPath/v3/api-docs/swagger-config instead of /v3/api-docs/swagger-config". Preferably by entry in application.properties or overriding some behavior in application code

Does anyone know how this can be achieved?

Looks like a default configuration URL cannot be configured by the user using available properties - it is strictly hard-coded as the constant - https://github.com/springdoc/springdoc-openapi/blob/e995192d9edd24d2335490bc97d72fea7bcea86a/springdoc-openapi-common/src/main/java/org/springdoc/core/Constants.java#L41

However, you can try to overcome this. I see two options here

  • implement a controller that will redirect from the default URL to your own.
  • debug SpringDoc beans to find a bean where the default URL can be customized and override it.

I suppose I had a similar problem in the past and it was also related to Kubernetes and a proxy (API Gateway), which is in front of my application. I needed to have url like this https://host/api-gw/my-app/swagger-ui.html and then redirection to https://host/api-gw/my-app/swagger-ui/index.html?configUrl=/api-gw/my-app/v3/api-docs/swagger-config . The redirection didn't work properly for me, because the /api-gw/ part was missing in configUrl parameter. The solution to my problem was adding ForwardedHeaderFilter ( https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter/ForwardedHeaderFilter.html ) bean to the configuration.

@Configuration
public class ForwardedHeaderFilterConfig {

  @Bean
  ForwardedHeaderFilter forwardedHeaderFilter() {
    return new ForwardedHeaderFilter();
  }
}

After that, redirection worked fine and the configUrl parameter was set to api-gw/my-app/v3/api-docs/swagger-config .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM