Skip to content

Commit

Permalink
Enhance swagger-ui url, post 1.6.0. Fixes #1384
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Dec 16, 2021
1 parent 8393afc commit 3e8227c
Show file tree
Hide file tree
Showing 34 changed files with 100 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public class SwaggerUiConfigParameters extends AbstractSwaggerUiConfigProperties
*/
public static final String VALIDATOR_URL_PROPERTY ="validatorUrl";

/**
* The constant QUERY_CONFIG_ENABLED_PROPERTY.
*/
public static final String QUERY_CONFIG_ENABLED_PROPERTY = "queryConfigEnabled";

/**
* The Ui root path.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

Expand All @@ -37,6 +38,8 @@

import org.springframework.util.CollectionUtils;

import static org.springdoc.core.SwaggerUiConfigParameters.QUERY_CONFIG_ENABLED_PROPERTY;

/**
* The type Abstract swagger index transformer.
* @author bnasslahsen
Expand Down Expand Up @@ -136,27 +139,26 @@ protected String overwriteSwaggerDefaultUrl(String html) {
*/
protected String defaultTransformations(InputStream inputStream) throws IOException {
String html = readFullyAsString(inputStream);
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()))
html = addInitOauth(html);
}

if (swaggerUiConfig.isCsrfEnabled()) {
if (swaggerUiConfig.getCsrf().isUseLocalStorage()) {
if (swaggerUiConfig.getCsrf().isUseLocalStorage())
html = addCSRFLocalStorage(html);
}
else {
else
html = addCSRF(html);
}
}
if (swaggerUiConfig.getSyntaxHighlight().isPresent()) {

if (swaggerUiConfig.getSyntaxHighlight().isPresent())
html = addSyntaxHighlight(html);
}

if (swaggerUiConfig.getQueryConfigEnabled() == null || !swaggerUiConfig.getQueryConfigEnabled())
html = addParameters(html);
else
html = enableQueryConfig(html);
if (swaggerUiConfig.isDisableSwaggerDefaultUrl() && swaggerUiConfigParameters.getConfigParameters().get(SwaggerUiConfigParameters.URL_PROPERTY) == null) {
html = addParameter(html, QUERY_CONFIG_ENABLED_PROPERTY, swaggerUiConfig.getQueryConfigEnabled().toString());

if (swaggerUiConfig.isDisableSwaggerDefaultUrl())
html = overwriteSwaggerDefaultUrl(html);
}

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

if (swaggerUiConfigParameters.getConfigParameters().get(SwaggerUiConfigParameters.URL_PROPERTY) != null && swaggerUiConfig.getConfigUrl() == null)
html = html.replace(Constants.SWAGGER_UI_DEFAULT_URL, swaggerUiConfigParameters.getConfigParameters().get(SwaggerUiConfigParameters.URL_PROPERTY).toString());

Map<String, Object> parametersObjectMap = swaggerUiConfigParameters.getConfigParameters().entrySet().stream()
.filter(entry -> (swaggerUiConfig.getConfigUrl() != null || !SwaggerUiConfigParameters.CONFIG_URL_PROPERTY.equals(entry.getKey())))
.filter(entry -> !SwaggerUiConfigParameters.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()) || !Constants.SWAGGER_UI_OAUTH_REDIRECT_URL.equals(entry.getValue()))
.filter(entry -> !SwaggerUiConfigParameters.OAUTH2_REDIRECT_URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !SwaggerUiConfigParameters.URL_PROPERTY.equals(entry.getKey()))
.filter(entry -> !SwaggerUiConfigParameters.LAYOUT_PROPERTY.equals(entry.getKey()))
.filter(entry -> SwaggerUiConfigParameters.URLS_PROPERTY.equals(entry.getKey()) || SwaggerUiConfigParameters.VALIDATOR_URL_PROPERTY.equals(entry.getKey()) || StringUtils.isNotEmpty((String) entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
.filter(entry -> !SwaggerUiConfigParameters.URLS_PROPERTY.equals(entry.getKey()))
.filter(entry -> StringUtils.isNotEmpty((String) entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,(e1, e2) -> e2,
LinkedHashMap::new));

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

protected String enableQueryConfig(String html) {
private String addParameter(String html, String key, String value) {
StringBuilder stringBuilder = new StringBuilder("const ui = SwaggerUIBundle({\n");
stringBuilder.append("queryConfigEnabled: " + swaggerUiConfig.getQueryConfigEnabled() + ",");
stringBuilder.append(key + ": \"" + value + "\",");
return html.replace("const ui = SwaggerUIBundle({", stringBuilder.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ SwaggerUiHome swaggerUiHome(){
* @param swaggerUiOAuthProperties the swagger ui o auth properties
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param objectMapper the object mapper
* @param swaggerWelcomeCommon the swagger welcome common
* @return the swagger index transformer
*/
@Bean
@ConditionalOnMissingBean
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper) {
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties,swaggerUiConfigParameters, objectMapper);
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper,SwaggerWelcomeCommon swaggerWelcomeCommon) {
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties,swaggerUiConfigParameters, objectMapper, swaggerWelcomeCommon);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,30 @@
*/
public class SwaggerIndexPageTransformer extends AbstractSwaggerIndexTransformer implements SwaggerIndexTransformer {

/**
* The Swagger welcome common.
*/
private final SwaggerWelcomeCommon swaggerWelcomeCommon;

/**
* Instantiates a new Swagger index transformer.
*
* @param swaggerUiConfig the swagger ui config
* @param swaggerUiOAuthProperties the swagger ui o auth properties
* @param swaggerUiConfigParameters the swagger ui config parameters
* @param objectMapper the object mapper
* @param swaggerWelcomeCommon the swagger welcome common
*/
public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper) {
public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapper objectMapper, SwaggerWelcomeCommon swaggerWelcomeCommon) {
super(swaggerUiConfig, swaggerUiOAuthProperties, swaggerUiConfigParameters, objectMapper);
this.swaggerWelcomeCommon = swaggerWelcomeCommon;
}

@Override
public Resource transform(HttpServletRequest request, Resource resource,
ResourceTransformerChain transformerChain) throws IOException {
if (swaggerUiConfigParameters.getConfigUrl() == null)
swaggerWelcomeCommon.buildFromCurrentContextPath(request);

final AntPathMatcher antPathMatcher = new AntPathMatcher();
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuil
*
* @param request the request
*/
private void buildFromCurrentContextPath(HttpServletRequest request) {
void buildFromCurrentContextPath(HttpServletRequest request) {
contextPath = request.getContextPath();
buildConfigUrl(ServletUriComponentsBuilder.fromCurrentContextPath());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MvcResult;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down
2 changes: 1 addition & 1 deletion springdoc-openapi-ui/src/test/resources/results/app1
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
],
layout: "StandaloneLayout" ,

"validatorUrl" : ""
"configUrl" : "/v3/api-docs/swagger-config"

});
// End Swagger UI call region
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/context-path/servlet-path/v3/api-docs",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand All @@ -51,8 +51,7 @@
],
layout: "StandaloneLayout" ,

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

});
// End Swagger UI call region
Expand Down
5 changes: 2 additions & 3 deletions springdoc-openapi-ui/src/test/resources/results/app1-prefix
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/documentation/v3/api-docs",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand All @@ -51,8 +51,7 @@
],
layout: "StandaloneLayout" ,

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

});
// End Swagger UI call region
Expand Down
2 changes: 1 addition & 1 deletion springdoc-openapi-ui/src/test/resources/results/app10
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
],
layout: "StandaloneLayout" ,

