Skip to content

Commit 0a6340b

Browse files
committed
code review
1 parent 0886ef0 commit 0a6340b

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
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
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app5/SpringDocOauthRedirectUrlRecalculateTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
package test.org.springdoc.ui.app5;
2020

2121
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
2224
import org.springframework.boot.autoconfigure.SpringBootApplication;
2325
import org.springframework.test.context.TestPropertySource;
24-
import test.org.springdoc.ui.AbstractSpringDocTest;
2526

2627
import static org.hamcrest.CoreMatchers.equalTo;
2728
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

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
}

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/app4/SpringDocOauthRedirectUrlRecalculateTest.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,37 @@
1616
*
1717
*/
1818

19-
package test.org.springdoc.ui.app5;
19+
package test.org.springdoc.ui.app4;
2020

2121
import org.junit.jupiter.api.Test;
2222
import test.org.springdoc.ui.AbstractSpringDocTest;
2323

2424
import org.springframework.boot.autoconfigure.SpringBootApplication;
2525
import org.springframework.test.context.TestPropertySource;
2626

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-
3227
@TestPropertySource(properties = {"server.forward-headers-strategy=framework", "springdoc.cache.disabled=true"})
3328
public class SpringDocOauthRedirectUrlRecalculateTest extends AbstractSpringDocTest {
3429

3530
@Test
3631
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")));
4032

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")));
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+
4450
}
4551

4652
@SpringBootApplication

0 commit comments

Comments
 (0)