Skip to content

Commit 39dd00f

Browse files
author
bnasslahsen
committed
Merge branch '745-ui-transformer-interface' of https://github.com/natrem/springdoc-openapi into natrem-745-ui-transformer-interface
2 parents 2fbc384 + d828bdd commit 39dd00f

File tree

9 files changed

+111
-42
lines changed

9 files changed

+111
-42
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/SwaggerConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ SwaggerWelcome swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringD
6969
*/
7070
@Bean
7171
@ConditionalOnMissingBean
72-
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
72+
SwaggerIndexPageTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
7373
return new SwaggerIndexTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
7474
}
7575

@@ -82,7 +82,7 @@ SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUi
8282
*/
8383
@Bean
8484
@ConditionalOnMissingBean
85-
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexTransformer swaggerIndexTransformer) {
85+
SwaggerWebMvcConfigurer swaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexPageTransformer swaggerIndexTransformer) {
8686
return new SwaggerWebMvcConfigurer(swaggerUiConfigParameters, swaggerIndexTransformer);
8787
}
8888

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2020 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package org.springdoc.webmvc.ui;
22+
23+
import org.springframework.web.servlet.resource.ResourceTransformer;
24+
25+
/**
26+
* The type Swagger index page transformer.
27+
* @author pverdage
28+
*/
29+
public interface SwaggerIndexPageTransformer extends ResourceTransformer {
30+
31+
}

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,14 @@
3232
import org.springframework.core.io.Resource;
3333
import org.springframework.util.AntPathMatcher;
3434
import org.springframework.util.CollectionUtils;
35-
import org.springframework.web.servlet.resource.ResourceTransformer;
3635
import org.springframework.web.servlet.resource.ResourceTransformerChain;
3736
import org.springframework.web.servlet.resource.TransformedResource;
3837

