Skip to content

Commit

Permalink
Merge branch 'Daimler-bugfix/respect_cache_disabled'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Mar 15, 2021
2 parents 5c7f784 + 0a6340b commit f505a5d
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.util.CollectionUtils;
import org.springframework.web.util.UriComponentsBuilder;

import static org.springdoc.core.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;


Expand Down Expand Up @@ -205,4 +206,16 @@ protected String buildApiDocUrl(String contextPath) {
protected String buildSwaggerConfigUrl(String contextPath) {
return this.swaggerConfigUrl;
}

/**
* Gets oauth2 redirect url.
*
* @return the oauth2 redirect url
*/
protected String getOauth2RedirectUrl() {
if (springDocConfigProperties.isCacheDisabled())
return StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_OAUTH_REDIRECT_URL);
else
return swaggerUiConfigParameters.getOauth2RedirectUrl();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,28 @@

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

/**
* The type Swagger welcome common.
* @author bnasslashen
*/
public abstract class SwaggerWelcomeCommon extends AbstractSwaggerWelcome {
/**
* Instantiates a new Abstract swagger welcome.
* @param swaggerUiConfig the swagger ui config
* @param swaggerUiConfig the swagger ui config
* @param springDocConfigProperties the spring doc config properties
* @param swaggerUiConfigParameters the swagger ui config parameters
*/
public SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters) {
public SwaggerWelcomeCommon(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties,
SwaggerUiConfigParameters swaggerUiConfigParameters) {
super(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters);
}

/**
* Redirect to ui response entity.
*
* @param request the request
* @return the response entity
*/
protected ResponseEntity<Void> redirectToUi(HttpServletRequest request) {
buildConfigUrl(request.getContextPath(), ServletUriComponentsBuilder.fromCurrentContextPath());
String sbUrl = request.getContextPath() + swaggerUiConfigParameters.getUiRootPath() + SWAGGER_UI_URL;
Expand All @@ -40,14 +51,22 @@ protected ResponseEntity<Void> redirectToUi(HttpServletRequest request) {
.build();
}

/**
* Openapi json map.
*
* @param request the request
* @return the map
*/
protected Map<String, Object> openapiJson(HttpServletRequest request) {
buildConfigUrl(request.getContextPath(), ServletUriComponentsBuilder.fromCurrentContextPath());
return swaggerUiConfigParameters.getConfigParameters();
}

@Override
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
if (!swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl()))
swaggerUiConfigParameters.setOauth2RedirectUrl(uriComponentsBuilder.path(swaggerUiConfigParameters.getUiRootPath()).path(swaggerUiConfigParameters.getOauth2RedirectUrl()).build().toString());
if (!swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl()) || springDocConfigProperties.isCacheDisabled())
swaggerUiConfigParameters.setOauth2RedirectUrl(uriComponentsBuilder
.path(swaggerUiConfigParameters.getUiRootPath())
.path(getOauth2RedirectUrl()).build().toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.ui.app5;

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

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@TestPropertySource(properties = {"server.forward-headers-strategy=framework", "springdoc.cache.disabled=true"})
public class SpringDocOauthRedirectUrlRecalculateTest extends AbstractSpringDocTest {

@Test
public void oauth2_redirect_url_recalculation() throws Exception {
mockMvc.perform(get("/v3/api-docs/swagger-config").header("X-Forwarded-Proto", "https").header("X-Forwarded-Host", "host1"))
.andExpect(status().isOk())
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("https://host1/swagger-ui/oauth2-redirect.html")));

mockMvc.perform(get("/v3/api-docs/swagger-config").header("X-Forwarded-Proto", "http").header("X-Forwarded-Host", "host2:8080"))
.andExpect(status().isOk())
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://host2:8080/swagger-ui/oauth2-redirect.html")));
}

@SpringBootApplication
static class SpringDocTestApp {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ protected void calculateUiRootPath(StringBuilder... sbUrls) {

@Override
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
if (oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) {
if ((oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) || springDocConfigProperties.isCacheDisabled()) {
this.oauthPrefix = uriComponentsBuilder.path(managementServerProperties.getBasePath() + swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl);
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(swaggerUiConfigParameters.getOauth2RedirectUrl()).build().toString());
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(getOauth2RedirectUrl()).build().toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ protected void calculateUiRootPath(StringBuilder... sbUrls) {

@Override
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
if (oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) {
if ((oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) || springDocConfigProperties.isCacheDisabled()) {
this.oauthPrefix = uriComponentsBuilder.path(webfluxBasePath).path(swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl);
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(swaggerUiConfigParameters.getOauth2RedirectUrl()).build().toString());
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(getOauth2RedirectUrl()).build().toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* * Copyright 2019-2020 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.ui.app4;

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

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;

@TestPropertySource(properties = {"server.forward-headers-strategy=framework", "springdoc.cache.disabled=true"})
public class SpringDocOauthRedirectUrlRecalculateTest extends AbstractSpringDocTest {

@Test
public void oauth2_redirect_url_recalculation() throws Exception {

webTestClient.get().uri("/v3/api-docs/swagger-config")
.header("X-Forwarded-Proto", "https")
.header("X-Forwarded-Host", "host1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.oauth2RedirectUrl").isEqualTo("https://host1/webjars/swagger-ui/oauth2-redirect.html");


webTestClient.get().uri("/v3/api-docs/swagger-config")
.header("X-Forwarded-Proto", "http")
.header("X-Forwarded-Host", "host2:8080")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.oauth2RedirectUrl").isEqualTo("http://host2:8080/webjars/swagger-ui/oauth2-redirect.html");

}

@SpringBootApplication
static class SpringDocTestApp {
}

}

0 comments on commit f505a5d

Please sign in to comment.