Skip to content

Commit 3a15bcd

Browse files
fix swagger webjars prefix path
1 parent 174e4d8 commit 3a15bcd

File tree

11 files changed

+119
-121
lines changed

11 files changed

+119
-121
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public final class Constants {
175175
/**
176176
* The constant DEFAULT_WEB_JARS_PREFIX_URL.
177177
*/
178-
public static final String DEFAULT_WEB_JARS_PREFIX_URL = "/webjars";
178+
public static final String DEFAULT_WEB_JARS_PREFIX_URL = "/webjars/swagger-ui/5.18.2";
179179

180180
/**
181181
* The constant CLASSPATH_RESOURCE_LOCATION.

springdoc-openapi-starter-webflux-api/src/main/java/org/springdoc/webflux/core/providers/SpringWebFluxProvider.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,10 @@
2525
*/
2626
package org.springdoc.webflux.core.providers;
2727

28-
import java.util.Collection;
29-
import java.util.LinkedHashMap;
30-
import java.util.Map;
31-
import java.util.Map.Entry;
32-
import java.util.Set;
33-
import java.util.stream.Collectors;
34-
3528
import org.apache.commons.lang3.StringUtils;
3629
import org.springdoc.core.properties.SpringDocConfigProperties;
3730
import org.springdoc.core.providers.SpringWebProvider;
38-
31+
import org.springdoc.core.utils.Constants;
3932
import org.springframework.util.CollectionUtils;
4033
import org.springframework.web.method.HandlerMethod;
4134
import org.springframework.web.reactive.result.condition.PatternsRequestCondition;
@@ -44,6 +37,13 @@
4437
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
4538
import org.springframework.web.util.pattern.PathPattern;
4639

40+
import java.util.Collection;
41+
import java.util.LinkedHashMap;
42+
import java.util.Map;
43+
import java.util.Map.Entry;
44+
import java.util.Set;
45+
import java.util.stream.Collectors;
46+
4747

4848
/**
4949
* The type Spring webflux provider.
@@ -66,8 +66,8 @@ public String findPathPrefix(SpringDocConfigProperties springDocConfigProperties
6666
Set<String> patterns = getActivePatterns(requestMappingInfo);
6767
if (!CollectionUtils.isEmpty(patterns)) {
6868
for (String operationPath : patterns) {
69-
if (operationPath.endsWith(springDocConfigProperties.getApiDocs().getPath()))
70-
return operationPath.replace(springDocConfigProperties.getApiDocs().getPath(), StringUtils.EMPTY);
69+
if (operationPath.endsWith(Constants.DEFAULT_API_DOCS_URL))
70+
return operationPath.replace(Constants.DEFAULT_API_DOCS_URL, StringUtils.EMPTY);
7171
}
7272
}
7373
}

springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerConfig.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
package org.springdoc.webflux.ui;
2828

29-
import java.util.Optional;
30-
3129
import org.springdoc.core.configuration.SpringDocConfiguration;
3230
import org.springdoc.core.properties.SpringDocConfigProperties;
3331
import org.springdoc.core.properties.SwaggerUiConfigProperties;
@@ -36,7 +34,6 @@
3634
import org.springdoc.core.providers.ObjectMapperProvider;
3735
import org.springdoc.core.providers.SpringWebProvider;
3836
import org.springdoc.webflux.core.providers.SpringWebFluxProvider;
39-
4037
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
4138
import org.springframework.boot.actuate.autoconfigure.web.server.ConditionalOnManagementPort;
4239
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
@@ -54,6 +51,8 @@
5451
import org.springframework.context.annotation.Lazy;
5552
import org.springframework.web.reactive.config.WebFluxConfigurer;
5653

54+
import java.util.Optional;
55+
5756
import static org.springdoc.core.utils.Constants.SPRINGDOC_SWAGGER_UI_ENABLED;
5857
import static org.springdoc.core.utils.Constants.SPRINGDOC_USE_MANAGEMENT_PORT;
5958
import static org.springdoc.core.utils.Constants.SPRINGDOC_USE_ROOT_PATH;
@@ -129,9 +128,9 @@ SwaggerUiHome swaggerUiHome(Optional<WebFluxProperties> optionalWebFluxPropertie
129128
@ConditionalOnMissingBean
130129
@Lazy(false)
131130
SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties,
132-
SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer,
131+
SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer,
133132
Optional<ActuatorProvider> actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) {
134-
return new SwaggerWebFluxConfigurer(swaggerUiConfigProperties,springDocConfigProperties, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver);
133+
return new SwaggerWebFluxConfigurer(swaggerUiConfigProperties, springDocConfigProperties, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver);
135134
}
136135

137136
/**
@@ -207,7 +206,7 @@ static class SwaggerActuatorWelcomeConfiguration {
207206
@Lazy(false)
208207
SwaggerWelcomeActuator swaggerActuatorWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties,
209208
WebEndpointProperties webEndpointProperties, ManagementServerProperties managementServerProperties) {
210-
return new SwaggerWelcomeActuator(swaggerUiConfig, springDocConfigProperties, webEndpointProperties, managementServerProperties);
209+
return new SwaggerWelcomeActuator(swaggerUiConfig, springDocConfigProperties, webEndpointProperties);
211210
}
212211
}
213212
}

springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWebFluxConfigurer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,22 @@
2121
* * * *
2222
* * *
2323
* *
24-
*
24+
*
2525
*/
2626

2727
package org.springdoc.webflux.ui;
2828

29-
import java.util.Optional;
30-
3129
import org.springdoc.core.properties.SpringDocConfigProperties;
3230
import org.springdoc.core.properties.SwaggerUiConfigProperties;
3331
import org.springdoc.core.providers.ActuatorProvider;
34-
3532
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
3633
import org.springframework.web.reactive.config.WebFluxConfigurer;
3734

35+
import java.util.Optional;
36+
3837
import static org.springdoc.core.utils.Constants.ALL_PATTERN;
3938
import static org.springdoc.core.utils.Constants.CLASSPATH_RESOURCE_LOCATION;
40-
import static org.springdoc.core.utils.Constants.DEFAULT_WEB_JARS_PREFIX_URL;
39+
import static org.springdoc.core.utils.Constants.SWAGGER_UI_PREFIX;
4140
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
4241

4342
/**
@@ -82,8 +81,8 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer {
8281
* @param swaggerResourceResolver the swagger resource resolver
8382
*/
8483
public SwaggerWebFluxConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties,
85-
SpringDocConfigProperties springDocConfigProperties,
86-
SwaggerIndexTransformer swaggerIndexTransformer,
84+
SpringDocConfigProperties springDocConfigProperties,
85+
SwaggerIndexTransformer swaggerIndexTransformer,
8786
Optional<ActuatorProvider> actuatorProvider, SwaggerResourceResolver swaggerResourceResolver) {
8887
this.swaggerIndexTransformer = swaggerIndexTransformer;
8988
this.actuatorProvider = actuatorProvider;
@@ -100,8 +99,9 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
10099
uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR));
101100
if (actuatorProvider.isPresent() && actuatorProvider.get().isUseManagementPort())
102101
uiRootPath.append(actuatorProvider.get().getBasePath());
103-
registry.addResourceHandler(uiRootPath + springDocConfigProperties.getWebjars().getPrefix() + ALL_PATTERN)
104-
.addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR)
102+
103+
registry.addResourceHandler(uiRootPath + SWAGGER_UI_PREFIX + ALL_PATTERN)
104+
.addResourceLocations(CLASSPATH_RESOURCE_LOCATION + springDocConfigProperties.getWebjars().getPrefix() + DEFAULT_PATH_SEPARATOR)
105105
.resourceChain(false)
106106
.addResolver(swaggerResourceResolver)
107107
.addTransformer(swaggerIndexTransformer);

springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeActuator.java

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,20 @@
2626

2727
package org.springdoc.webflux.ui;
2828

29-
import java.util.Map;
30-
3129
import io.swagger.v3.oas.annotations.Operation;
32-
import org.apache.commons.lang3.StringUtils;
3330
import org.springdoc.core.properties.SpringDocConfigProperties;
3431
import org.springdoc.core.properties.SwaggerUiConfigParameters;
3532
import org.springdoc.core.properties.SwaggerUiConfigProperties;
36-
import reactor.core.publisher.Mono;
37-
3833
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
39-
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties;
4034
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoint;
4135
import org.springframework.http.MediaType;
4236
import org.springframework.http.server.reactive.ServerHttpRequest;
4337
import org.springframework.http.server.reactive.ServerHttpResponse;
4438
import org.springframework.web.bind.annotation.GetMapping;
4539
import org.springframework.web.bind.annotation.ResponseBody;
46-
import org.springframework.web.util.UriComponentsBuilder;
40+
import reactor.core.publisher.Mono;
41+
42+
import java.util.Map;
4743

4844
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_ACTUATOR_URL;
4945
import static org.springdoc.core.utils.Constants.DEFAULT_SWAGGER_UI_ACTUATOR_PATH;
@@ -68,26 +64,18 @@ public class SwaggerWelcomeActuator extends SwaggerWelcomeCommon {
6864
*/
6965
private final WebEndpointProperties webEndpointProperties;
7066

71-
/**
72-
* The Management server properties.
73-
*/
74-
private final ManagementServerProperties managementServerProperties;
75-
7667
/**
7768
* Instantiates a new Swagger welcome.
7869
*
79-
* @param swaggerUiConfig the swagger ui config
80-
* @param springDocConfigProperties the spring doc config properties
81-
* @param webEndpointProperties the web endpoint properties
82-
* @param managementServerProperties the management server properties
70+
* @param swaggerUiConfig the swagger ui config
71+
* @param springDocConfigProperties the swagger ui config parameters
72+
* @param webEndpointProperties the web endpoint properties
8373
*/
8474
public SwaggerWelcomeActuator(SwaggerUiConfigProperties swaggerUiConfig
8575
, SpringDocConfigProperties springDocConfigProperties,
86-
WebEndpointProperties webEndpointProperties,
87-
ManagementServerProperties managementServerProperties) {
76+
WebEndpointProperties webEndpointProperties) {
8877
super(swaggerUiConfig, springDocConfigProperties);
8978
this.webEndpointProperties = webEndpointProperties;
90-
this.managementServerProperties = managementServerProperties;
9179
}
9280

9381
/**
@@ -104,39 +92,30 @@ public Mono<Void> redirectToUi(ServerHttpRequest request, ServerHttpResponse res
10492
return super.redirectToUi(request, response);
10593
}
10694

107-
10895
/**
109-
* Gets swagger ui config.
96+
* Openapi yaml map.
11097
*
11198
* @param request the request
112-
* @return the swagger ui config
99+
* @return the map
113100
*/
114101
@Operation(hidden = true)
115102
@GetMapping(value = SWAGGER_CONFIG_ACTUATOR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
116103
@ResponseBody
117104
@Override
118-
public Map<String, Object> getSwaggerUiConfig(ServerHttpRequest request) {
119-
return super.getSwaggerUiConfig(request);
105+
public Map<String, Object> openapiJson(ServerHttpRequest request) {
106+
return super.openapiJson(request);
120107
}
121108

122109
@Override
123-
protected void calculateUiRootPath(SwaggerUiConfigParameters swaggerUiConfigParameters,StringBuilder... sbUrls) {
110+
protected void calculateUiRootPath(SwaggerUiConfigParameters swaggerUiConfigParameters, StringBuilder... sbUrls) {
124111
StringBuilder sbUrl = new StringBuilder();
125112
sbUrl.append(webEndpointProperties.getBasePath());
126-
calculateUiRootCommon(swaggerUiConfigParameters,sbUrl, sbUrls);
127-
}
128-
129-
@Override
130-
protected void calculateOauth2RedirectUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder) {
131-
if (StringUtils.isBlank(swaggerUiConfig.getOauth2RedirectUrl()) || !swaggerUiConfigParameters.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) {
132-
UriComponentsBuilder oauthPrefix = uriComponentsBuilder.path(managementServerProperties.getBasePath() + swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl);
133-
swaggerUiConfigParameters.setOauth2RedirectUrl(oauthPrefix.path(getOauth2RedirectUrl()).build().toString());
134-
}
113+
calculateUiRootCommon(swaggerUiConfigParameters, sbUrl, sbUrls);
135114
}
136115

137116
@Override
138117
protected void buildApiDocUrl(SwaggerUiConfigParameters swaggerUiConfigParameters) {
139-
swaggerUiConfigParameters.setApiDocsUrl( buildUrl(swaggerUiConfigParameters.getContextPath() + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL));
118+
swaggerUiConfigParameters.setApiDocsUrl(buildUrl(swaggerUiConfigParameters.getContextPath() + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL));
140119
}
141120

142121
@Override

springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerWelcomeCommon.java

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,24 @@
2121
* * * *
2222
* * *
2323
* *
24-
*
24+
*
2525
*/
2626

2727
package org.springdoc.webflux.ui;
2828

29-
import java.net.URI;
30-
import java.util.Map;
31-
29+
import org.apache.commons.lang3.StringUtils;
3230
import org.springdoc.core.properties.SpringDocConfigProperties;
3331
import org.springdoc.core.properties.SwaggerUiConfigParameters;
3432
import org.springdoc.core.properties.SwaggerUiConfigProperties;
3533
import org.springdoc.ui.AbstractSwaggerWelcome;
36-
import reactor.core.publisher.Mono;
37-
3834
import org.springframework.http.HttpStatus;
3935
import org.springframework.http.server.reactive.ServerHttpRequest;
4036
import org.springframework.http.server.reactive.ServerHttpResponse;
41-
import org.springframework.util.AntPathMatcher;
4237
import org.springframework.web.util.UriComponentsBuilder;
38+
import reactor.core.publisher.Mono;
39+
40+
import java.net.URI;
41+
import java.util.Map;
4342

4443
/**
4544
* The type Swagger welcome common.
@@ -48,11 +47,6 @@
4847
*/
4948
public abstract class SwaggerWelcomeCommon extends AbstractSwaggerWelcome {
5049

51-
/**
52-
* The Web jars prefix url.
53-
*/
54-
protected String webJarsPrefixUrl;
55-
5650

5751
/**
5852
* Instantiates a new Abstract swagger welcome.
@@ -62,7 +56,6 @@ public abstract class SwaggerWelcomeCommon extends AbstractSwaggerWelcome {
6256
*/
6357
protected SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties) {
6458
super(swaggerUiConfig, springDocConfigProperties);
65-
this.webJarsPrefixUrl = springDocConfigProperties.getWebjars().getPrefix();
6659
}
6760

6861
/**
@@ -74,14 +67,39 @@ protected SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, Spring
7467
*/
7568
protected Mono<Void> redirectToUi(ServerHttpRequest request, ServerHttpResponse response) {
7669
SwaggerUiConfigParameters swaggerUiConfigParameters = new SwaggerUiConfigParameters(swaggerUiConfig);
77-
this.buildFromCurrentContextPath(swaggerUiConfigParameters, request);
78-
String sbUrl = this.buildUrl(swaggerUiConfigParameters.getContextPath(), swaggerUiConfigParameters.getUiRootPath() + springDocConfigProperties.getWebjars().getPrefix() + getSwaggerUiUrl());
70+
buildFromCurrentContextPath(swaggerUiConfigParameters, request);
71+
String sbUrl = swaggerUiConfigParameters.getContextPath() + swaggerUiConfigParameters.getUiRootPath() + getSwaggerUiUrl();
7972
UriComponentsBuilder uriBuilder = getUriComponentsBuilder(swaggerUiConfigParameters, sbUrl);
73+
74+
// forward all queryParams from original request
75+
request.getQueryParams().forEach(uriBuilder::queryParam);
76+
8077
response.setStatusCode(HttpStatus.FOUND);
8178
response.getHeaders().setLocation(URI.create(uriBuilder.build().encode().toString()));
8279
return response.setComplete();
8380
}
8481

82+
/**
83+
* Openapi json map.
84+
*
85+
* @param request the request
86+
* @return the map
87+
*/
88+
protected Map<String, Object> openapiJson(ServerHttpRequest request) {
89+
SwaggerUiConfigParameters swaggerUiConfigParameters = new SwaggerUiConfigParameters(swaggerUiConfig);
90+
buildFromCurrentContextPath(swaggerUiConfigParameters, request);
91+
return swaggerUiConfigParameters.getConfigParameters();
92+
}
93+
94+
@Override
95+
protected void calculateOauth2RedirectUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder) {
96+
if (StringUtils.isBlank(swaggerUiConfig.getOauth2RedirectUrl()) || !swaggerUiConfigParameters.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) {
97+
swaggerUiConfigParameters.setOauth2RedirectUrl(uriComponentsBuilder
98+
.path(swaggerUiConfigParameters.getUiRootPath())
99+
.path(getOauth2RedirectUrl()).build().toString());
100+
}
101+
}
102+
85103
/**
86104
* Gets swagger ui config.
87105
*
@@ -105,8 +123,13 @@ void buildFromCurrentContextPath(SwaggerUiConfigParameters swaggerUiConfigParame
105123
super.init(swaggerUiConfigParameters);
106124
swaggerUiConfigParameters.setContextPath(request.getPath().contextPath().value());
107125
String url = UriComponentsBuilder.fromHttpRequest(request).toUriString();
108-
if (!AntPathMatcher.DEFAULT_PATH_SEPARATOR.equals(request.getPath().toString()))
126+
String target = UriComponentsBuilder.fromPath(request.getPath().contextPath().value()).toUriString();
127+
int endIndex = url.indexOf(target) + target.length();
128+
if (endIndex > 0) {
129+
url = url.substring(0, endIndex);
130+
} else {
109131
url = url.replace(request.getPath().toString(), "");
132+
}
110133
buildConfigUrl(swaggerUiConfigParameters, UriComponentsBuilder.fromUriString(url));
111134
}
112135

0 commit comments

Comments
 (0)