3938
/**
4039
* The type Swagger index transformer.
4140
* @author bnasslahsen
4241
*/
43-
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements ResourceTransformer {
42+
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements SwaggerIndexPageTransformer {
4443

4544
/**
4645
* Instantiates a new Swagger index transformer.
@@ -58,20 +57,9 @@ public Resource transform(HttpServletRequest request, Resource resource,
5857
ResourceTransformerChain transformerChain) throws IOException {
5958
final AntPathMatcher antPathMatcher = new AntPathMatcher();
6059
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
61-
if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()) && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
62-
String html = readFullyAsString(resource.getInputStream());
63-
html = addInitOauth(html);
64-
html = overwriteSwaggerDefaultUrl(html);
65-
return new TransformedResource(resource, html.getBytes());
66-
}
67-
else if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
68-
String html = readFullyAsString(resource.getInputStream());
69-
html = addInitOauth(html);
70-
return new TransformedResource(resource, html.getBytes());
71-
}
72-
else if (isIndexFound && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
73-
String html = readFullyAsString(resource.getInputStream());
74-
html = overwriteSwaggerDefaultUrl(html);
60+
61+
if (isIndexFound && hasDefaultTransformations()) {
62+
String html = defaultTransformations(resource.getInputStream());
7563
return new TransformedResource(resource, html.getBytes());
7664
}
7765
else

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public class SwaggerWebMvcConfigurer extends WebMvcConfigurerAdapter { // NOSONA
4444
/**
4545
* The Swagger index transformer.
4646
*/
47-
private SwaggerIndexTransformer swaggerIndexTransformer;
47+
private SwaggerIndexPageTransformer swaggerIndexTransformer;
4848

4949
/**
5050
* Instantiates a new Swagger web mvc configurer.
5151
*
5252
* @param swaggerUiConfigParameters the swagger ui calculated config
5353
* @param swaggerIndexTransformer the swagger index transformer
5454
*/
55-
public SwaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexTransformer swaggerIndexTransformer) {
55+
public SwaggerWebMvcConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SwaggerIndexPageTransformer swaggerIndexTransformer) {
5656
this.swaggerPath = swaggerUiConfigParameters.getPath();
5757
this.swaggerIndexTransformer = swaggerIndexTransformer;
5858
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class SwaggerConfig implements WebFluxConfigurer {
5656
*/
5757
@Bean
5858
@ConditionalOnMissingBean
59-
SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer) {
59+
SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SpringDocConfigProperties springDocConfigProperties, SwaggerIndexPageTransformer swaggerIndexTransformer) {
6060
return new SwaggerWebFluxConfigurer(swaggerUiConfigParameters, springDocConfigProperties, swaggerIndexTransformer);
6161
}
6262

@@ -70,7 +70,7 @@ SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigParameters swag
7070
*/
7171
@Bean
7272
@ConditionalOnMissingBean
73-
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig ,SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
73+
SwaggerIndexPageTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig ,SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
7474
return new SwaggerIndexTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
7575
}
7676

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
*
3+
* *
4+
* * * Copyright 2019-2020 the original author or authors.
5+
* * *
6+
* * * Licensed under the Apache License, Version 2.0 (the "License");
7+
* * * you may not use this file except in compliance with the License.
8+
* * * You may obtain a copy of the License at
9+
* * *
10+
* * * https://www.apache.org/licenses/LICENSE-2.0
11+
* * *
12+
* * * Unless required by applicable law or agreed to in writing, software
13+
* * * distributed under the License is distributed on an "AS IS" BASIS,
14+
* * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* * * See the License for the specific language governing permissions and
16+
* * * limitations under the License.
17+
* *
18+
*
19+
*/
20+
21+
package org.springdoc.webflux.ui;
22+
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import org.springdoc.core.SwaggerUiConfigProperties;
25+
import org.springdoc.core.SwaggerUiOAuthProperties;
26+
import org.springdoc.ui.AbstractSwaggerIndexTransformer;
27+
import org.springdoc.ui.SpringDocUIException;
28+
import reactor.core.publisher.Mono;
29+
30+
import org.springframework.core.io.Resource;
31+
import org.springframework.util.AntPathMatcher;
32+
import org.springframework.util.CollectionUtils;
33+
import org.springframework.web.reactive.resource.ResourceTransformer;
34+
import org.springframework.web.reactive.resource.ResourceTransformerChain;
35+
import org.springframework.web.reactive.resource.TransformedResource;
36+
import org.springframework.web.server.ServerWebExchange;
37+
38+
/**
39+
* The type Swagger index transformer.
40+
* @author pverdage
41+
*/
42+
public interface SwaggerIndexPageTransformer extends ResourceTransformer {
43+
44+
}

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.core.io.Resource;
3131
import org.springframework.util.AntPathMatcher;
3232
import org.springframework.util.CollectionUtils;
33-
import org.springframework.web.reactive.resource.ResourceTransformer;
3433
import org.springframework.web.reactive.resource.ResourceTransformerChain;
3534
import org.springframework.web.reactive.resource.TransformedResource;
3635
import org.springframework.web.server.ServerWebExchange;
@@ -39,7 +38,7 @@
3938
* The type Swagger index transformer.
4039
* @author bnasslahsen
4140
*/
42-
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements ResourceTransformer {
41+
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements SwaggerIndexPageTransformer {
4342

4443
/**
4544
* Instantiates a new Swagger index transformer.
@@ -55,23 +54,11 @@ public SwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, Swagge
5554
@Override
5655
public Mono<Resource> transform(ServerWebExchange serverWebExchange, Resource resource, ResourceTransformerChain resourceTransformerChain) {
5756
final AntPathMatcher antPathMatcher = new AntPathMatcher();
58-
boolean isIndexFound = false;
57+
5958
try {
60-
isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
61-
if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()) && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
62-
String html = readFullyAsString(resource.getInputStream());
63-
html = addInitOauth(html);
64-
html = overwriteSwaggerDefaultUrl(html);
65-
return Mono.just(new TransformedResource(resource, html.getBytes()));
66-
}
67-
else if (isIndexFound && !CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters())) {
68-
String html = readFullyAsString(resource.getInputStream());
69-
html = addInitOauth(html);
70-
return Mono.just(new TransformedResource(resource, html.getBytes()));
71-
}
72-
else if (isIndexFound && swaggerUiConfig.isDisableSwaggerDefaultUrl()) {
73-
String html = readFullyAsString(resource.getInputStream());
74-
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());
7562
return Mono.just(new TransformedResource(resource, html.getBytes()));
7663
}
7764
else {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer {
4949
/**
5050
* The Swagger index transformer.
5151
*/
52-
private SwaggerIndexTransformer swaggerIndexTransformer;
52+
private SwaggerIndexPageTransformer swaggerIndexTransformer;
5353

5454
/**
5555
* Instantiates a new Swagger web flux configurer.
@@ -58,7 +58,7 @@ public class SwaggerWebFluxConfigurer implements WebFluxConfigurer {
5858
* @param springDocConfigProperties the spring doc config properties
5959
* @param swaggerIndexTransformer the swagger index transformer
6060
*/
61-
public SwaggerWebFluxConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SpringDocConfigProperties springDocConfigProperties, SwaggerIndexTransformer swaggerIndexTransformer) {
61+
public SwaggerWebFluxConfigurer(SwaggerUiConfigParameters swaggerUiConfigParameters, SpringDocConfigProperties springDocConfigProperties, SwaggerIndexPageTransformer swaggerIndexTransformer) {
6262
this.swaggerPath = swaggerUiConfigParameters.getPath();
6363
this.webJarsPrefixUrl = springDocConfigProperties.getWebjars().getPrefix();
6464
this.swaggerIndexTransformer = swaggerIndexTransformer;

0 commit comments

Comments
 (0)