Skip to content

Commit f505a5d

Browse files
committed
Merge branch 'Daimler-bugfix/respect_cache_disabled'
2 parents 5c7f784 + 0a6340b commit f505a5d

File tree

6 files changed

+146
-8
lines changed

6 files changed

+146
-8
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.util.CollectionUtils;
3131
import org.springframework.web.util.UriComponentsBuilder;
3232

33+
import static org.springdoc.core.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
3334
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
3435

3536

@@ -205,4 +206,16 @@ protected String buildApiDocUrl(String contextPath) {
205206
protected String buildSwaggerConfigUrl(String contextPath) {
206207
return this.swaggerConfigUrl;
207208
}
209+
210+
/**
211+
* Gets oauth2 redirect url.
212+
*
213+
* @return the oauth2 redirect url
214+
*/
215+
protected String getOauth2RedirectUrl() {
216+
if (springDocConfigProperties.isCacheDisabled())
217+
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_OAUTH_REDIRECT_URL);
218+
else
219+
return swaggerUiConfigParameters.getOauth2RedirectUrl();
220+
}
208221
}

springdoc-openapi-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerWelcomeCommon.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,28 @@
1616

1717
import static org.springdoc.core.Constants.SWAGGER_UI_URL;
1818

19+
/**
20+
* The type Swagger welcome common.
21+
* @author bnasslashen
22+
*/
1923
public abstract class SwaggerWelcomeCommon extends AbstractSwaggerWelcome {
2024
/**
2125
* Instantiates a new Abstract swagger welcome.
22-
* @param swaggerUiConfig the swagger ui config
26+
* @param swaggerUiConfig the swagger ui config
2327
* @param springDocConfigProperties the spring doc config properties
2428
* @param swaggerUiConfigParameters the swagger ui config parameters
2529
*/
26-
public SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters) {
30+
public SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties,
31+
SwaggerUiConfigParameters swaggerUiConfigParameters) {
2732
super(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters);
2833
}
2934

35+
/**
36+
* Redirect to ui response entity.
37+
*
38+
* @param request the request
39+
* @return the response entity
40+
*/
3041
protected ResponseEntity<Void> redirectToUi(HttpServletRequest request) {
3142
buildConfigUrl(request.getContextPath(), ServletUriComponentsBuilder.fromCurrentContextPath());
3243
String sbUrl = request.getContextPath() + swaggerUiConfigParameters.getUiRootPath() + SWAGGER_UI_URL;
@@ -40,14 +51,22 @@ protected ResponseEntity<Void> redirectToUi(HttpServletRequest request) {
4051
.build();
4152
}
4253

54+
/**
55+
* Openapi json map.
56+
*
57+
* @param request the request
58+
* @return the map
59+
*/
4360
protected Map<String, Object> openapiJson(HttpServletRequest request) {
4461
buildConfigUrl(request.getContextPath(), ServletUriComponentsBuilder.fromCurrentContextPath());
4562
return swaggerUiConfigParameters.getConfigParameters();
4663
}
4764

4865
@Override
4966
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
50-
if (!swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl()))
51-
swaggerUiConfigParameters.setOauth2RedirectUrl(uriComponentsBuilder.path(swaggerUiConfigParameters.getUiRootPath()).path(swaggerUiConfigParameters.getOauth2RedirectUrl()).build().toString());
67+
if (!swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl()) || springDocConfigProperties.isCacheDisabled())
68+
swaggerUiConfigParameters.setOauth2RedirectUrl(uriComponentsBuilder
69+
.path(swaggerUiConfigParameters.getUiRootPath())
70+
.path(getOauth2RedirectUrl()).build().toString());
5271
}
5372
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.ui.app5;
20+
21+
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.test.context.TestPropertySource;
26+
27+
import static org.hamcrest.CoreMatchers.equalTo;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
@TestPropertySource(properties = {"server.forward-headers-strategy=framework", "springdoc.cache.disabled=true"})
33+
public class SpringDocOauthRedirectUrlRecalculateTest extends AbstractSpringDocTest {
34+
35+
@Test
36+
public void oauth2_redirect_url_recalculation() throws Exception {
37+
mockMvc.perform(get("/v3/api-docs/swagger-config").header("X-Forwarded-Proto", "https").header("X-Forwarded-Host", "host1"))
38+
.andExpect(status().isOk())
39+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("https://host1/swagger-ui/oauth2-redirect.html")));
40+
41+
mockMvc.perform(get("/v3/api-docs/swagger-config").header("X-Forwarded-Proto", "http").header("X-Forwarded-Host", "host2:8080"))
42+
.andExpect(status().isOk())
43+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://host2:8080/swagger-ui/oauth2-redirect.html")));
44+
}
45+
46+
@SpringBootApplication
47+
static class SpringDocTestApp {
48+
}
49+
50+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ protected void calculateUiRootPath(StringBuilder... sbUrls) {
122122

123123
@Override
124124
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
125-
if (oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) {
125+
if ((oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) || springDocConfigProperties.isCacheDisabled()) {
126126
this.oauthPrefix = uriComponentsBuilder.path(managementServerProperties.getBasePath() + swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl);
127-
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(swaggerUiConfigParameters.getOauth2RedirectUrl()).build().toString());
127+
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(getOauth2RedirectUrl()).build().toString());
128128
}
129129
}
130130

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ protected void calculateUiRootPath(StringBuilder... sbUrls) {
144144

145145
@Override
146146
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
147-
if (oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) {
147+
if ((oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) || springDocConfigProperties.isCacheDisabled()) {
148148
this.oauthPrefix = uriComponentsBuilder.path(webfluxBasePath).path(swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl);
149-
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(swaggerUiConfigParameters.getOauth2RedirectUrl()).build().toString());
149+
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(getOauth2RedirectUrl()).build().toString());
150150
}
151151
}
152152
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.ui.app4;
20+
21+
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.test.context.TestPropertySource;
26+
27+
@TestPropertySource(properties = {"server.forward-headers-strategy=framework", "springdoc.cache.disabled=true"})
28+
public class SpringDocOauthRedirectUrlRecalculateTest extends AbstractSpringDocTest {
29+
30+
@Test
31+
public void oauth2_redirect_url_recalculation() throws Exception {
32+
33+
webTestClient.get().uri("/v3/api-docs/swagger-config")
34+
.header("X-Forwarded-Proto", "https")
35+
.header("X-Forwarded-Host", "host1")
36+
.exchange()
37+
.expectStatus().isOk()
38+
.expectBody()
39+
.jsonPath("$.oauth2RedirectUrl").isEqualTo("https://host1/webjars/swagger-ui/oauth2-redirect.html");
40+
41+
42+
webTestClient.get().uri("/v3/api-docs/swagger-config")
43+
.header("X-Forwarded-Proto", "http")
44+
.header("X-Forwarded-Host", "host2:8080")
45+
.exchange()
46+
.expectStatus().isOk()
47+
.expectBody()
48+
.jsonPath("$.oauth2RedirectUrl").isEqualTo("http://host2:8080/webjars/swagger-ui/oauth2-redirect.html");
49+
50+
}
51+
52+
@SpringBootApplication
53+
static class SpringDocTestApp {
54+
}
55+
56+
}

0 commit comments

Comments
 (0)