Swagger UI resources are incorrectly mapped when using WebJar prefix or Swagger root path
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
uiRootPathis 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
swaggerUiPrefixvalue is set to the same asswaggerUiWebjarsPrefixwhenspringdoc.webjars.prefixis not/webjars. TheswaggerUiPrefixpattern 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.prefixproperty 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-mappingscontrol whether the WebJar resources are actually exposed.spring.mvc.webjars-path-patternandspring.webflux.webjars-path-patternset 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
uiRootPathfor 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 ofAbstractSwaggerWelcome), 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, thenSwaggerWelcomeCommonwill redirect to/actuator/documentation/swagger-ui/index.htmlas 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 thespringdoc-openapi-starter-webflux-uitestapp34(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.prefixshould 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-patternandspring.webflux.webjars-path-patternshould be used instead ofspringdoc.webjars.prefix. -
The
/webjars/swagger-ui/**mapping should not be added if resource mappings at/webjars/**have been disabled (i.e. ifspring.web.resources.add-mappingsis set tofalse).
I have created PR #3146 to fix the aforementioned issues.