"validatorUrl" : ""
"configUrl" : "/v3/api-docs/swagger-config"

});
// End Swagger UI call region
Expand Down
2 changes: 1 addition & 1 deletion springdoc-openapi-ui/src/test/resources/results/app12
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ presets: [
],
layout: "StandaloneLayout" ,

"validatorUrl" : ""
"configUrl" : "/v3/api-docs/swagger-config"

});
// End Swagger UI call region
Expand Down
5 changes: 2 additions & 3 deletions springdoc-openapi-ui/src/test/resources/results/app21
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/rest/v3/api-docs",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand All @@ -51,8 +51,7 @@
],
layout: "StandaloneLayout" ,

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

});
// End Swagger UI call region
Expand Down
2 changes: 1 addition & 1 deletion springdoc-openapi-ui/src/test/resources/results/app23
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
queryConfigEnabled: true,
queryConfigEnabled: "true",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
Expand Down
4 changes: 1 addition & 3 deletions springdoc-openapi-ui/src/test/resources/results/app24
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@
],
layout: "StandaloneLayout" ,

"validatorUrl" : "",
"configUrl" : "/foo/bar",
"oauth2RedirectUrl" : "http://localhost/swagger-ui/oauth2-redirect.html"
"configUrl" : "/foo/bar"

});
// End Swagger UI call region
Expand Down
6 changes: 3 additions & 3 deletions springdoc-openapi-ui/src/test/resources/results/app25
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/baf/batz",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand All @@ -51,8 +51,8 @@
],
layout: "StandaloneLayout" ,

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

});
// End Swagger UI call region
Expand Down
6 changes: 3 additions & 3 deletions springdoc-openapi-ui/src/test/resources/results/app26
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/v3/api-docs",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand All @@ -51,8 +51,8 @@
],
layout: "BaseLayout" ,

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

});
// End Swagger UI call region
Expand Down
2 changes: 1 addition & 1 deletion springdoc-openapi-ui/src/test/resources/results/app27
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
queryConfigEnabled: true,
queryConfigEnabled: "true",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
Expand Down
7 changes: 3 additions & 4 deletions springdoc-openapi-ui/src/test/resources/results/app28
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/v3/api-docs",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand All @@ -51,9 +51,8 @@
],
layout: "StandaloneLayout" ,

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

});
// End Swagger UI call region
Expand Down
5 changes: 2 additions & 3 deletions springdoc-openapi-ui/src/test/resources/results/app5
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/context-path/v3/api-docs",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand All @@ -51,8 +51,7 @@
],
layout: "StandaloneLayout" ,

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

});
// End Swagger UI call region
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "/context-path/servlet-path/v3/api-docs",
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
Expand All @@ -51,8 +51,7 @@
],
layout: "StandaloneLayout" ,

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

});
// End Swagger UI call region
Expand Down
2 changes: 1 addition & 1 deletion springdoc-openapi-ui/src/test/resources/results/app7
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
],
layout: "StandaloneLayout" ,

"validatorUrl" : ""
"configUrl" : "/v3/api-docs/swagger-config"

});
// End Swagger UI call region
Expand Down
2 changes: 1 addition & 1 deletion springdoc-openapi-ui/src/test/resources/results/app9
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
],
layout: "StandaloneLayout" ,

"validatorUrl" : ""
"configUrl" : "/v3/api-docs/swagger-config"

});
// End Swagger UI call region
Expand Down

0 comments on commit 3e8227c

Please sign in to comment.