Skip to content

Commit 46dff5b

Browse files
author
bnasslahsen
committed
Prevent swagger-config from being loaded twice in case of no groups. Fixes #688, #349, #545.
1 parent 2f7c4c6 commit 46dff5b

File tree

6 files changed

+231
-7
lines changed

6 files changed

+231
-7
lines changed

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/SwaggerUiConfigProperties.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class SwaggerUiConfigProperties {
6161

6262
public static final String FILTER_PROPERTY = "filter";
6363

64+
public static final String URLS_PROPERTY = "urls";
65+
66+
public static final String OAUTH2_REDIRECT_URL_PROPERTY = "oauth2RedirectUrl";
6467
/**
6568
* The path for the Swagger UI pages to load. Will redirect to the springdoc.webjars.prefix property.
6669
*/
@@ -169,6 +172,10 @@ public class SwaggerUiConfigProperties {
169172

170173
private boolean disableSwaggerDefaultUrl;
171174

175+
private boolean displayQueryParams;
176+
177+
private boolean displayQueryParamsWithoutOauth2;
178+
172179
public void addGroup(String group) {
173180
SwaggerUrl swaggerUrl = new SwaggerUrl(group);
174181
urls.add(swaggerUrl);
@@ -212,7 +219,7 @@ public Map<String, Object> getConfigParameters() {
212219
SpringDocPropertiesUtils.put("supportedSubmitMethods", supportedSubmitMethods.toString(), params);
213220
SpringDocPropertiesUtils.put("oauth2RedirectUrl", oauth2RedirectUrl, params);
214221
SpringDocPropertiesUtils.put("url", url, params);
215-
put("urls", urls, params);
222+
put(URLS_PROPERTY, urls, params);
216223
SpringDocPropertiesUtils.put("urls.primaryName", urlsPrimaryName, params);
217224
return params;
218225
}
@@ -385,6 +392,22 @@ public void setDisableSwaggerDefaultUrl(boolean disableSwaggerDefaultUrl) {
385392
this.disableSwaggerDefaultUrl = disableSwaggerDefaultUrl;
386393
}
387394

395+
public boolean isDisplayQueryParams() {
396+
return displayQueryParams;
397+
}
398+
399+
public void setDisplayQueryParams(boolean displayQueryParams) {
400+
this.displayQueryParams = displayQueryParams;
401+
}
402+
403+
public boolean isDisplayQueryParamsWithoutOauth2() {
404+
return displayQueryParamsWithoutOauth2;
405+
}
406+
407+
public void setDisplayQueryParamsWithoutOauth2(boolean displayQueryParamsWithoutOauth2) {
408+
this.displayQueryParamsWithoutOauth2 = displayQueryParamsWithoutOauth2;
409+
}
410+
388411
public boolean isValidUrl(String url) {
389412
try {
390413
new URL(url).toURI();

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

+21-6
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,27 @@ protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriCompon
8686

8787
protected UriComponentsBuilder getUriComponentsBuilder(String sbUrl) {
8888
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl);
89-
uriBuilder.queryParam(SwaggerUiConfigProperties.CONFIG_URL_PROPERTY, swaggerUiConfig.getConfigUrl());
90-
if (StringUtils.isNotEmpty(swaggerUiConfig.getLayout()))
91-
uriBuilder.queryParam(SwaggerUiConfigProperties.LAYOUT_PROPERTY, swaggerUiConfig.getLayout());
92-
if (StringUtils.isNotEmpty(swaggerUiConfig.getFilter()))
93-
uriBuilder.queryParam(SwaggerUiConfigProperties.FILTER_PROPERTY, swaggerUiConfig.getFilter());
94-
89+
if (swaggerUiConfig.isDisplayQueryParams() && StringUtils.isNotEmpty(swaggerUiConfig.getUrl())) {
90+
swaggerUiConfig.getConfigParameters().entrySet().stream()
91+
.filter(entry -> !SwaggerUiConfigProperties.CONFIG_URL_PROPERTY.equals(entry.getKey()))
92+
.filter(entry -> !entry.getKey().startsWith(SwaggerUiConfigProperties.URLS_PROPERTY))
93+
.filter(entry -> StringUtils.isNotEmpty((String) entry.getValue()))
94+
.forEach(entry -> uriBuilder.queryParam(entry.getKey(), entry.getValue()));
95+
} else if (swaggerUiConfig.isDisplayQueryParamsWithoutOauth2() && StringUtils.isNotEmpty(swaggerUiConfig.getUrl())) {
96+
swaggerUiConfig.getConfigParameters().entrySet().stream()
97+
.filter(entry -> !SwaggerUiConfigProperties.CONFIG_URL_PROPERTY.equals(entry.getKey()))
98+
.filter(entry -> !SwaggerUiConfigProperties.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()))
99+
.filter(entry -> !entry.getKey().startsWith(SwaggerUiConfigProperties.URLS_PROPERTY))
100+
.filter(entry -> StringUtils.isNotEmpty((String) entry.getValue()))
101+
.forEach(entry -> uriBuilder.queryParam(entry.getKey(), entry.getValue()));
102+
}
103+
else {
104+
uriBuilder.queryParam(SwaggerUiConfigProperties.CONFIG_URL_PROPERTY, swaggerUiConfig.getConfigUrl());
105+
if (StringUtils.isNotEmpty(swaggerUiConfig.getLayout()))
106+
uriBuilder.queryParam(SwaggerUiConfigProperties.LAYOUT_PROPERTY, swaggerUiConfig.getLayout());
107+
if (StringUtils.isNotEmpty(swaggerUiConfig.getFilter()))
108+
uriBuilder.queryParam(SwaggerUiConfigProperties.FILTER_PROPERTY, swaggerUiConfig.getFilter());
109+
}
95110
return uriBuilder;
96111
}
97112

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.app1;
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+
import org.springframework.test.web.servlet.MvcResult;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params-without-oauth2=true")
33+
public class SpringDocRedirectQueryParams1Test extends AbstractSpringDocTest {
34+
35+
@Test
36+
public void shouldRedirectWithQueryParamsWithoutOauth2() throws Exception {
37+
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38+
.andExpect(status().isFound()).andReturn();
39+
40+
String locationHeader = mvcResult.getResponse().getHeader("Location");
41+
assertEquals("/swagger-ui/index.html?url=/v3/api-docs", locationHeader);
42+
}
43+
44+
@SpringBootApplication
45+
static class SpringDocTestApp {}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.app1;
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+
import org.springframework.test.web.servlet.MvcResult;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params=true")
33+
public class SpringDocRedirectQueryParams2Test extends AbstractSpringDocTest {
34+
35+
@Test
36+
public void shouldRedirectWithQueryParams() throws Exception {
37+
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38+
.andExpect(status().isFound()).andReturn();
39+
40+
String locationHeader = mvcResult.getResponse().getHeader("Location");
41+
assertEquals("/swagger-ui/index.html?oauth2RedirectUrl=http://localhost/swagger-ui/oauth2-redirect.html&url=/v3/api-docs", locationHeader);
42+
}
43+
44+
@SpringBootApplication
45+
static class SpringDocTestApp {}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.app1;
20+
21+
import org.hamcrest.Matchers;
22+
import org.junit.jupiter.api.Test;
23+
import test.org.springdoc.ui.AbstractSpringDocTest;
24+
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.test.context.TestPropertySource;
27+
import org.springframework.test.web.reactive.server.WebTestClient;
28+
29+
30+
@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params-without-oauth2=true")
31+
public class SpringDocApp1RedirectQueryParams1Test extends AbstractSpringDocTest {
32+
33+
@Test
34+
public void shouldRedirectWithQueryParamsWithoutOauth2() throws Exception {
35+
36+
WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
37+
.expectStatus().isTemporaryRedirect();
38+
responseSpec.expectHeader()
39+
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?url=/v3/api-docs"));
40+
41+
}
42+
43+
@SpringBootApplication
44+
static class SpringDocTestApp {}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.app1;
20+
21+
import org.hamcrest.Matchers;
22+
import org.junit.jupiter.api.Test;
23+
import test.org.springdoc.ui.AbstractSpringDocTest;
24+
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.test.context.TestPropertySource;
27+
import org.springframework.test.web.reactive.server.WebTestClient;
28+
29+
30+
@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params=true")
31+
public class SpringDocApp1RedirectQueryParams2Test extends AbstractSpringDocTest {
32+
33+
@Test
34+
public void shouldRedirectWithQueryParams() throws Exception {
35+
36+
WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
37+
.expectStatus().isTemporaryRedirect();
38+
responseSpec.expectHeader()
39+
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?oauth2RedirectUrl=/webjars/swagger-ui/oauth2-redirect.html&url=/v3/api-docs"));
40+
41+
}
42+
43+
@SpringBootApplication
44+
static class SpringDocTestApp {}
45+
46+
}

0 commit comments

Comments
 (0)