Skip to content

Commit 3e8227c

Browse files
committed
Enhance swagger-ui url, post 1.6.0. Fixes #1384
1 parent 8393afc commit 3e8227c

File tree

34 files changed

+100
-93
lines changed

34 files changed

+100
-93
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public class SwaggerUiConfigParameters extends AbstractSwaggerUiConfigProperties
8686
*/
8787
public static final String VALIDATOR_URL_PROPERTY ="validatorUrl";
8888

89+
/**
90+
* The constant QUERY_CONFIG_ENABLED_PROPERTY.
91+
*/
92+
public static final String QUERY_CONFIG_ENABLED_PROPERTY = "queryConfigEnabled";
93+
8994
/**
9095
* The Ui root path.
9196
*/

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.nio.charset.StandardCharsets;
27+
import java.util.LinkedHashMap;
2728
import java.util.Map;
2829
import java.util.stream.Collectors;
2930

@@ -37,6 +38,8 @@
3738

3839
import org.springframework.util.CollectionUtils;
3940

41+
import static org.springdoc.core.SwaggerUiConfigParameters.QUERY_CONFIG_ENABLED_PROPERTY;
42+
4043
/**
4144
* The type Abstract swagger index transformer.
4245
* @author bnasslahsen
@@ -136,27 +139,26 @@ protected String overwriteSwaggerDefaultUrl(String html) {
136139
*/
137140
protected String defaultTransformations(InputStream inputStream) throws IOException {
138141
String html = readFullyAsString(inputStream);
139-
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
142+
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()))
140143
html = addInitOauth(html);
141-
}
144+
142145
if (swaggerUiConfig.isCsrfEnabled()) {
143-
if (swaggerUiConfig.getCsrf().isUseLocalStorage()) {
146+
if (swaggerUiConfig.getCsrf().isUseLocalStorage())
144147
html = addCSRFLocalStorage(html);
145-
}
146-
else {
148+
else
147149
html = addCSRF(html);
148-
}
149150
}
150-
if (swaggerUiConfig.getSyntaxHighlight().isPresent()) {
151+
152+
if (swaggerUiConfig.getSyntaxHighlight().isPresent())
151153
html = addSyntaxHighlight(html);
152-
}
154+
153155
if (swaggerUiConfig.getQueryConfigEnabled() == null || !swaggerUiConfig.getQueryConfigEnabled())
154156
html = addParameters(html);
155157
else
156-
html = enableQueryConfig(html);
157-
if (swaggerUiConfig.isDisableSwaggerDefaultUrl() && swaggerUiConfigParameters.getConfigParameters().get(SwaggerUiConfigParameters.URL_PROPERTY) == null) {
158+
html = addParameter(html, QUERY_CONFIG_ENABLED_PROPERTY, swaggerUiConfig.getQueryConfigEnabled().toString());
159+
160+
if (swaggerUiConfig.isDisableSwaggerDefaultUrl())
158161
html = overwriteSwaggerDefaultUrl(html);
159-
}
160162

161163
return html;
162164
}
@@ -165,16 +167,13 @@ protected String addParameters(String html) throws JsonProcessingException {
165167
String layout = swaggerUiConfigParameters.getLayout() != null ? swaggerUiConfigParameters.getLayout() : "StandaloneLayout";
166168
StringBuilder stringBuilder = new StringBuilder("layout: \"" + layout + "\" ,\n");
167169

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-
171170
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()))
171+
.filter(entry -> !SwaggerUiConfigParameters.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()))
174172
.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()) || SwaggerUiConfigParameters.VALIDATOR_URL_PROPERTY.equals(entry.getKey()) || StringUtils.isNotEmpty((String) entry.getValue()))
177-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
173+
.filter(entry -> !SwaggerUiConfigParameters.URLS_PROPERTY.equals(entry.getKey()))
174+
.filter(entry -> StringUtils.isNotEmpty((String) entry.getValue()))
175+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(e1, e2) -> e2,
176+
LinkedHashMap::new));
178177

