Skip to content

Commit 2514d9b

Browse files
author
bnasslahsen
committed
workaround for swagger-ui, layout and filter properties. Fixes #536 #424
1 parent eb9536a commit 2514d9b

File tree

9 files changed

+205
-9
lines changed

9 files changed

+205
-9
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocPropertiesUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void put(final String name, final Boolean value, final Map<String, Object
4646
}
4747

4848
static void put(final String name, final String value, final Map<String, Object> params) {
49-
if (!StringUtils.isEmpty(value)) {
49+
if (StringUtils.isNotEmpty(value)) {
5050
params.put(name, value);
5151
}
5252
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
public class SwaggerUiConfigProperties {
5454

5555
public static final String CONFIG_URL_PROPERTY = "configUrl";
56-
56+
public static final String LAYOUT_PROPERTY = "layout";
57+
public static final String FILTER_PROPERTY = "filter";
5758
/**
5859
* The path for the Swagger UI pages to load. Will redirect to the springdoc.webjars.prefix property.
5960
*/
@@ -182,11 +183,9 @@ public void addUrl(String url) {
182183

183184
public Map<String, Object> getConfigParameters() {
184185
final Map<String, Object> params = new TreeMap<>();
185-
SpringDocPropertiesUtils.put("layout", layout, params);
186-
SpringDocPropertiesUtils.put(CONFIG_URL_PROPERTY, configUrl, params);
187186
// empty-string prevents swagger-ui default validation
188187
params.put("validatorUrl", validatorUrl != null ? validatorUrl : "");
189-
SpringDocPropertiesUtils.put("filter", filter, params);
188+
SpringDocPropertiesUtils.put(CONFIG_URL_PROPERTY, configUrl, params);
190189
SpringDocPropertiesUtils.put("deepLinking", this.deepLinking, params);
191190
SpringDocPropertiesUtils.put("displayOperationId", displayOperationId, params);
192191
SpringDocPropertiesUtils.put("defaultModelsExpandDepth", defaultModelsExpandDepth, params);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriCompon
8686
calculateOauth2RedirectUrl(uriComponentsBuilder);
8787
}
8888

89+
protected UriComponentsBuilder getUriComponentsBuilder(String sbUrl) {
90+
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl);
91+
uriBuilder.queryParam(SwaggerUiConfigProperties.CONFIG_URL_PROPERTY, swaggerUiConfig.getConfigUrl());
92+
if(StringUtils.isNotEmpty(swaggerUiConfig.getLayout()))
93+
uriBuilder.queryParam(SwaggerUiConfigProperties.LAYOUT_PROPERTY, swaggerUiConfig.getLayout());
94+
if(StringUtils.isNotEmpty(swaggerUiConfig.getFilter()))
95+
uriBuilder.queryParam(SwaggerUiConfigProperties.FILTER_PROPERTY, swaggerUiConfig.getFilter());
96+
97+
return uriBuilder;
98+
}
99+
89100
abstract void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder);
90101

91102
abstract void calculateUiRootPath(StringBuilder... sbUrls);

springdoc-openapi-ui/src/main/java/org/springdoc/ui/SwaggerWelcome.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public SwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfig
6565
public String redirectToUi(HttpServletRequest request) {
6666
buildConfigUrl(request.getContextPath(), ServletUriComponentsBuilder.fromCurrentContextPath());
6767
String sbUrl = REDIRECT_URL_PREFIX + this.uiRootPath + SWAGGER_UI_URL;
68-
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl);
69-
return uriBuilder.queryParam(SwaggerUiConfigProperties.CONFIG_URL_PROPERTY, swaggerUiConfig.getConfigUrl()).build().encode().toString();
68+
UriComponentsBuilder uriBuilder = getUriComponentsBuilder(sbUrl);
69+
return uriBuilder.build().encode().toString();
7070
}
7171

7272
@Operation(hidden = true)
Lines changed: 47 additions & 0 deletions
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.filter=false")
33+
public class SpringDocRedirectFilterTest extends AbstractSpringDocTest {
34+
35+
@Test
36+
public void shouldRedirectWithConfigUrlIgnoringQueryParams() 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?configUrl=/v3/api-docs/swagger-config&filter=false", locationHeader);
42+
}
43+
44+
@SpringBootApplication
45+
static class SpringDocTestApp {}
46+
47+
}
Lines changed: 47 additions & 0 deletions
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.layout=BaseLayout")
33+
public class SpringDocRedirectLayoutTest extends AbstractSpringDocTest {
34+
35+
@Test
36+
public void shouldRedirectWithConfigUrlIgnoringQueryParams() 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?configUrl=/v3/api-docs/swagger-config&layout=BaseLayout", locationHeader);
42+
}
43+
44+
@SpringBootApplication
45+
static class SpringDocTestApp {}
46+
47+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ public SwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfig
6666
public Mono<Void> redirectToUi(ServerHttpRequest request, ServerHttpResponse response) {
6767
String contextPath = this.fromCurrentContextPath(request);
6868
String sbUrl = this.buildUrl(contextPath, this.uiRootPath + springDocConfigProperties.getWebjars().getPrefix() + SWAGGER_UI_URL);
69-
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl);
70-
uriBuilder.queryParam(SwaggerUiConfigProperties.CONFIG_URL_PROPERTY, swaggerUiConfig.getConfigUrl());
69+
UriComponentsBuilder uriBuilder = getUriComponentsBuilder(sbUrl);
7170
response.setStatusCode(HttpStatus.TEMPORARY_REDIRECT);
7271
response.getHeaders().setLocation(URI.create(uriBuilder.build().encode().toString()));
7372
return response.setComplete();
7473
}
7574

75+
7676
@Operation(hidden = true)
7777
@GetMapping(value = SWAGGER_CONFIG_URL, produces = MediaType.APPLICATION_JSON_VALUE)
7878
@ResponseBody
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.filter=false")
31+
public class SpringDocApp1RedirecFilterTest extends AbstractSpringDocTest {
32+
33+
@Test
34+
public void shouldRedirectWithConfigUrlIgnoringQueryParams() 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?configUrl=/v3/api-docs/swagger-config&filter=false"));
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.layout=BaseLayout")
31+
public class SpringDocApp1RedirectLayoutTest extends AbstractSpringDocTest {
32+
33+
@Test
34+
public void shouldRedirectWithConfigUrlIgnoringQueryParams() 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?configUrl=/v3/api-docs/swagger-config&layout=BaseLayout"));
40+
41+
}
42+
43+
@SpringBootApplication
44+
static class SpringDocTestApp {}
45+
46+
}

0 commit comments

Comments
 (0)