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

Swagger UI resources are incorrectly mapped when using WebJar prefix or Swagger root path

Open jamesmissen opened this issue 1 month ago • 1 comments

Describe the bug

Issue #2969 and pull #2970 attempt to apply the same SpringDoc transformations to the SpringDoc resources under /webjars/**.

In addition, commit https://github.com/springdoc/springdoc-openapi/commit/e3b4311a8611cd8a20fc8b9ffd0d013810f1bcb6 modifies the mappings for SpringDoc resources in WebFlux if the springdoc.webjars.prefix property is altered.

However, these changes have multiple issues:

  • The uiRootPath is erroneously added to the front of the pattern. E.g. if the Swagger path is /documentation/spring-doc.html, then the WebJar resources will be at /documentation/webjars/** instead of /webjars/**.

    https://github.com/springdoc/springdoc-openapi/blob/4b95d0e658359156bcaf1d15a892ef718c776cef/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java#L119

  • The swaggerUiPrefix value is set to the same as swaggerUiWebjarsPrefix when springdoc.webjars.prefix is not /webjars. The swaggerUiPrefix pattern should not be affected by the WebJars prefix value.

    https://github.com/springdoc/springdoc-openapi/blob/4b95d0e658359156bcaf1d15a892ef718c776cef/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java#L114-L116

  • The springdoc.webjars.prefix property is unnecessary and confusing. It is only currently used for WebFlux and not WebMvc, when the behaviour of WebJars is the same for both.

    It also doesn't make sense to have, as the mappings for WebJar resources on the classpath are already configured using Spring config properties:

    • spring.web.resources.add-mappings control whether the WebJar resources are actually exposed.
    • spring.mvc.webjars-path-pattern and spring.webflux.webjars-path-pattern set the pattern to expose WebJar resources (the default is /webjars/**) - hence another property for the same value is not needed.

    WebFlux reference: https://github.com/spring-projects/spring-boot/blob/3d8c4390915edcd0c279de3b742f5260983a6fa7/module/spring-boot-webflux/src/main/java/org/springframework/boot/webflux/autoconfigure/WebFluxAutoConfiguration.java#L234-L247

    WebMvc reference: https://github.com/spring-projects/spring-boot/blob/3d8c4390915edcd0c279de3b742f5260983a6fa7/module/spring-boot-webmvc/src/main/java/org/springframework/boot/webmvc/autoconfigure/WebMvcAutoConfiguration.java#L352-L357

  • The uiRootPath for the resource handler patterns is computed incorrectly. For the resource mappings the actuator base path is added last, instead of first. For the consumers (i.e. the subclasses of AbstractSwaggerWelcome), the actuator base path is added first.

    E.g. if the Swagger path is /documentation/spring-doc.html, and the actuator base path is /actuator, then SwaggerWelcomeCommon will redirect to /actuator/documentation/swagger-ui/index.html as expected, but the index resource will be actually be exposed at /documentation/actuator/swagger-ui/index.html.

    https://github.com/springdoc/springdoc-openapi/blob/4b95d0e658359156bcaf1d15a892ef718c776cef/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java#L98-L103

    https://github.com/springdoc/springdoc-openapi/blob/4b95d0e658359156bcaf1d15a892ef718c776cef/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java#L193-L198

To Reproduce Steps to reproduce the behavior:

  • Clone the latest version (i.e. 3.0.0-RC1).

  • Add the property "springdoc.swagger-ui.path=/documentation/swagger-ui.html" to the springdoc-openapi-starter-webflux-ui test app34 (from pull #2970), and it will fail.

    https://github.com/springdoc/springdoc-openapi/blob/4b95d0e658359156bcaf1d15a892ef718c776cef/springdoc-openapi-starter-webflux-ui/src/test/java/test/org/springdoc/ui/app34/SpringDocApp34Test.java#L36

Expected behavior

  • The springdoc.webjars.prefix should not affect the normal (non-WebJar) mappings to the Swagger UI resources.

  • The behaviour of the WebJars prefix should be the same for WebFlux and WebMvc, as both map WebJar resources to /webjars/** by default (i.e. it's not specific to WebFlux only).

  • The properties spring.mvc.webjars-path-pattern and spring.webflux.webjars-path-pattern should be used instead of springdoc.webjars.prefix.

  • The /webjars/swagger-ui/** mapping should not be added if resource mappings at /webjars/** have been disabled (i.e. if spring.web.resources.add-mappings is set to false).

jamesmissen avatar Nov 18 '25 14:11 jamesmissen

I have created PR #3146 to fix the aforementioned issues.

jamesmissen avatar Nov 18 '25 14:11 jamesmissen