Skip to content

Commit 9969216

Browse files
fixes #192
1 parent 1cf546e commit 9969216

File tree

6 files changed

+249
-4
lines changed

6 files changed

+249
-4
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package org.springdoc.core;
2+
3+
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
import java.util.Map;
9+
import java.util.TreeMap;
10+
11+
/**
12+
* Please refer to https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
13+
* to get the idea what each parameter does.
14+
*/
15+
@Configuration
16+
@ConfigurationProperties(prefix = "springdoc.swagger-ui")
17+
public class SwaggerUiConfig {
18+
// URL to fetch external configuration document from.
19+
private String configUrl;
20+
// The url pointing to API definition (normally
21+
// swagger.json/swagger.yaml/openapi.json/openapi.yaml).
22+
private String url;
23+
// If set, enables filtering. The top bar will show an edit box that
24+
// could be used to filter the tagged operations that are shown.
25+
private String filter;
26+
27+
// Enables or disables deep linking for tags and operations.
28+
private Boolean deepLinking;
29+
// Controls the display of operationId in operations list.
30+
private Boolean displayOperationId;
31+
// The default expansion depth for models (set to -1 completely hide the models).
32+
private Integer defaultModelsExpandDepth;
33+
// The default expansion depth for the model on the model-example section.
34+
private Integer defaultModelExpandDepth;
35+
36+
// Controls how the model is shown when the API is first rendered.
37+
private String defaultModelRendering;
38+
// Controls the display of the request duration (in milliseconds) for Try-It-Out requests.
39+
private Boolean displayRequestDuration;
40+
// Controls the default expansion setting for the operations and tags.
41+
private String docExpansion;
42+
// If set, limits the number of tagged operations displayed to at most this many.
43+
private Integer maxDisplayedTags;
44+
// Controls the display of vendor extension (x-) fields and values.
45+
private Boolean showExtensions;
46+
// Controls the display of extensions
47+
private Boolean showCommonExtensions;
48+
// Set a different validator URL, for example for locally deployed validators
49+
private String validatorUrl;
50+
51+
public Map<String, String> getConfigParameters() {
52+
final Map<String, String> params = new TreeMap<>();
53+
put("url", url, params);
54+
put("configUrl", configUrl, params);
55+
put("filter", filter, params);
56+
put("deepLinking", this.deepLinking, params);
57+
put("displayOperationId", displayOperationId, params);
58+
put("defaultModelsExpandDepth", defaultModelsExpandDepth, params);
59+
put("defaultModelExpandDepth", defaultModelExpandDepth, params);
60+
put("defaultModelRendering", defaultModelRendering, params);
61+
put("displayRequestDuration", displayRequestDuration, params);
62+
put("docExpansion", docExpansion, params);
63+
put("maxDisplayedTags", maxDisplayedTags, params);
64+
put("showExtensions", showExtensions, params);
65+
put("showCommonExtensions", showCommonExtensions, params);
66+
put("validatorUrl", validatorUrl, params);
67+
return params;
68+
}
69+
70+
protected void put(final String name, final Integer value, final Map<String, String> params) {
71+
if (value != null) {
72+
params.put(name, value.toString());
73+
}
74+
}
75+
76+
protected void put(final String name, final Boolean value, final Map<String, String> params) {
77+
if (value != null) {
78+
params.put(name, value.toString());
79+
}
80+
}
81+
82+
protected void put(final String name, final String value, final Map<String, String> params) {
83+
if (!StringUtils.isEmpty(value)) {
84+
params.put(name, value);
85+
}
86+
}
87+
88+
public String getConfigUrl() {
89+
return configUrl;
90+
}
91+
92+
public void setConfigUrl(String configUrl) {
93+
this.configUrl = configUrl;
94+
}
95+
96+
public String getUrl() {
97+
return url;
98+
}
99+
100+
public void setUrl(String url) {
101+
this.url = url;
102+
}
103+
104+
public String getFilter() {
105+
return filter;
106+
}
107+
108+
public void setFilter(String filter) {
109+
this.filter = filter;
110+
}
111+
112+
public Boolean getDeepLinking() {
113+
return deepLinking;
114+
}
115+
116+
public void setDeepLinking(Boolean deepLinking) {
117+
this.deepLinking = deepLinking;
118+
}
119+
120+
public Boolean getDisplayOperationId() {
121+
return displayOperationId;
122+
}
123+
124+
public void setDisplayOperationId(Boolean displayOperationId) {
125+
this.displayOperationId = displayOperationId;
126+
}
127+
128+
public Integer getDefaultModelsExpandDepth() {
129+
return defaultModelsExpandDepth;
130+
}
131+
132+
public void setDefaultModelsExpandDepth(Integer defaultModelsExpandDepth) {
133+
this.defaultModelsExpandDepth = defaultModelsExpandDepth;
134+
}
135+
136+
public Integer getDefaultModelExpandDepth() {
137+
return defaultModelExpandDepth;
138+
}
139+
140+
public void setDefaultModelExpandDepth(Integer defaultModelExpandDepth) {
141+
this.defaultModelExpandDepth = defaultModelExpandDepth;
142+
}
143+
144+
public String getDefaultModelRendering() {
145+
return defaultModelRendering;
146+
}
147+
148+
public void setDefaultModelRendering(String defaultModelRendering) {
149+
this.defaultModelRendering = defaultModelRendering;
150+
}
151+
152+
public Boolean getDisplayRequestDuration() {
153+
return displayRequestDuration;
154+
}
155+
156+
public void setDisplayRequestDuration(Boolean displayRequestDuration) {
157+
this.displayRequestDuration = displayRequestDuration;
158+
}
159+
160+
public String getDocExpansion() {
161+
return docExpansion;
162+
}
163+
164+
public void setDocExpansion(String docExpansion) {
165+
this.docExpansion = docExpansion;
166+
}
167+
168+
public Integer getMaxDisplayedTags() {
169+
return maxDisplayedTags;
170+
}
171+
172+
public void setMaxDisplayedTags(Integer maxDisplayedTags) {
173+
this.maxDisplayedTags = maxDisplayedTags;
174+
}
175+
176+
public Boolean getShowExtensions() {
177+
return showExtensions;
178+
}
179+
180+
public void setShowExtensions(Boolean showExtensions) {
181+
this.showExtensions = showExtensions;
182+
}
183+
184+
public Boolean getShowCommonExtensions() {
185+
return showCommonExtensions;
186+
}
187+
188+
public void setShowCommonExtensions(Boolean showCommonExtensions) {
189+
this.showCommonExtensions = showCommonExtensions;
190+
}
191+
192+
public String getValidatorUrl() {
193+
return validatorUrl;
194+
}
195+
196+
public void setValidatorUrl(String validatorUrl) {
197+
this.validatorUrl = validatorUrl;
198+
}
199+
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.springdoc.ui;
22

33
import io.swagger.v3.oas.annotations.Operation;
4+
import org.springdoc.core.SwaggerUiConfig;
5+
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.beans.factory.annotation.Value;
57
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
68
import org.springframework.stereotype.Controller;
79
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.util.UriComponentsBuilder;
811

912
import javax.servlet.http.HttpServletRequest;
13+
import java.util.Map;
1014

1115
import static org.springdoc.core.Constants.*;
1216
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
@@ -22,6 +26,9 @@ class SwaggerWelcome {
2226
@Value(SWAGGER_UI_PATH)
2327
private String swaggerPath;
2428

29+
@Autowired
30+
private SwaggerUiConfig swaggerUiConfig;
31+
2532
@Operation(hidden = true)
2633
@GetMapping(SWAGGER_UI_PATH)
2734
public String redirectToUi(HttpServletRequest request) {
@@ -41,6 +48,18 @@ public String redirectToUi(HttpServletRequest request) {
4148
sbUrl.append(contextPath).append(apiDocsUrl);
4249
}
4350
sbUrl.append(DEFAULT_VALIDATOR_URL);
44-
return sbUrl.toString();
51+
52+
final Map<String, String> params = swaggerUiConfig.getConfigParameters();
53+
54+
final UriComponentsBuilder builder = params
55+
.entrySet()
56+
.stream()
57+
.reduce(
58+
UriComponentsBuilder
59+
.fromUriString(sbUrl.toString()),
60+
(b, e) -> b.queryParam(e.getKey(), e.getValue()),
61+
(left, right) -> left);
62+
63+
return builder.build().encode().toString();
4564
}
4665
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.springdoc.ui;
22

3+
import org.springdoc.core.SwaggerUiConfig;
4+
import org.springframework.beans.factory.annotation.Autowired;
35
import org.springframework.beans.factory.annotation.Value;
46
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
57
import org.springframework.context.annotation.Bean;
68
import org.springframework.stereotype.Controller;
79
import org.springframework.web.reactive.function.server.RouterFunction;
810
import org.springframework.web.reactive.function.server.ServerResponse;
11+
import org.springframework.web.util.UriComponentsBuilder;
912

1013
import java.net.URI;
14+
import java.util.Map;
1115

1216
import static org.springdoc.core.Constants.*;
1317
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
@@ -25,6 +29,9 @@ public class SwaggerWelcome {
2529
@Value(WEB_JARS_PREFIX_URL)
2630
private String webJarsPrefixUrl;
2731

32+
@Autowired
33+
private SwaggerUiConfig swaggerUiConfig;
34+
2835
@Bean
2936
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
3037
RouterFunction<ServerResponse> routerFunction() {
@@ -33,7 +40,20 @@ RouterFunction<ServerResponse> routerFunction() {
3340
apiDocsUrl +
3441
DEFAULT_VALIDATOR_URL;
3542

43+
final Map<String, String> params = swaggerUiConfig.getConfigParameters();
44+
45+
46+
final UriComponentsBuilder builder = params
47+
.entrySet()
48+
.stream()
49+
.reduce(
50+
UriComponentsBuilder
51+
.fromUriString(url.toString()),
52+
(b, e) -> b.queryParam(e.getKey(), e.getValue()),
53+
(left, right) -> left);
54+
55+
3656
return route(GET(uiPath),
37-
req -> ServerResponse.temporaryRedirect(URI.create(url)).build());
57+
req -> ServerResponse.temporaryRedirect(URI.create(builder.build().encode().toString())).build());
3858
}
3959
}

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocApp1Test.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
6+
import org.springdoc.core.SwaggerUiConfig;
67
import org.springdoc.ui.SwaggerWelcome;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
@@ -14,7 +15,7 @@
1415
@RunWith(SpringRunner.class)
1516
@WebFluxTest
1617
@ActiveProfiles("test")
17-
@ContextConfiguration(classes = {SwaggerWelcome.class})
18+
@ContextConfiguration(classes = {SwaggerWelcome.class,SwaggerUiConfig.class})
1819
public class SpringDocApp1Test {
1920

2021
@Autowired

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocTestApp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package test.org.springdoc.ui.app1;
22

3+
import org.springdoc.core.SwaggerUiConfig;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
57

68
@SpringBootApplication
79
public class SpringDocTestApp {

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/app2/SpringDocApp2Test.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
6+
import org.springdoc.core.SwaggerUiConfig;
67
import org.springdoc.ui.SwaggerWelcome;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
@@ -14,7 +15,7 @@
1415
@RunWith(SpringRunner.class)
1516
@WebFluxTest(properties = "springdoc.swagger-ui.enabled=false")
1617
@ActiveProfiles("test")
17-
@ContextConfiguration(classes = {SwaggerWelcome.class})
18+
@ContextConfiguration(classes = {SwaggerWelcome.class,SwaggerUiConfig.class})
1819
public class SpringDocApp2Test {
1920

2021
@Autowired
@@ -26,7 +27,10 @@ public class SpringDocApp2Test {
2627
@Test
2728
public void shouldDisplaySwaggerUiPage() throws Exception {
2829
webTestClient.get().uri("/swagger-ui.html").exchange()
30+
.expectStatus().isTemporaryRedirect();
31+
webTestClient.get().uri("/persons?name=toto").exchange()
2932
.expectStatus().isNotFound();
33+
3034
}
3135

3236

0 commit comments

Comments
 (0)