Skip to content

Commit 8f8ebc6

Browse files
committed
Use Swagger welcome to get UI root path for resource handlers
1 parent 8173442 commit 8f8ebc6

File tree

12 files changed

+116
-138
lines changed

12 files changed

+116
-138
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springdoc.core.properties.SpringDocConfigProperties;
3434
import org.springdoc.core.properties.SwaggerUiConfigProperties;
3535
import org.springdoc.core.properties.SwaggerUiOAuthProperties;
36-
import org.springdoc.core.providers.ActuatorProvider;
3736
import org.springdoc.core.providers.ObjectMapperProvider;
3837
import org.springdoc.core.providers.SpringWebProvider;
3938
import org.springdoc.webflux.core.providers.SpringWebFluxProvider;
@@ -125,18 +124,18 @@ SwaggerUiHome swaggerUiHome(Optional<WebFluxProperties> optionalWebFluxPropertie
125124
* @param springWebProperties the spring web config
126125
* @param springWebFluxProperties the spring webflux config
127126
* @param swaggerIndexTransformer the swagger index transformer
128-
* @param actuatorProvider the actuator provider
129127
* @param swaggerResourceResolver the swagger resource resolver
128+
* @param swaggerWelcomeCommon the swagger welcome common
130129
* @return the swagger web flux configurer
131130
*/
132131
@Bean
133132
@ConditionalOnMissingBean
134133
@Lazy(false)
135134
SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties,
136135
WebProperties springWebProperties, WebFluxProperties springWebFluxProperties,
137-
SwaggerIndexTransformer swaggerIndexTransformer, Optional<ActuatorProvider> actuatorProvider,
138-
SwaggerResourceResolver swaggerResourceResolver) {
139-
return new SwaggerWebFluxConfigurer(swaggerUiConfigProperties, springWebProperties, springWebFluxProperties, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver);
136+
SwaggerIndexTransformer swaggerIndexTransformer, SwaggerResourceResolver swaggerResourceResolver,
137+
SwaggerWelcomeCommon swaggerWelcomeCommon) {
138+
return new SwaggerWebFluxConfigurer(swaggerUiConfigProperties, springWebProperties, springWebFluxProperties, swaggerIndexTransformer, swaggerResourceResolver, swaggerWelcomeCommon);
140139
}
141140

142141
/**

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import io.swagger.v3.oas.annotations.Operation;
3131

3232
import org.springframework.http.MediaType;
33-
import org.springframework.http.server.reactive.ServerHttpRequest;
3433
import org.springframework.web.bind.annotation.GetMapping;
3534
import org.springframework.web.bind.annotation.RestController;
35+
import org.springframework.web.server.ServerWebExchange;
3636

3737
import static org.springdoc.core.utils.Constants.SWAGGER_CONFIG_URL;
3838

@@ -61,13 +61,13 @@ public SwaggerConfigResource(SwaggerWelcomeCommon swaggerWelcomeCommon) {
6161
/**
6262
* Gets swagger ui config.
6363
*
64-
* @param request the request
64+
* @param exchange the exchange
6565
* @return the swagger ui config
6666
*/
6767
@Operation(hidden = true)
6868
@GetMapping(value = SWAGGER_CONFIG_URL, produces = MediaType.APPLICATION_JSON_VALUE)
69-
public Map<String, Object> getSwaggerUiConfig(ServerHttpRequest request) {
70-
return swaggerWelcomeCommon.getSwaggerUiConfig(request);
69+
public Map<String, Object> getSwaggerUiConfig(ServerWebExchange exchange) {
70+
return swaggerWelcomeCommon.getSwaggerUiConfig(exchange);
7171
}
7272

7373
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, Sw
7171
}
7272

