Skip to content

Commit c9f7cef

Browse files
committed
Enable support for Swagger-UI 4.1.3 Release. Enable support for queryConfigEnabled. Fixes #1379.
Disable reading config params from URL search params by default.
1 parent 06b6e12 commit c9f7cef

File tree

66 files changed

+1575
-256
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1575
-256
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ public abstract class AbstractSwaggerUiConfigProperties {
170170
*/
171171
protected Boolean persistAuthorization;
172172

173+
/**
174+
* The Query config enabled.
175+
*/
176+
protected Boolean queryConfigEnabled;
177+
178+
/**
179+
* Gets query config enabled.
180+
*
181+
* @return the query config enabled
182+
*/
183+
public Boolean getQueryConfigEnabled() {
184+
return queryConfigEnabled;
185+
}
186+
187+
/**
188+
* Sets query config enabled.
189+
*
190+
* @param queryConfigEnabled the query config enabled
191+
*/
192+
public void setQueryConfigEnabled(Boolean queryConfigEnabled) {
193+
this.queryConfigEnabled = queryConfigEnabled;
194+
}
195+
173196
/**
174197
* Gets try it out enabled
175198
* @return try it out enabled

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@ public final class Constants {
170170
*/
171171
public static final String CLASSPATH_RESOURCE_LOCATION = ResourceUtils.CLASSPATH_URL_PREFIX + "/META-INF/resources";
172172

173-
/**
174-
* The constant SPRINGDOC_CONFIG_FILE.
175-
*/
176-
public static final String SPRINGDOC_CONFIG_FILE = ResourceUtils.CLASSPATH_URL_PREFIX + "springdoc.swagger-ui.config";
177173

178174
/**
179175
* The constant SWAGGER_UI_PREFIX.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public class SwaggerUiConfigParameters extends AbstractSwaggerUiConfigProperties
6666
*/
6767
public static final String FILTER_PROPERTY = "filter";
6868

69+
/**
70+
* The constant URL_PROPERTY.
71+
*/
72+
public static final String URL_PROPERTY = "url";
73+
6974
/**
7075
* The constant URLS_PROPERTY.
7176
*/
@@ -120,6 +125,7 @@ public SwaggerUiConfigParameters(SwaggerUiConfigProperties swaggerUiConfig) {
120125
this.syntaxHighlight = swaggerUiConfig.getSyntaxHighlight();
121126
this.tryItOutEnabled = swaggerUiConfig.getTryItOutEnabled();
122127
this.persistAuthorization = swaggerUiConfig.getPersistAuthorization();
128+
this.queryConfigEnabled = swaggerUiConfig.getQueryConfigEnabled();
123129
}
124130

125131
/**
@@ -202,14 +208,16 @@ public Map<String, Object> getConfigParameters() {
202208
SpringDocPropertiesUtils.put("showCommonExtensions", showCommonExtensions, params);
203209
SpringDocPropertiesUtils.put("operationsSorter", operationsSorter, params);
204210
SpringDocPropertiesUtils.put("tagsSorter", tagsSorter, params);
205-
if (supportedSubmitMethods!=null)
211+
SpringDocPropertiesUtils.put(SwaggerUiConfigParameters.LAYOUT_PROPERTY, layout , params);
212+
if (supportedSubmitMethods != null)
206213
SpringDocPropertiesUtils.put("supportedSubmitMethods", supportedSubmitMethods.toString(), params);
207214
SpringDocPropertiesUtils.put(OAUTH2_REDIRECT_URL_PROPERTY, oauth2RedirectUrl, params);
208-
SpringDocPropertiesUtils.put("url", url, params);
215+
SpringDocPropertiesUtils.put(URL_PROPERTY, url, params);
209216
put(URLS_PROPERTY, urls, params);
210217
SpringDocPropertiesUtils.put("urls.primaryName", urlsPrimaryName, params);
211218
SpringDocPropertiesUtils.put("tryItOutEnabled", tryItOutEnabled, params);
212219
SpringDocPropertiesUtils.put("persistAuthorization", persistAuthorization, params);
220+
SpringDocPropertiesUtils.put(FILTER_PROPERTY, filter, params);
213221
return params;
214222
}
215223

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

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
package org.springdoc.core;
2222

23+
import org.apache.commons.lang3.StringUtils;
24+
2325
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2426
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2527
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -38,22 +40,13 @@
3840
@ConfigurationProperties(prefix = "springdoc.swagger-ui")
3941
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
4042
@ConditionalOnBean(SpringDocConfiguration.class)
41-
public class SwaggerUiConfigProperties extends AbstractSwaggerUiConfigProperties{
43+
public class SwaggerUiConfigProperties extends AbstractSwaggerUiConfigProperties {
4244

4345
/**
4446
* The Disable swagger default url.
4547
*/
4648
private boolean disableSwaggerDefaultUrl;
4749

48-
/**
49-
* The Display query params.
50-
*/
51-
private boolean displayQueryParams;
52-
53-
/**
54-
* The Display query params without oauth 2.
55-
*/
56-
private boolean displayQueryParamsWithoutOauth2;
5750

5851
/**
5952
* The Swagger ui version.
@@ -273,42 +266,6 @@ public void setDisableSwaggerDefaultUrl(boolean disableSwaggerDefaultUrl) {
273266
this.disableSwaggerDefaultUrl = disableSwaggerDefaultUrl;
274267
}
275268

276-
/**
277-
* Is display query params boolean.
278-
*
279-
* @return the boolean
280-
*/
281-
public boolean isDisplayQueryParams() {
282-
return displayQueryParams;
283-
}
284-
285-
/**
286-
* Sets display query params.
287-
*
288-
* @param displayQueryParams the display query params
289-
*/
290-
public void setDisplayQueryParams(boolean displayQueryParams) {
291-
this.displayQueryParams = displayQueryParams;
292-
}
293-
294-
/**
295-
* Is display query params without oauth 2 boolean.
296-
*
297-
* @return the boolean
298-
*/
299-
public boolean isDisplayQueryParamsWithoutOauth2() {
300-
return displayQueryParamsWithoutOauth2;
301-
}
302-
303-
/**
304-
* Sets display query params without oauth 2.
305-
*
306-
* @param displayQueryParamsWithoutOauth2 the display query params without oauth 2
307-
*/
308-
public void setDisplayQueryParamsWithoutOauth2(boolean displayQueryParamsWithoutOauth2) {
309-
this.displayQueryParamsWithoutOauth2 = displayQueryParamsWithoutOauth2;
310-
}
311-
312269
/**
313270
* Gets csrf.
314271
*
@@ -332,15 +289,15 @@ public void setCsrf(Csrf csrf) {
332289
*
333290
* @return the boolean
334291
*/
335-
public boolean isCsrfEnabled(){
292+
public boolean isCsrfEnabled() {
336293
return csrf.isEnabled();
337294
}
338295

339296
/**
340297
* The type Syntax highlight.
341298
* @author bnasslashen
342299
*/
343-
public static class SyntaxHighlight{
300+
public static class SyntaxHighlight {
344301

345302
/**
346303
* The Activated.
@@ -387,6 +344,15 @@ public String getTheme() {
387344
public void setTheme(String theme) {
388345
this.theme = theme;
389346
}
347+
348+
/**
349+
* Is present boolean.
350+
*
351+
* @return the boolean
352+
*/
353+
public boolean isPresent() {
354+
return activated != null || StringUtils.isNotEmpty(theme);
355+
}
390356
}
391357

392358
/**

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

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.nio.charset.StandardCharsets;
27+
import java.util.Map;
28+
import java.util.stream.Collectors;
2729

2830
import com.fasterxml.jackson.core.JsonProcessingException;
2931
import com.fasterxml.jackson.databind.ObjectMapper;
3032
import org.apache.commons.lang3.StringUtils;
3133
import org.springdoc.core.Constants;
34+
import org.springdoc.core.SwaggerUiConfigParameters;
3235
import org.springdoc.core.SwaggerUiConfigProperties;
3336
import org.springdoc.core.SwaggerUiOAuthProperties;
3437

@@ -50,6 +53,11 @@ public class AbstractSwaggerIndexTransformer {
5053
*/
5154
protected SwaggerUiOAuthProperties swaggerUiOAuthProperties;
5255

56+
/**
57+
* The Swagger ui config parameters.
58+
*/
59+
protected SwaggerUiConfigParameters swaggerUiConfigParameters;
60+
5361
/**
5462
* The Object mapper.
5563
*/
@@ -65,11 +73,13 @@ public class AbstractSwaggerIndexTransformer {
6573
*
6674
* @param swaggerUiConfig the swagger ui config
6775
* @param swaggerUiOAuthProperties the swagger ui o auth properties
76+
* @param swaggerUiConfigParameters the swagger ui config parameters
6877
* @param objectMapper the object mapper
6978
*/
70-
public AbstractSwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
79+
public AbstractSwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper) {
7180
this.swaggerUiConfig = swaggerUiConfig;
7281
this.swaggerUiOAuthProperties = swaggerUiOAuthProperties;
82+
this.swaggerUiConfigParameters = swaggerUiConfigParameters;
7383
this.objectMapper = objectMapper;
7484
}
7585

@@ -117,16 +127,6 @@ protected String overwriteSwaggerDefaultUrl(String html) {
117127
return html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
118128
}
119129

120-
/**
121-
* Has default transformations boolean.
122-
*
123-
* @return the boolean
124-
*/
125-
protected boolean hasDefaultTransformations() {
126-
boolean oauth2Configured = !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters());
127-
return oauth2Configured || swaggerUiConfig.isDisableSwaggerDefaultUrl() || swaggerUiConfig.isCsrfEnabled() || swaggerUiConfig.getSyntaxHighlight() != null;
128-
}
129-
130130
/**
131131
* Default transformations string.
132132
*
@@ -139,22 +139,59 @@ protected String defaultTransformations(InputStream inputStream) throws IOExcept
139139
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
140140
html = addInitOauth(html);
141141
}
142-
if (swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
143-
html = overwriteSwaggerDefaultUrl(html);
144-
}
145142
if (swaggerUiConfig.isCsrfEnabled()) {
146143
if (swaggerUiConfig.getCsrf().isUseLocalStorage()) {
147144
html = addCSRFLocalStorage(html);
148-
} else {
145+
}
146+
else {
149147
html = addCSRF(html);
150148
}
151149
}
152-
if (swaggerUiConfig.getSyntaxHighlight() != null) {
150+
if (swaggerUiConfig.getSyntaxHighlight().isPresent()) {
153151
html = addSyntaxHighlight(html);
154152
}
153+
if (swaggerUiConfig.getQueryConfigEnabled() == null || !swaggerUiConfig.getQueryConfigEnabled())
154+
html = addParameters(html);
155+
else
156+
html = enableQueryConfig(html);
157+
if (swaggerUiConfig.isDisableSwaggerDefaultUrl() && swaggerUiConfigParameters.getConfigParameters().get(SwaggerUiConfigParameters.URL_PROPERTY) == null) {
158+
html = overwriteSwaggerDefaultUrl(html);
159+
}
160+
161+
return html;
162+
}
163+
164+
protected String addParameters(String html) throws JsonProcessingException {
165+
String layout = swaggerUiConfigParameters.getLayout() != null ? swaggerUiConfigParameters.getLayout() : "StandaloneLayout";
166+
StringBuilder stringBuilder = new StringBuilder("layout: \"" + layout + "\" ,\n");
167+
168+
if (swaggerUiConfigParameters.getConfigParameters().get(SwaggerUiConfigParameters.URL_PROPERTY) != null && swaggerUiConfig.getConfigUrl() == null)
169+
html = html.replace(Constants.SWAGGER_UI_DEFAULT_URL, swaggerUiConfigParameters.getConfigParameters().get(SwaggerUiConfigParameters.URL_PROPERTY).toString());
170+
171+
Map<String, Object> parametersObjectMap = swaggerUiConfigParameters.getConfigParameters().entrySet().stream()
172+
.filter(entry -> (swaggerUiConfig.getConfigUrl() != null || !SwaggerUiConfigParameters.CONFIG_URL_PROPERTY.equals(entry.getKey())))
173+
.filter(entry -> !SwaggerUiConfigParameters.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()) || !Constants.SWAGGER_UI_OAUTH_REDIRECT_URL.equals(entry.getValue()))
174+
.filter(entry -> !SwaggerUiConfigParameters.URL_PROPERTY.equals(entry.getKey()))
175+
.filter(entry -> !SwaggerUiConfigParameters.LAYOUT_PROPERTY.equals(entry.getKey()))
176+
.filter(entry -> SwaggerUiConfigParameters.URLS_PROPERTY.equals(entry.getKey()) || StringUtils.isNotEmpty((String) entry.getValue()))
177+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
178+
179+
if (!CollectionUtils.isEmpty(parametersObjectMap)) {
180+
String result = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(parametersObjectMap);
181+
result = result.substring(1, result.length() - 1);
182+
stringBuilder.append(result);
183+
html = html.replace("layout: \"StandaloneLayout\"", stringBuilder.toString());
184+
}
185+
155186
return html;
156187
}
157188

189+
protected String enableQueryConfig(String html) {
190+
StringBuilder stringBuilder = new StringBuilder("const ui = SwaggerUIBundle({\n");
191+
stringBuilder.append("queryConfigEnabled: " + swaggerUiConfig.getQueryConfigEnabled() + ",");
192+
return html.replace("const ui = SwaggerUIBundle({", stringBuilder.toString());
193+
}
194+
158195
/**
159196
* Add csrf string.
160197
*

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,15 @@ protected void buildConfigUrl(UriComponentsBuilder uriComponentsBuilder) {
139139
*/
140140
protected UriComponentsBuilder getUriComponentsBuilder(String sbUrl) {
141141
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl);
142-
if (swaggerUiConfig.isDisplayQueryParams() && StringUtils.isNotEmpty(swaggerUiConfigParameters.getUrl())) {
143-
swaggerUiConfigParameters.getConfigParameters().entrySet().stream()
144-
.filter(entry -> !SwaggerUiConfigParameters.CONFIG_URL_PROPERTY.equals(entry.getKey()))
145-
.filter(entry -> !entry.getKey().startsWith(SwaggerUiConfigParameters.URLS_PROPERTY))
146-
.filter(entry -> StringUtils.isNotEmpty((String) entry.getValue()))
147-
.forEach(entry -> uriBuilder.queryParam(entry.getKey(), entry.getValue()));
148-
}
149-
else if (swaggerUiConfig.isDisplayQueryParamsWithoutOauth2() && StringUtils.isNotEmpty(swaggerUiConfigParameters.getUrl())) {
142+
if ((swaggerUiConfig.getQueryConfigEnabled() != null && swaggerUiConfig.getQueryConfigEnabled())) {
150143
swaggerUiConfigParameters.getConfigParameters().entrySet().stream()
151144
.filter(entry -> !SwaggerUiConfigParameters.CONFIG_URL_PROPERTY.equals(entry.getKey()))
152145
.filter(entry -> !SwaggerUiConfigParameters.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()))
146+
.filter(entry -> !SwaggerUiConfigParameters.URL_PROPERTY.equals(entry.getKey()))
153147
.filter(entry -> !entry.getKey().startsWith(SwaggerUiConfigParameters.URLS_PROPERTY))
154148
.filter(entry -> StringUtils.isNotEmpty((String) entry.getValue()))
155149
.forEach(entry -> uriBuilder.queryParam(entry.getKey(), entry.getValue()));
156-
}
157-
else {
158150
uriBuilder.queryParam(SwaggerUiConfigParameters.CONFIG_URL_PROPERTY, swaggerUiConfigParameters.getConfigUrl());
159-
if (StringUtils.isNotEmpty(swaggerUiConfigParameters.getLayout()))
160-
uriBuilder.queryParam(SwaggerUiConfigParameters.LAYOUT_PROPERTY, swaggerUiConfigParameters.getLayout());
161-
if (swaggerUiConfigParameters.getFilter() != null) {
162-
uriBuilder.queryParam(SwaggerUiConfigParameters.FILTER_PROPERTY, swaggerUiConfigParameters.getFilter());
163-
}
164151
}
165152
return uriBuilder;
166153
}

springdoc-openapi-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,14 @@ SwaggerUiHome swaggerUiHome(){
104104
*
105105
* @param swaggerUiConfig the swagger ui config
106106
* @param swaggerUiOAuthProperties the swagger ui o auth properties
107+
* @param swaggerUiConfigParameters the swagger ui config parameters
107108
* @param objectMapper the object mapper
108109
* @return the swagger index transformer
109110
*/
110111
@Bean
111112
@ConditionalOnMissingBean
112-
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
113-
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
113+
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper) {
114+
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties,swaggerUiConfigParameters, objectMapper);
114115
}
115116