179178
if (!CollectionUtils.isEmpty(parametersObjectMap)) {
180179
String result = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(parametersObjectMap);
@@ -186,9 +185,9 @@ protected String addParameters(String html) throws JsonProcessingException {
186185
return html;
187186
}
188187

189-
protected String enableQueryConfig(String html) {
188+
private String addParameter(String html, String key, String value) {
190189
StringBuilder stringBuilder = new StringBuilder("const ui = SwaggerUIBundle({\n");
191-
stringBuilder.append("queryConfigEnabled: " + swaggerUiConfig.getQueryConfigEnabled() + ",");
190+
stringBuilder.append(key + ": \"" + value + "\",");
192191
return html.replace("const ui = SwaggerUIBundle({", stringBuilder.toString());
193192
}
194193

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
@@ -106,12 +106,13 @@ SwaggerUiHome swaggerUiHome(){
106106
* @param swaggerUiOAuthProperties the swagger ui o auth properties
107107
* @param swaggerUiConfigParameters the swagger ui config parameters
108108
* @param objectMapper the object mapper
109+
* @param swaggerWelcomeCommon the swagger welcome common
109110
* @return the swagger index transformer
110111
*/
111112
@Bean
112113
@ConditionalOnMissingBean
113-
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper) {
114-
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties,swaggerUiConfigParameters, objectMapper);
114+
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper,SwaggerWelcomeCommon swaggerWelcomeCommon) {
115+
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties,swaggerUiConfigParameters, objectMapper, swaggerWelcomeCommon);
115116
}
116117

117118
/**

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,30 @@
4141
*/
4242
public class SwaggerIndexPageTransformer extends AbstractSwaggerIndexTransformer implements SwaggerIndexTransformer {
4343

44+
/**
45+
* The Swagger welcome common.
46+
*/
47+
private final SwaggerWelcomeCommon swaggerWelcomeCommon;
48+
4449
/**
4550
* Instantiates a new Swagger index transformer.
46-
*
4751
* @param swaggerUiConfig the swagger ui config
4852
* @param swaggerUiOAuthProperties the swagger ui o auth properties
4953
* @param swaggerUiConfigParameters the swagger ui config parameters
5054
* @param objectMapper the object mapper
55+
* @param swaggerWelcomeCommon the swagger welcome common
5156
*/
52-
public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper) {
57+
public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper, SwaggerWelcomeCommon swaggerWelcomeCommon) {
5358
super(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters, objectMapper);
59+
this.swaggerWelcomeCommon = swaggerWelcomeCommon;
5460
}
5561

5662
@Override
5763
public Resource transform(HttpServletRequest request, Resource resource,
5864
ResourceTransformerChain transformerChain) throws IOException {
65+
if (swaggerUiConfigParameters.getConfigUrl() == null)
66+
swaggerWelcomeCommon.buildFromCurrentContextPath(request);
67+
5968
final AntPathMatcher antPathMatcher = new AntPathMatcher();
6069
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
6170

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuil
7373
*
7474
* @param request the request
7575
*/
76-
private void buildFromCurrentContextPath(HttpServletRequest request) {
76+
void buildFromCurrentContextPath(HttpServletRequest request) {
7777
contextPath = request.getContextPath();
7878
buildConfigUrl(ServletUriComponentsBuilder.fromCurrentContextPath());
7979
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app24/SpringDocApp24Test.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323

2424
import org.springframework.boot.autoconfigure.SpringBootApplication;
2525
import org.springframework.test.context.TestPropertySource;
26-
import org.springframework.test.web.servlet.MvcResult;
2726

28-
import static org.junit.jupiter.api.Assertions.assertEquals;
29-
import static org.junit.jupiter.api.Assertions.assertTrue;
3027
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3128
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3229
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

springdoc-openapi-ui/src/test/resources/results/app1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : ""
54+
"configUrl" : "/v3/api-docs/swagger-config"
5555

5656
});
5757
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app1-contextpath

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
url: "/context-path/servlet-path/v3/api-docs",
42+
url: "https://petstore.swagger.io/v2/swagger.json",
4343
dom_id: '#swagger-ui',
4444
deepLinking: true,
4545
presets: [
@@ -51,8 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : "",
55-
"oauth2RedirectUrl" : "http://localhost/context-path/servlet-path/test/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/context-path/servlet-path/v3/api-docs/swagger-config"
5655

5756
});
5857
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app1-prefix

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
url: "/documentation/v3/api-docs",
42+
url: "https://petstore.swagger.io/v2/swagger.json",
4343
dom_id: '#swagger-ui',
4444
deepLinking: true,
4545
presets: [
@@ -51,8 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : "",
55-
"oauth2RedirectUrl" : "http://localhost/documentation/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/documentation/v3/api-docs/swagger-config"
5655

5756
});
5857
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app10

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : ""
54+
"configUrl" : "/v3/api-docs/swagger-config"
5555

