Search code examples
swaggerspringdoc

Setting swagger config parameters in SpringDoc


I am trying to override Swagger configuration in SpringDoc as described in:

https://springdoc.org/#swagger-ui-properties

I am setting these in the code in (kotlin class) init block

init {
    System.setProperty("springdoc.swagger-ui.path", "/services/$serviceName")
    System.setProperty("springdoc.swagger-ui.url", "/services/$serviceName/v3/api-docs")
    System.setProperty("springdoc.swagger-ui.configUrl", "/services/$serviceName/v3/api-docs/swagger-config")
    // Controls the display of extensions (pattern, maxLength, minLength, maximum, minimum) fields and values for Parameters.
    System.setProperty("springdoc.swagger-ui.showCommonExtensions", "true")
}

However it seems to be completely ignored and none of the fields are taken into account. What is the place these should be set correctly?

Note that I need to set the config properties according to the SERVICE_NAME env var, so I cannot use the static properties file.

Full config here: https://gist.github.com/knyttl/852f67f1688ea6e808b8eb89068e90d1


Solution

  • As suggested by the SpringDoc author in https://github.com/springdoc/springdoc-openapi/issues/1485, this can be done as follows:

        @Bean
        open fun swaggerUiConfig(config: SwaggerUiConfigProperties): SwaggerUiConfigProperties {
            // For details, see https://springdoc.org/#swagger-ui-properties
            // Controls the display of extensions (pattern, maxLength, minLength, maximum, minimum) fields and values for Parameters.
            config.showCommonExtensions = true
            // Allows to configure source for the documentation via query params (?url=/v3/api-docs).
            config.queryConfigEnabled = true
            return config
        }