116117
/**

springdoc-openapi-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerIndexPageTransformer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.servlet.http.HttpServletRequest;
2626

2727
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import org.springdoc.core.SwaggerUiConfigParameters;
2829
import org.springdoc.core.SwaggerUiConfigProperties;
2930
import org.springdoc.core.SwaggerUiOAuthProperties;
3031
import org.springdoc.ui.AbstractSwaggerIndexTransformer;
@@ -45,10 +46,11 @@ public class SwaggerIndexPageTransformer extends AbstractSwaggerIndexTransformer
4546
*
4647
* @param swaggerUiConfig the swagger ui config
4748
* @param swaggerUiOAuthProperties the swagger ui o auth properties
49+
* @param swaggerUiConfigParameters the swagger ui config parameters
4850
* @param objectMapper the object mapper
4951
*/
50-
public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
51-
super(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
52+
public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper) {
53+
super(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters, objectMapper);
5254
}
5355

5456
@Override
@@ -57,7 +59,7 @@ public Resource transform(HttpServletRequest request, Resource resource,
5759
final AntPathMatcher antPathMatcher = new AntPathMatcher();
5860
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
5961

60-
if (isIndexFound && hasDefaultTransformations()) {
62+
if (isIndexFound) {
6163
String html = defaultTransformations(resource.getInputStream());
6264
return new TransformedResource(resource, html.getBytes());
6365
}

0 commit comments

Comments
 (0)