7373
@Override
74-
public Mono<Resource> transform(ServerWebExchange serverWebExchange, Resource resource, ResourceTransformerChain resourceTransformerChain) {
74+
public Mono<Resource> transform(ServerWebExchange exchange, Resource resource, ResourceTransformerChain resourceTransformerChain) {
7575
SwaggerUiConfigParameters swaggerUiConfigParameters = new SwaggerUiConfigParameters(swaggerUiConfig);
76-
swaggerWelcomeCommon.buildFromCurrentContextPath(swaggerUiConfigParameters, serverWebExchange.getRequest());
76+
swaggerWelcomeCommon.buildFromCurrentContextPath(swaggerUiConfigParameters, exchange);
7777

7878
final AntPathMatcher antPathMatcher = new AntPathMatcher();
7979
try {

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

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
package org.springdoc.webflux.ui;
2828

29-
import java.util.Optional;
29+
import org.springdoc.core.properties.SwaggerUiConfigParameters;
3030
import org.springdoc.core.properties.SwaggerUiConfigProperties;
31-
import org.springdoc.core.providers.ActuatorProvider;
31+
3232
import org.springframework.boot.autoconfigure.web.WebProperties;
3333
import org.springframework.boot.webflux.autoconfigure.WebFluxProperties;
3434
import org.springframework.http.CacheControl;
@@ -56,11 +56,6 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer {
5656
*/
5757
private final SwaggerIndexTransformer swaggerIndexTransformer;
5858

59-
/**
60-
* The Actuator provider.
61-
*/
62-
private final Optional<ActuatorProvider> actuatorProvider;
63-
6459
/**
6560
* The Swagger resource resolver.
6661
*/
@@ -81,6 +76,11 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer {
8176
*/
8277
private final WebFluxProperties springWebFluxProperties;
8378

79+
/**
80+
* The Swagger welcome common.
81+
*/
82+
private final SwaggerWelcomeCommon swaggerWelcomeCommon;
83+
8484
private final PathPatternParser parser = new PathPatternParser();
8585

8686
/**
@@ -90,24 +90,27 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer {
9090
* @param springWebProperties the spring web config
9191
* @param springWebFluxProperties the spring webflux config
9292
* @param swaggerIndexTransformer the swagger index transformer
93-
* @param actuatorProvider the actuator provider
9493
* @param swaggerResourceResolver the swagger resource resolver
94+
* @param swaggerWelcomeCommon the swagger welcome common
9595
*/
9696
public SwaggerWebFluxConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties,
9797
WebProperties springWebProperties, WebFluxProperties springWebFluxProperties,
98-
SwaggerIndexTransformer swaggerIndexTransformer, Optional<ActuatorProvider> actuatorProvider,
99-
SwaggerResourceResolver swaggerResourceResolver) {
98+
SwaggerIndexTransformer swaggerIndexTransformer, SwaggerResourceResolver swaggerResourceResolver,
99+
SwaggerWelcomeCommon swaggerWelcomeCommon) {
100100
this.swaggerIndexTransformer = swaggerIndexTransformer;
101-
this.actuatorProvider = actuatorProvider;
102101
this.swaggerResourceResolver = swaggerResourceResolver;
103102
this.swaggerUiConfigProperties = swaggerUiConfigProperties;
104103
this.springWebProperties = springWebProperties;
105104
this.springWebFluxProperties = springWebFluxProperties;
105+
this.swaggerWelcomeCommon = swaggerWelcomeCommon;
106106
}
107107

108108
@Override
109109
public void addResourceHandlers(ResourceHandlerRegistry registry) {
110-
String swaggerUiPattern = getUiRootPath() + SWAGGER_UI_PREFIX + ALL_PATTERN;
110+
SwaggerUiConfigParameters swaggerUiConfigParameters = new SwaggerUiConfigParameters(swaggerUiConfigProperties);
111+
swaggerWelcomeCommon.calculateUiRootPath(swaggerUiConfigParameters);
112+
113+
String swaggerUiPattern = swaggerUiConfigParameters.getUiRootPath() + SWAGGER_UI_PREFIX + ALL_PATTERN;
111114
String swaggerUiResourceLocation = WEBJARS_RESOURCE_LOCATION + SWAGGER_UI_WEBJAR_NAME + DEFAULT_PATH_SEPARATOR +
112115
swaggerUiConfigProperties.getVersion() + DEFAULT_PATH_SEPARATOR;
113116

@@ -143,26 +146,6 @@ protected void addSwaggerUiResourceHandler(ResourceHandlerRegistry registry, Str
143146
.addTransformer(swaggerIndexTransformer);
144147
}
145148

146-
/**
147-
* Computes and returns the root path for the Swagger UI.
148-
*
149-
* @return the Swagger UI root path.
150-
*/
151-
protected String getUiRootPath() {
152-
StringBuilder uiRootPath = new StringBuilder();
153-
154-
if (actuatorProvider.isPresent() && actuatorProvider.get().isUseManagementPort()) {
155-
uiRootPath.append(actuatorProvider.get().getBasePath());
156-
}
157-
158-
String swaggerUiPath = swaggerUiConfigProperties.getPath();
159-
if (swaggerUiPath.contains(DEFAULT_PATH_SEPARATOR)) {
160-
uiRootPath.append(swaggerUiPath, 0, swaggerUiPath.lastIndexOf(DEFAULT_PATH_SEPARATOR));
161-
}
162-
163-
return uiRootPath.toString();
164-
}
165-
166149
/**
167150
* Combines two patterns into a new pattern according to the rules of {@link PathPattern#combine}.
168151
*

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@
3737
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
3838
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoint;
3939
import org.springframework.http.MediaType;
40-
import org.springframework.http.server.reactive.ServerHttpRequest;
41-
import org.springframework.http.server.reactive.ServerHttpResponse;
4240
import org.springframework.web.bind.annotation.GetMapping;
4341
import org.springframework.web.bind.annotation.ResponseBody;
42+
import org.springframework.web.server.ServerWebExchange;
4443

4544
import static org.springdoc.core.utils.Constants.DEFAULT_API_DOCS_ACTUATOR_URL;
4645
import static org.springdoc.core.utils.Constants.DEFAULT_SWAGGER_UI_ACTUATOR_PATH;
@@ -82,29 +81,28 @@ public SwaggerWelcomeActuator(SwaggerUiConfigProperties swaggerUiConfig
8281
/**
8382
* Redirect to ui mono.
8483
*
85-
* @param request the request
86-
* @param response the response
84+
* @param exchange the exchange
8785
* @return the mono
8886
*/
8987
@Operation(hidden = true)
9088
@GetMapping(DEFAULT_PATH_SEPARATOR)
9189
@Override
92-
public Mono<Void> redirectToUi(ServerHttpRequest request, ServerHttpResponse response) {
93-
return super.redirectToUi(request, response);
90+
public Mono<Void> redirectToUi(ServerWebExchange exchange) {
91+
return super.redirectToUi(exchange);
9492
}
9593

9694
/**
9795
* Openapi yaml map.
9896
*
99-
* @param request the request
97+
* @param exchange the exchange
10098
* @return the map
10199
*/
102100
@Operation(hidden = true)
103101
@GetMapping(value = SWAGGER_CONFIG_ACTUATOR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
104102
@ResponseBody
105103
@Override
106-
public Map<String, Object> getSwaggerUiConfig(ServerHttpRequest request) {
107-
return super.getSwaggerUiConfig(request);
104+
public Map<String, Object> getSwaggerUiConfig(ServerWebExchange exchange) {
105+
return super.getSwaggerUiConfig(exchange);
108106
}
109107

110108
@Override

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
import org.springdoc.core.properties.SwaggerUiConfigParameters;
3535
import org.springdoc.core.properties.SwaggerUiConfigProperties;
3636
import org.springdoc.ui.AbstractSwaggerWelcome;
37+
import org.springframework.http.HttpHeaders;
38+
import org.springframework.web.util.ForwardedHeaderUtils;
3739
import reactor.core.publisher.Mono;
3840

3941
import org.springframework.http.HttpStatus;
40-
import org.springframework.http.server.reactive.ServerHttpRequest;
41-
import org.springframework.http.server.reactive.ServerHttpResponse;
42-
import org.springframework.web.util.ForwardedHeaderUtils;
42+
import org.springframework.web.server.ServerWebExchange;
4343
import org.springframework.web.util.UriComponentsBuilder;
4444

4545
/**
@@ -63,22 +63,21 @@ protected SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, Spring
6363
/**
6464
* Redirect to ui mono.
6565
*
66-
* @param request the request
67-
* @param response the response
66+
* @param exchange the exchange
6867
* @return the mono
6968
*/
70-
protected Mono<Void> redirectToUi(ServerHttpRequest request, ServerHttpResponse response) {
69+
protected Mono<Void> redirectToUi(ServerWebExchange exchange) {
7170
SwaggerUiConfigParameters swaggerUiConfigParameters = new SwaggerUiConfigParameters(swaggerUiConfig);
72-
buildFromCurrentContextPath(swaggerUiConfigParameters, request);
71+
buildFromCurrentContextPath(swaggerUiConfigParameters, exchange);
7372
String sbUrl = swaggerUiConfigParameters.getContextPath() + swaggerUiConfigParameters.getUiRootPath() + getSwaggerUiUrl();
7473
UriComponentsBuilder uriBuilder = getUriComponentsBuilder(swaggerUiConfigParameters, sbUrl);
7574

7675
// forward all queryParams from original request
77-
request.getQueryParams().forEach(uriBuilder::queryParam);
76+
exchange.getRequest().getQueryParams().forEach(uriBuilder::queryParam);
7877

79-
response.setStatusCode(HttpStatus.FOUND);
80-
response.getHeaders().setLocation(URI.create(uriBuilder.build().encode().toString()));
81-
return response.setComplete();
78+
exchange.getResponse().setStatusCode(HttpStatus.FOUND);
79+
exchange.getResponse().getHeaders().setLocation(URI.create(uriBuilder.build().encode().toString()));
80+
return exchange.getResponse().setComplete();
8281
}
8382

8483
@Override
@@ -90,38 +89,39 @@ protected void calculateOauth2RedirectUrl(SwaggerUiConfigParameters swaggerUiCon
9089
}
9190
}
9291

92+
@Override
93+
protected abstract void calculateUiRootPath(SwaggerUiConfigParameters swaggerUiConfigParameters, StringBuilder... sbUrls);
94+
9395
/**
9496
* Gets swagger ui config.
9597
*
96-
* @param request the request
98+
* @param exchange the exchange
9799
* @return the swagger ui config
98100
*/
99-
protected Map<String, Object> getSwaggerUiConfig(ServerHttpRequest request) {
101+
protected Map<String, Object> getSwaggerUiConfig(ServerWebExchange exchange) {
100102
SwaggerUiConfigParameters swaggerUiConfigParameters = new SwaggerUiConfigParameters(swaggerUiConfig);
101-
this.buildFromCurrentContextPath(swaggerUiConfigParameters, request);
103+
this.buildFromCurrentContextPath(swaggerUiConfigParameters, exchange);
102104
return swaggerUiConfigParameters.getConfigParameters();
103105
}
104106

105107
/**
106108
* From current context path string.
107109
*
108110
* @param swaggerUiConfigParameters the swagger ui config parameters
109-
* @param request the request
110-
* @return the string
111+
* @param exchange the exchange
111112
*/
112-
void buildFromCurrentContextPath(SwaggerUiConfigParameters swaggerUiConfigParameters, ServerHttpRequest request) {
113+
void buildFromCurrentContextPath(SwaggerUiConfigParameters swaggerUiConfigParameters, ServerWebExchange exchange) {
113114
super.init(swaggerUiConfigParameters);
114-
swaggerUiConfigParameters.setContextPath(request.getPath().contextPath().value());
115-
String url = ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders()).toUriString();
116-
String target = UriComponentsBuilder.fromPath(request.getPath().contextPath().value()).toUriString();
117-
int endIndex = url.indexOf(target) + target.length();
118-
if (endIndex > 0) {
119-
url = url.substring(0, endIndex);
120-
}
121-
else {
122-
url = url.replace(request.getPath().toString(), "");
123-
}
124-
buildConfigUrl(swaggerUiConfigParameters, UriComponentsBuilder.fromUriString(url));
115+
116+
String contextPath = exchange.getRequest().getPath().contextPath().value();
117+
swaggerUiConfigParameters.setContextPath(contextPath);
118+
119+
URI uri = exchange.getRequest().getURI();
120+
HttpHeaders headers = exchange.getRequest().getHeaders();
121+
122+
UriComponentsBuilder uriComponentsBuilder = ForwardedHeaderUtils.adaptFromForwardedHeaders(uri, headers)
123+
.replacePath(contextPath).replaceQuery(null).fragment(null);
124+
buildConfigUrl(swaggerUiConfigParameters, uriComponentsBuilder);
125125
}
126126

127127
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@
3333
import org.springdoc.core.providers.SpringWebProvider;
3434
import reactor.core.publisher.Mono;
3535

36-
import org.springframework.http.server.reactive.ServerHttpRequest;
37-
import org.springframework.http.server.reactive.ServerHttpResponse;
3836
import org.springframework.stereotype.Controller;
3937
import org.springframework.web.bind.annotation.GetMapping;
38+
import org.springframework.web.server.ServerWebExchange;
4039

4140
import static org.springdoc.core.utils.Constants.SWAGGER_CONFIG_FILE;
4241
import static org.springdoc.core.utils.Constants.SWAGGER_UI_PATH;
@@ -71,15 +70,14 @@ public SwaggerWelcomeWebFlux(SwaggerUiConfigProperties swaggerUiConfig, SpringDo
7170
/**
7271
* Redirect to ui mono.
7372
*
74-
* @param request the request
75-
* @param response the response
73+
* @param exchange the exchange
7674
* @return the mono
7775
*/
7876
@Operation(hidden = true)
7977
@GetMapping(SWAGGER_UI_PATH)
8078
@Override
81-
public Mono<Void> redirectToUi(ServerHttpRequest request, ServerHttpResponse response) {
82-
return super.redirectToUi(request, response);
79+
public Mono<Void> redirectToUi(ServerWebExchange exchange) {
80+
return super.redirectToUi(exchange);
8381
}
8482

8583
@Override

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.springdoc.core.properties.SpringDocConfigProperties;
3434
import org.springdoc.core.properties.SwaggerUiConfigProperties;
3535
import org.springdoc.core.properties.SwaggerUiOAuthProperties;
36-
import org.springdoc.core.providers.ActuatorProvider;
3736
import org.springdoc.core.providers.ObjectMapperProvider;
3837
import org.springdoc.core.providers.SpringWebProvider;
3938
import org.springdoc.webmvc.core.providers.SpringWebMvcProvider;
@@ -153,18 +152,18 @@ SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUi
153152
* @param springWebProperties the spring web config
154153
* @param springWebMvcProperties the spring mvc config
155154
* @param swaggerIndexTransformer the swagger index transformer
156-
* @param actuatorProvider the actuator provider
157155
* @param swaggerResourceResolver the swagger resource resolver
156+
* @param swaggerWelcomeCommon the swagger welcome common
158157
* @return the swagger web mvc configurer
159158
*/
160159
@Bean
161160
@ConditionalOnMissingBean
162161
@Lazy(false)
163162
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigProperties swaggerUiConfigProperties,
164163
WebProperties springWebProperties, WebMvcProperties springWebMvcProperties,
165-
SwaggerIndexTransformer swaggerIndexTransformer, Optional<ActuatorProvider> actuatorProvider,
166-
SwaggerResourceResolver swaggerResourceResolver) {
167-
return new SwaggerWebMvcConfigurer(swaggerUiConfigProperties, springWebProperties, springWebMvcProperties, swaggerIndexTransformer, actuatorProvider, swaggerResourceResolver);
164+
SwaggerIndexTransformer swaggerIndexTransformer, SwaggerResourceResolver swaggerResourceResolver,
165+
SwaggerWelcomeCommon swaggerWelcomeCommon) {
166+
return new SwaggerWebMvcConfigurer(swaggerUiConfigProperties, springWebProperties, springWebMvcProperties, swaggerIndexTransformer, swaggerResourceResolver, swaggerWelcomeCommon);
168167
}
169168

170169
/**

0 commit comments

Comments
 (0)