Describe the bug
Issue #2969 and pull #2970 attempt to apply the same SpringDoc transformations to the SpringDoc resources under /webjars/**.
In addition, commit e3b4311 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/**.
|
registry.addResourceHandler(uiRootPath + swaggerUiWebjarsPrefix + ALL_PATTERN) |
-
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.
|
swaggerUiPrefix = webjarsPrefix; |
|
resourcePath = DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR; |
|
swaggerUiWebjarsPrefix = swaggerUiPrefix; |
-
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.
|
StringBuilder uiRootPath = new StringBuilder(); |
|
String swaggerPath = swaggerUiConfigProperties.getPath(); |
|
if (swaggerPath.contains(DEFAULT_PATH_SEPARATOR)) |
|
uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR)); |
|
if (actuatorProvider.isPresent() && actuatorProvider.get().isUseManagementPort()) |
|
uiRootPath.append(actuatorProvider.get().getBasePath()); |
|
if (ArrayUtils.isNotEmpty(sbUrls)) |
|
sbUrl = sbUrls[0]; |
|
String swaggerPath = swaggerUiConfigParameters.getPath(); |
|
if (swaggerPath.contains(DEFAULT_PATH_SEPARATOR)) |
|
sbUrl.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR)); |
|
swaggerUiConfigParameters.setUiRootPath(sbUrl.toString()); |
To Reproduce
Steps to reproduce the behavior:
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).
Describe the bug
Issue #2969 and pull #2970 attempt to apply the same SpringDoc transformations to the SpringDoc resources under
/webjars/**.In addition, commit e3b4311 modifies the mappings for SpringDoc resources in WebFlux if the
springdoc.webjars.prefixproperty 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/**.springdoc-openapi/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java
Line 119 in 4b95d0e
The
swaggerUiPrefixvalue is set to the same asswaggerUiWebjarsPrefixwhenspringdoc.webjars.prefixis not/webjars. TheswaggerUiPrefixpattern should not be affected by the WebJars prefix value.springdoc-openapi/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java
Lines 114 to 116 in 4b95d0e
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.springdoc-openapi/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java
Lines 98 to 103 in 4b95d0e
springdoc-openapi/springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java
Lines 193 to 198 in 4b95d0e
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 ISSUE-2969 fix path to register resource handler to work SwaggerIndexPageTransformer considering /webjar path prefix #2970), and it will fail.springdoc-openapi/springdoc-openapi-starter-webflux-ui/src/test/java/test/org/springdoc/ui/app34/SpringDocApp34Test.java
Line 36 in 4b95d0e
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).