5656
});
5757
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app12

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ presets: [
5252
],
5353
layout: "StandaloneLayout" ,
5454

55-
"validatorUrl" : ""
55+
"configUrl" : "/v3/api-docs/swagger-config"
5656

5757
});
5858
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app21

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
url: "/rest/v3/api-docs",
42+
url: "https://petstore.swagger.io/v2/swagger.json",
4343
dom_id: '#swagger-ui',
4444
deepLinking: true,
4545
presets: [
@@ -51,8 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : "",
55-
"oauth2RedirectUrl" : "http://localhost/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/rest/v3/api-docs/swagger-config"
5655

5756
});
5857
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app23

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
queryConfigEnabled: true,
42+
queryConfigEnabled: "true",
4343
url: "https://petstore.swagger.io/v2/swagger.json",
4444
dom_id: '#swagger-ui',
4545
deepLinking: true,

springdoc-openapi-ui/src/test/resources/results/app24

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : "",
55-
"configUrl" : "/foo/bar",
56-
"oauth2RedirectUrl" : "http://localhost/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/foo/bar"
5755

5856
});
5957
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app25

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
url: "/baf/batz",
42+
url: "https://petstore.swagger.io/v2/swagger.json",
4343
dom_id: '#swagger-ui',
4444
deepLinking: true,
4545
presets: [
@@ -51,8 +51,8 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : "/foo/validate",
55-
"oauth2RedirectUrl" : "http://localhost/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/baf/batz/swagger-config",
55+
"validatorUrl" : "/foo/validate"
5656

5757
});
5858
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app26

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
url: "/v3/api-docs",
42+
url: "https://petstore.swagger.io/v2/swagger.json",
4343
dom_id: '#swagger-ui',
4444
deepLinking: true,
4545
presets: [
@@ -51,8 +51,8 @@
5151
],
5252
layout: "BaseLayout" ,
5353

54-
"validatorUrl" : "",
55-
"oauth2RedirectUrl" : "http://localhost/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/v3/api-docs/swagger-config",
55+
"layout" : "BaseLayout"
5656

5757
});
5858
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app27

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
queryConfigEnabled: true,
42+
queryConfigEnabled: "true",
4343
url: "https://petstore.swagger.io/v2/swagger.json",
4444
dom_id: '#swagger-ui',
4545
deepLinking: true,

springdoc-openapi-ui/src/test/resources/results/app28

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
url: "/v3/api-docs",
42+
url: "https://petstore.swagger.io/v2/swagger.json",
4343
dom_id: '#swagger-ui',
4444
deepLinking: true,
4545
presets: [
@@ -51,9 +51,8 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"filter" : "false",
55-
"validatorUrl" : "",
56-
"oauth2RedirectUrl" : "http://localhost/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/v3/api-docs/swagger-config",
55+
"filter" : "false"
5756

5857
});
5958
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app5

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
url: "/context-path/v3/api-docs",
42+
url: "https://petstore.swagger.io/v2/swagger.json",
4343
dom_id: '#swagger-ui',
4444
deepLinking: true,
4545
presets: [
@@ -51,8 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : "",
55-
"oauth2RedirectUrl" : "http://localhost/context-path/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/context-path/v3/api-docs/swagger-config"
5655

5756
});
5857
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app5-contextpath

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
window.onload = function() {
4040
// Begin Swagger UI call region
4141
const ui = SwaggerUIBundle({
42-
url: "/context-path/servlet-path/v3/api-docs",
42+
url: "https://petstore.swagger.io/v2/swagger.json",
4343
dom_id: '#swagger-ui',
4444
deepLinking: true,
4545
presets: [
@@ -51,8 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : "",
55-
"oauth2RedirectUrl" : "http://localhost/context-path/servlet-path/test/swagger-ui/oauth2-redirect.html"
54+
"configUrl" : "/context-path/servlet-path/v3/api-docs/swagger-config"
5655

5756
});
5857
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app7

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : ""
54+
"configUrl" : "/v3/api-docs/swagger-config"
5555

5656
});
5757
// End Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app9

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
],
5252
layout: "StandaloneLayout" ,
5353

54-
"validatorUrl" : ""
54+
"configUrl" : "/v3/api-docs/swagger-config"
5555

5656
});
5757
// End Swagger UI call region

0 commit comments

Comments
 (0)