Skip to content

Commit d828bdd

Browse files
committed
factorize default UI transformation behavior
1 parent 4cb2d3f commit d828bdd

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springdoc.core.Constants;
3232
import org.springdoc.core.SwaggerUiConfigProperties;
3333
import org.springdoc.core.SwaggerUiOAuthProperties;
34+
import org.springframework.core.io.Resource;
35+
import org.springframework.util.CollectionUtils;
3436

3537
/**
3638
* The type Abstract swagger index transformer.
@@ -109,4 +111,21 @@ protected String readFullyAsString(InputStream inputStream)
109111
protected String overwriteSwaggerDefaultUrl(String html) {
110112
return html.replace(Constants.SWAGGER_UI_DEFAULT_URL, StringUtils.EMPTY);
111113
}
114+
115+
protected boolean hasDefaultTransformations() {
116+
boolean oauth2Configured = !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters());
117+
return oauth2Configured || swaggerUiConfig.isDisableSwaggerDefaultUrl();
118+
}
119+
120+
protected String defaultTransformations(InputStream inputStream) throws IOException {
121+
String html = readFullyAsString(inputStream);
122+
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
123+
html = addInitOauth(html);
124+
}
125+
if (swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
126+
html = overwriteSwaggerDefaultUrl(html);
127+
}
128+
129+
return html;
130+
}
112131
}

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,9 @@ public Resource transform(HttpServletRequest request, Resource resource,
5757
ResourceTransformerChain transformerChain) throws IOException {
5858
final AntPathMatcher antPathMatcher = new AntPathMatcher();
5959
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
60-
if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()) && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
61-
String html = readFullyAsString(resource.getInputStream());
62-
html = addInitOauth(html);
63-
html = overwriteSwaggerDefaultUrl(html);
64-
return new TransformedResource(resource, html.getBytes());
65-
}
66-
else if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
67-
String html = readFullyAsString(resource.getInputStream());
68-
html = addInitOauth(html);
69-
return new TransformedResource(resource, html.getBytes());
70-
}
71-
else if (isIndexFound && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
72-
String html = readFullyAsString(resource.getInputStream());
73-
html = overwriteSwaggerDefaultUrl(html);
60+
61+
if (isIndexFound && hasDefaultTransformations()) {
62+
String html = defaultTransformations(resource.getInputStream());
7463
return new TransformedResource(resource, html.getBytes());
7564
}
7665
else

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,11 @@ public SwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, Swagge
5454
@Override
5555
public Mono<Resource> transform(ServerWebExchange serverWebExchange, Resource resource, ResourceTransformerChain resourceTransformerChain) {
5656
final AntPathMatcher antPathMatcher = new AntPathMatcher();
57-
boolean isIndexFound = false;
57+
5858
try {
59-
isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
60-
if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()) && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
61-
String html = readFullyAsString(resource.getInputStream());
62-
html = addInitOauth(html);
63-
html = overwriteSwaggerDefaultUrl(html);
64-
return Mono.just(new TransformedResource(resource, html.getBytes()));
65-
}
66-
else if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
67-
String html = readFullyAsString(resource.getInputStream());
68-
html = addInitOauth(html);
69-
return Mono.just(new TransformedResource(resource, html.getBytes()));
70-
}
71-
else if (isIndexFound && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
72-
String html = readFullyAsString(resource.getInputStream());
73-
html = overwriteSwaggerDefaultUrl(html);
59+
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
60+
if (isIndexFound && hasDefaultTransformations()) {
61+
String html = defaultTransformations(resource.getInputStream());
7462
return Mono.just(new TransformedResource(resource, html.getBytes()));
7563
}
7664
else {

0 commit comments

Comments
 (0)