From 3a15bcd65a531b12359dcbe6c98fb0cb47ebcea5 Mon Sep 17 00:00:00 2001 From: Abdelmoez GUETAT Date: Tue, 12 Dec 2023 16:18:39 +0100 Subject: [PATCH] fix swagger webjars prefix path --- .../org/springdoc/core/utils/Constants.java | 2 +- .../core/providers/SpringWebFluxProvider.java | 20 +++---- .../springdoc/webflux/ui/SwaggerConfig.java | 11 ++-- .../webflux/ui/SwaggerWebFluxConfigurer.java | 18 +++--- .../webflux/ui/SwaggerWelcomeActuator.java | 49 +++++------------ .../webflux/ui/SwaggerWelcomeCommon.java | 55 +++++++++++++------ .../webflux/ui/SwaggerWelcomeWebFlux.java | 19 ++----- .../core/providers/SpringWebMvcProvider.java | 20 +++---- .../springdoc/webmvc/ui/SwaggerConfig.java | 10 ++-- .../webmvc/ui/SwaggerWebMvcConfigurer.java | 29 +++++----- .../webmvc/ui/SwaggerWelcomeWebMvc.java | 7 ++- 11 files changed, 119 insertions(+), 121 deletions(-) diff --git a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java index e8dbf962e..5c808ec39 100644 --- a/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java +++ b/springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java @@ -175,7 +175,7 @@ public final class Constants { /** * The constant DEFAULT_WEB_JARS_PREFIX_URL. */ - public static final String DEFAULT_WEB_JARS_PREFIX_URL = "/webjars"; + public static final String DEFAULT_WEB_JARS_PREFIX_URL = "/webjars/swagger-ui/5.18.2"; /** * The constant CLASSPATH_RESOURCE_LOCATION. diff --git a/springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/core/providers/SpringWebFluxProvider.java b/springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/core/providers/SpringWebFluxProvider.java index 2c2bd5bd1..536f4169b 100644 --- a/springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/core/providers/SpringWebFluxProvider.java +++ b/springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/core/providers/SpringWebFluxProvider.java @@ -25,17 +25,10 @@ */ package org.springdoc.webflux.core.providers; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.stream.Collectors; - import org.apache.commons.lang3.StringUtils; import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.providers.SpringWebProvider; - +import org.springdoc.core.utils.Constants; import org.springframework.util.CollectionUtils; import org.springframework.web.method.HandlerMethod; import org.springframework.web.reactive.result.condition.PatternsRequestCondition; @@ -44,6 +37,13 @@ import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.util.pattern.PathPattern; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + /** * The type Spring webflux provider. @@ -66,8 +66,8 @@ public String findPathPrefix(SpringDocConfigProperties springDocConfigProperties Set patterns = getActivePatterns(requestMappingInfo); if (!CollectionUtils.isEmpty(patterns)) { for (String operationPath : patterns) { - if (operationPath.endsWith(springDocConfigProperties.getApiDocs().getPath())) - return operationPath.replace(springDocConfigProperties.getApiDocs().getPath(), StringUtils.EMPTY); + if (operationPath.endsWith(Constants.DEFAULT_API_DOCS_URL)) + return operationPath.replace(Constants.DEFAULT_API_DOCS_URL, StringUtils.EMPTY); } } } diff --git a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerConfig.java b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerConfig.java index cbabe6642..4d74abbf1 100644 --- a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerConfig.java +++ b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerConfig.java @@ -26,8 +26,6 @@ package org.springdoc.webflux.ui; -import java.util.Optional; - import org.springdoc.core.configuration.SpringDocConfiguration; import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.properties.SwaggerUiConfigProperties; @@ -36,7 +34,6 @@ import org.springdoc.core.providers.ObjectMapperProvider; import org.springdoc.core.providers.SpringWebProvider; import org.springdoc.webflux.core.providers.SpringWebFluxProvider; - import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; import org.springframework.boot.actuate.autoconfigure.web.server.ConditionalOnManagementPort; import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType; @@ -54,6 +51,8 @@ import org.springframework.context.annotation.Lazy; import org.springframework.web.reactive.config.WebFluxConfigurer; +import java.util.Optional; + import static org.springdoc.core.utils.Constants.SPRINGDOC_SWAGGER_UI_ENABLED; import static org.springdoc.core.utils.Constants.SPRINGDOC_USE_MANAGEMENT_PORT; import static org.springdoc.core.utils.Constants.SPRINGDOC_USE_ROOT_PATH; @@ -129,9 +128,9 @@ SwaggerUiHome swaggerUiHome(Optional optionalWebFluxPropertie @ConditionalOnMissingBean @Lazy(false) SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties, - SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer, + SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer, Optional actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) { - return new SwaggerWebFluxConfigurer(swaggerUiConfigProperties,springDocConfigProperties, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver); + return new SwaggerWebFluxConfigurer(swaggerUiConfigProperties, springDocConfigProperties, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver); } /** @@ -207,7 +206,7 @@ static class SwaggerActuatorWelcomeConfiguration { @Lazy(false) SwaggerWelcomeActuator swaggerActuatorWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, WebEndpointProperties webEndpointProperties, ManagementServerProperties managementServerProperties) { - return new SwaggerWelcomeActuator(swaggerUiConfig, springDocConfigProperties, webEndpointProperties, managementServerProperties); + return new SwaggerWelcomeActuator(swaggerUiConfig, springDocConfigProperties, webEndpointProperties); } } } diff --git a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java index eefa9f798..67779ca3a 100644 --- a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java +++ b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java @@ -21,23 +21,22 @@ * * * * * * * * * - * + * */ package org.springdoc.webflux.ui; -import java.util.Optional; - import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.properties.SwaggerUiConfigProperties; import org.springdoc.core.providers.ActuatorProvider; - import org.springframework.web.reactive.config.ResourceHandlerRegistry; import org.springframework.web.reactive.config.WebFluxConfigurer; +import java.util.Optional; + import static org.springdoc.core.utils.Constants.ALL_PATTERN; import static org.springdoc.core.utils.Constants.CLASSPATH_RESOURCE_LOCATION; -import static org.springdoc.core.utils.Constants.DEFAULT_WEB_JARS_PREFIX_URL; +import static org.springdoc.core.utils.Constants.SWAGGER_UI_PREFIX; import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR; /** @@ -82,8 +81,8 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer { * @param swaggerResourceResolver the swagger resource resolver */ public SwaggerWebFluxConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties, - SpringDocConfigProperties springDocConfigProperties, - SwaggerIndexTransformer swaggerIndexTransformer, + SpringDocConfigProperties springDocConfigProperties, + SwaggerIndexTransformer swaggerIndexTransformer, Optional actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) { this.swaggerIndexTransformer = swaggerIndexTransformer; this.actuatorProvider = actuatorProvider; @@ -100,8 +99,9 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) { uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR)); if (actuatorProvider.isPresent() && actuatorProvider.get().isUseManagementPort()) uiRootPath.append(actuatorProvider.get().getBasePath()); - registry.addResourceHandler(uiRootPath + springDocConfigProperties.getWebjars().getPrefix() + ALL_PATTERN) - .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR) + + registry.addResourceHandler(uiRootPath + SWAGGER_UI_PREFIX + ALL_PATTERN) + .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + springDocConfigProperties.getWebjars().getPrefix() + DEFAULT_PATH_SEPARATOR) .resourceChain(false) .addResolver(swaggerResourceResolver) .addTransformer(swaggerIndexTransformer); diff --git a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeActuator.java b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeActuator.java index e3f71dcd3..aeee37fea 100644 --- a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeActuator.java +++ b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeActuator.java @@ -26,24 +26,20 @@ package org.springdoc.webflux.ui; -import java.util.Map; - import io.swagger.v3.oas.annotations.Operation; -import org.apache.commons.lang3.StringUtils; import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.properties.SwaggerUiConfigParameters; import org.springdoc.core.properties.SwaggerUiConfigProperties; -import reactor.core.publisher.Mono; - import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; -import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties; import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoint; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.util.UriComponentsBuilder; +import reactor.core.publisher.Mono; + +import java.util.Map; import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_ACTUATOR_URL; import static org.springdoc.core.utils.Constants.DEFAULT_SWAGGER_UI_ACTUATOR_PATH; @@ -68,26 +64,18 @@ public class SwaggerWelcomeActuator extends SwaggerWelcomeCommon { */ private final WebEndpointProperties webEndpointProperties; - /** - * The Management server properties. - */ - private final ManagementServerProperties managementServerProperties; - /** * Instantiates a new Swagger welcome. * - * @param swaggerUiConfig the swagger ui config - * @param springDocConfigProperties the spring doc config properties - * @param webEndpointProperties the web endpoint properties - * @param managementServerProperties the management server properties + * @param swaggerUiConfig the swagger ui config + * @param springDocConfigProperties the swagger ui config parameters + * @param webEndpointProperties the web endpoint properties */ public SwaggerWelcomeActuator(SwaggerUiConfigProperties swaggerUiConfig , SpringDocConfigProperties springDocConfigProperties, - WebEndpointProperties webEndpointProperties, - ManagementServerProperties managementServerProperties) { + WebEndpointProperties webEndpointProperties) { super(swaggerUiConfig, springDocConfigProperties); this.webEndpointProperties = webEndpointProperties; - this.managementServerProperties = managementServerProperties; } /** @@ -104,39 +92,30 @@ public Mono redirectToUi(ServerHttpRequest request, ServerHttpResponse res return super.redirectToUi(request, response); } - /** - * Gets swagger ui config. + * Openapi yaml map. * * @param request the request - * @return the swagger ui config + * @return the map */ @Operation(hidden = true) @GetMapping(value = SWAGGER_CONFIG_ACTUATOR_URL, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody @Override - public Map getSwaggerUiConfig(ServerHttpRequest request) { - return super.getSwaggerUiConfig(request); + public Map openapiJson(ServerHttpRequest request) { + return super.openapiJson(request); } @Override - protected void calculateUiRootPath(SwaggerUiConfigParameters swaggerUiConfigParameters,StringBuilder... sbUrls) { + protected void calculateUiRootPath(SwaggerUiConfigParameters swaggerUiConfigParameters, StringBuilder... sbUrls) { StringBuilder sbUrl = new StringBuilder(); sbUrl.append(webEndpointProperties.getBasePath()); - calculateUiRootCommon(swaggerUiConfigParameters,sbUrl, sbUrls); - } - - @Override - protected void calculateOauth2RedirectUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder) { - if (StringUtils.isBlank(swaggerUiConfig.getOauth2RedirectUrl()) || !swaggerUiConfigParameters.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) { - UriComponentsBuilder oauthPrefix = uriComponentsBuilder.path(managementServerProperties.getBasePath() + swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl); - swaggerUiConfigParameters.setOauth2RedirectUrl(oauthPrefix.path(getOauth2RedirectUrl()).build().toString()); - } + calculateUiRootCommon(swaggerUiConfigParameters, sbUrl, sbUrls); } @Override protected void buildApiDocUrl(SwaggerUiConfigParameters swaggerUiConfigParameters) { - swaggerUiConfigParameters.setApiDocsUrl( buildUrl(swaggerUiConfigParameters.getContextPath() + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL)); + swaggerUiConfigParameters.setApiDocsUrl(buildUrl(swaggerUiConfigParameters.getContextPath() + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL)); } @Override diff --git a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeCommon.java b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeCommon.java index a7e4d1d8b..0e815af3c 100644 --- a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeCommon.java +++ b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeCommon.java @@ -21,25 +21,24 @@ * * * * * * * * * - * + * */ package org.springdoc.webflux.ui; -import java.net.URI; -import java.util.Map; - +import org.apache.commons.lang3.StringUtils; import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.properties.SwaggerUiConfigParameters; import org.springdoc.core.properties.SwaggerUiConfigProperties; import org.springdoc.ui.AbstractSwaggerWelcome; -import reactor.core.publisher.Mono; - import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; -import org.springframework.util.AntPathMatcher; import org.springframework.web.util.UriComponentsBuilder; +import reactor.core.publisher.Mono; + +import java.net.URI; +import java.util.Map; /** * The type Swagger welcome common. @@ -48,11 +47,6 @@ */ public abstract class SwaggerWelcomeCommon extends AbstractSwaggerWelcome { - /** - * The Web jars prefix url. - */ - protected String webJarsPrefixUrl; - /** * Instantiates a new Abstract swagger welcome. @@ -62,7 +56,6 @@ public abstract class SwaggerWelcomeCommon extends AbstractSwaggerWelcome { */ protected SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties) { super(swaggerUiConfig, springDocConfigProperties); - this.webJarsPrefixUrl = springDocConfigProperties.getWebjars().getPrefix(); } /** @@ -74,14 +67,39 @@ protected SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, Spring */ protected Mono redirectToUi(ServerHttpRequest request, ServerHttpResponse response) { SwaggerUiConfigParameters swaggerUiConfigParameters = new SwaggerUiConfigParameters(swaggerUiConfig); - this.buildFromCurrentContextPath(swaggerUiConfigParameters, request); - String sbUrl = this.buildUrl(swaggerUiConfigParameters.getContextPath(), swaggerUiConfigParameters.getUiRootPath() + springDocConfigProperties.getWebjars().getPrefix() + getSwaggerUiUrl()); + buildFromCurrentContextPath(swaggerUiConfigParameters, request); + String sbUrl = swaggerUiConfigParameters.getContextPath() + swaggerUiConfigParameters.getUiRootPath() + getSwaggerUiUrl(); UriComponentsBuilder uriBuilder = getUriComponentsBuilder(swaggerUiConfigParameters, sbUrl); + + // forward all queryParams from original request + request.getQueryParams().forEach(uriBuilder::queryParam); + response.setStatusCode(HttpStatus.FOUND); response.getHeaders().setLocation(URI.create(uriBuilder.build().encode().toString())); return response.setComplete(); } + /** + * Openapi json map. + * + * @param request the request + * @return the map + */ + protected Map openapiJson(ServerHttpRequest request) { + SwaggerUiConfigParameters swaggerUiConfigParameters = new SwaggerUiConfigParameters(swaggerUiConfig); + buildFromCurrentContextPath(swaggerUiConfigParameters, request); + return swaggerUiConfigParameters.getConfigParameters(); + } + + @Override + protected void calculateOauth2RedirectUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder) { + if (StringUtils.isBlank(swaggerUiConfig.getOauth2RedirectUrl()) || !swaggerUiConfigParameters.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) { + swaggerUiConfigParameters.setOauth2RedirectUrl(uriComponentsBuilder + .path(swaggerUiConfigParameters.getUiRootPath()) + .path(getOauth2RedirectUrl()).build().toString()); + } + } + /** * Gets swagger ui config. * @@ -105,8 +123,13 @@ void buildFromCurrentContextPath(SwaggerUiConfigParameters swaggerUiConfigParame super.init(swaggerUiConfigParameters); swaggerUiConfigParameters.setContextPath(request.getPath().contextPath().value()); String url = UriComponentsBuilder.fromHttpRequest(request).toUriString(); - if (!AntPathMatcher.DEFAULT_PATH_SEPARATOR.equals(request.getPath().toString())) + String target = UriComponentsBuilder.fromPath(request.getPath().contextPath().value()).toUriString(); + int endIndex = url.indexOf(target) + target.length(); + if (endIndex > 0) { + url = url.substring(0, endIndex); + } else { url = url.replace(request.getPath().toString(), ""); + } buildConfigUrl(swaggerUiConfigParameters, UriComponentsBuilder.fromUriString(url)); } diff --git a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeWebFlux.java b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeWebFlux.java index bcc3a0119..8ee781d92 100644 --- a/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeWebFlux.java +++ b/springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeWebFlux.java @@ -27,18 +27,15 @@ package org.springdoc.webflux.ui; import io.swagger.v3.oas.annotations.Operation; -import org.apache.commons.lang3.StringUtils; import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.properties.SwaggerUiConfigParameters; import org.springdoc.core.properties.SwaggerUiConfigProperties; import org.springdoc.core.providers.SpringWebProvider; -import reactor.core.publisher.Mono; - import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.util.UriComponentsBuilder; +import reactor.core.publisher.Mono; import static org.springdoc.core.utils.Constants.SWAGGER_CONFIG_FILE; import static org.springdoc.core.utils.Constants.SWAGGER_UI_PATH; @@ -90,14 +87,6 @@ protected void calculateUiRootPath(SwaggerUiConfigParameters swaggerUiConfigPara calculateUiRootCommon(swaggerUiConfigParameters,sbUrl, sbUrls); } - @Override - protected void calculateOauth2RedirectUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder) { - if (StringUtils.isBlank(swaggerUiConfig.getOauth2RedirectUrl()) || !swaggerUiConfigParameters.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) { - UriComponentsBuilder oauthPrefix = uriComponentsBuilder.path(swaggerUiConfigParameters.getContextPath()).path(swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl); - swaggerUiConfigParameters.setOauth2RedirectUrl(oauthPrefix.path(getOauth2RedirectUrl()).build().toString()); - } - } - @Override protected void buildApiDocUrl(SwaggerUiConfigParameters swaggerUiConfigParameters) { swaggerUiConfigParameters.setApiDocsUrl(buildUrlWithContextPath(swaggerUiConfigParameters, springDocConfigProperties.getApiDocs().getPath())); @@ -107,7 +96,11 @@ protected void buildApiDocUrl(SwaggerUiConfigParameters swaggerUiConfigParameter protected String buildUrlWithContextPath(SwaggerUiConfigParameters swaggerUiConfigParameters, String swaggerUiUrl) { if (swaggerUiConfigParameters.getPathPrefix() == null) swaggerUiConfigParameters.setPathPrefix(springWebProvider.findPathPrefix(springDocConfigProperties)); - return buildUrl(swaggerUiConfigParameters.getContextPath() + swaggerUiConfigParameters.getPathPrefix(), swaggerUiUrl); + if (swaggerUiUrl.startsWith(swaggerUiConfigParameters.getPathPrefix())) { + return buildUrl(swaggerUiConfigParameters.getContextPath(), swaggerUiUrl); + } else { + return buildUrl(swaggerUiConfigParameters.getContextPath() + swaggerUiConfigParameters.getPathPrefix(), swaggerUiUrl); + } } @Override diff --git a/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/providers/SpringWebMvcProvider.java b/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/providers/SpringWebMvcProvider.java index e64e17e4d..51f6bde2d 100644 --- a/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/providers/SpringWebMvcProvider.java +++ b/springdoc-openapi-starter-webmvc-api/src/main/java/org/springdoc/webmvc/core/providers/SpringWebMvcProvider.java @@ -25,17 +25,10 @@ */ package org.springdoc.webmvc.core.providers; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.stream.Collectors; - import org.apache.commons.lang3.StringUtils; import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.providers.SpringWebProvider; - +import org.springdoc.core.utils.Constants; import org.springframework.util.CollectionUtils; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.AbstractHandlerMethodMapping; @@ -44,6 +37,13 @@ import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + /** * The type Spring web mvc provider. * @@ -65,8 +65,8 @@ public String findPathPrefix(SpringDocConfigProperties springDocConfigProperties Set patterns = getActivePatterns(requestMappingInfo); if (!CollectionUtils.isEmpty(patterns)) { for (String operationPath : patterns) { - if (operationPath.endsWith(springDocConfigProperties.getApiDocs().getPath())) - return operationPath.replace(springDocConfigProperties.getApiDocs().getPath(), StringUtils.EMPTY); + if (operationPath.endsWith(Constants.DEFAULT_API_DOCS_URL)) + return operationPath.replace(Constants.DEFAULT_API_DOCS_URL, StringUtils.EMPTY); } } } diff --git a/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerConfig.java b/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerConfig.java index 8d5eb953c..bdd48be0b 100644 --- a/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerConfig.java +++ b/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerConfig.java @@ -26,8 +26,6 @@ package org.springdoc.webmvc.ui; -import java.util.Optional; - import org.springdoc.core.configuration.SpringDocConfiguration; import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.properties.SwaggerUiConfigProperties; @@ -36,7 +34,6 @@ import org.springdoc.core.providers.ObjectMapperProvider; import org.springdoc.core.providers.SpringWebProvider; import org.springdoc.webmvc.core.providers.SpringWebMvcProvider; - import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; import org.springframework.boot.actuate.autoconfigure.web.server.ConditionalOnManagementPort; import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType; @@ -51,6 +48,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; +import java.util.Optional; + import static org.springdoc.core.utils.Constants.SPRINGDOC_SWAGGER_UI_ENABLED; import static org.springdoc.core.utils.Constants.SPRINGDOC_USE_MANAGEMENT_PORT; import static org.springdoc.core.utils.Constants.SPRINGDOC_USE_ROOT_PATH; @@ -153,8 +152,9 @@ SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUi @ConditionalOnMissingBean @Lazy(false) SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties, - SwaggerIndexTransformer swaggerIndexTransformer, Optional actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) { - return new SwaggerWebMvcConfigurer(swaggerUiConfigProperties, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver); + SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer, + Optional actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) { + return new SwaggerWebMvcConfigurer(swaggerUiConfigProperties, springDocConfigProperties, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver); } /** diff --git a/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWebMvcConfigurer.java b/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWebMvcConfigurer.java index 1399e0857..c83403a3d 100644 --- a/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWebMvcConfigurer.java +++ b/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWebMvcConfigurer.java @@ -26,12 +26,9 @@ package org.springdoc.webmvc.ui; -import java.util.List; -import java.util.Optional; - +import org.springdoc.core.properties.SpringDocConfigProperties; import org.springdoc.core.properties.SwaggerUiConfigProperties; import org.springdoc.core.providers.ActuatorProvider; - import org.springframework.format.FormatterRegistry; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.lang.Nullable; @@ -51,9 +48,11 @@ import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import java.util.List; +import java.util.Optional; + +import static org.springdoc.core.utils.Constants.ALL_PATTERN; import static org.springdoc.core.utils.Constants.CLASSPATH_RESOURCE_LOCATION; -import static org.springdoc.core.utils.Constants.DEFAULT_WEB_JARS_PREFIX_URL; -import static org.springdoc.core.utils.Constants.SWAGGER_INITIALIZER_JS; import static org.springdoc.core.utils.Constants.SWAGGER_UI_PREFIX; import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR; @@ -84,21 +83,29 @@ public class SwaggerWebMvcConfigurer implements WebMvcConfigurer { */ private final SwaggerResourceResolver swaggerResourceResolver; + /** + * The Spring doc config properties. + */ + private final SpringDocConfigProperties springDocConfigProperties; + /** * Instantiates a new Swagger web mvc configurer. * * @param swaggerUiConfigProperties the swagger ui calculated config + * @param springDocConfigProperties * @param swaggerIndexTransformer the swagger index transformer * @param actuatorProvider the actuator provider * @param swaggerResourceResolver the swagger resource resolver */ public SwaggerWebMvcConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties, + SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer, Optional actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) { this.swaggerIndexTransformer = swaggerIndexTransformer; this.actuatorProvider = actuatorProvider; this.swaggerResourceResolver = swaggerResourceResolver; this.swaggerUiConfigProperties = swaggerUiConfigProperties; + this.springDocConfigProperties = springDocConfigProperties; } @Override @@ -110,18 +117,12 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) { if (actuatorProvider.isPresent() && actuatorProvider.get().isUseManagementPort()) uiRootPath.append(actuatorProvider.get().getBasePath()); - registry.addResourceHandler(uiRootPath + SWAGGER_UI_PREFIX + "*/*" + SWAGGER_INITIALIZER_JS) - .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR) + registry.addResourceHandler(uiRootPath + SWAGGER_UI_PREFIX + ALL_PATTERN) + .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + springDocConfigProperties.getWebjars().getPrefix() + DEFAULT_PATH_SEPARATOR) .setCachePeriod(0) .resourceChain(false) .addResolver(swaggerResourceResolver) .addTransformer(swaggerIndexTransformer); - - registry.addResourceHandler(uiRootPath + SWAGGER_UI_PREFIX + "*/**") - .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR) - .resourceChain(false) - .addResolver(swaggerResourceResolver) - .addTransformer(swaggerIndexTransformer); } @Override diff --git a/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWelcomeWebMvc.java b/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWelcomeWebMvc.java index 00f485938..de248dea0 100644 --- a/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWelcomeWebMvc.java +++ b/springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWelcomeWebMvc.java @@ -33,7 +33,6 @@ import org.springdoc.core.properties.SwaggerUiConfigProperties; import org.springdoc.core.providers.SpringWebProvider; import org.springdoc.core.utils.SpringDocUtils; - import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; @@ -107,7 +106,11 @@ protected void buildApiDocUrl(SwaggerUiConfigParameters swaggerUiConfigParameter protected String buildUrlWithContextPath(SwaggerUiConfigParameters swaggerUiConfigParameters, String swaggerUiUrl) { if (swaggerUiConfigParameters.getPathPrefix() == null) swaggerUiConfigParameters.setPathPrefix(springWebProvider.findPathPrefix(springDocConfigProperties)); - return buildUrl(swaggerUiConfigParameters.getContextPath() + swaggerUiConfigParameters.getPathPrefix(), swaggerUiUrl); + if (swaggerUiUrl.startsWith(swaggerUiConfigParameters.getPathPrefix())) { + return buildUrl(swaggerUiConfigParameters.getContextPath(), swaggerUiUrl); + } else { + return buildUrl(swaggerUiConfigParameters.getContextPath() + swaggerUiConfigParameters.getPathPrefix(), swaggerUiUrl); + } } @Override