Skip to content

Commit c315d3f

Browse files
author
bnasslahsen
committed
Merge branch 'natrem-745-ui-transformer-interface'
2 parents 2fbc384 + 0af7d97 commit c315d3f

File tree

7 files changed

+166
-112
lines changed

7 files changed

+166
-112
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
@@ -32,6 +32,8 @@
3232
import org.springdoc.core.SwaggerUiConfigProperties;
3333
import org.springdoc.core.SwaggerUiOAuthProperties;
3434

35+
import org.springframework.util.CollectionUtils;
36+
3537
/**
3638
* The type Abstract swagger index transformer.
3739
* @author bnasslahsen
@@ -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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ SwaggerWelcome swaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringD
7070
@Bean
7171
@ConditionalOnMissingBean
7272
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
73-
return new SwaggerIndexTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
73+
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
7474
}
7575

7676
/**
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 java.io.IOException;
24+
25+
import javax.servlet.http.HttpServletRequest;
26+
27+
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import org.springdoc.core.SwaggerUiConfigProperties;
29+
import org.springdoc.core.SwaggerUiOAuthProperties;
30+
import org.springdoc.ui.AbstractSwaggerIndexTransformer;
31+
32+
import org.springframework.core.io.Resource;
33+
import org.springframework.util.AntPathMatcher;
34+
import org.springframework.web.servlet.resource.ResourceTransformerChain;
35+
import org.springframework.web.servlet.resource.TransformedResource;
36+
37+
/**
38+
* The type Swagger index transformer.
39+
* @author bnasslahsen
40+
*/
41+
public class SwaggerIndexPageTransformer extends AbstractSwaggerIndexTransformer implements SwaggerIndexTransformer {
42+
43+
/**
44+
* Instantiates a new Swagger index transformer.
45+
*
46+
* @param swaggerUiConfig the swagger ui config
47+
* @param swaggerUiOAuthProperties the swagger ui o auth properties
48+
* @param objectMapper the object mapper
49+
*/
50+
public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
51+
super(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
52+
}
53+
54+
@Override
55+
public Resource transform(HttpServletRequest request, Resource resource,
56+
ResourceTransformerChain transformerChain) throws IOException {
57+
final AntPathMatcher antPathMatcher = new AntPathMatcher();
58+
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
59+
60+
if (isIndexFound && hasDefaultTransformations()) {
61+
String html = defaultTransformations(resource.getInputStream());
62+
return new TransformedResource(resource, html.getBytes());
63+
}
64+
else
65+
return resource;
66+
}
67+
68+
}

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

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,62 +20,12 @@
2020

2121
package org.springdoc.webmvc.ui;
2222

23-
import java.io.IOException;
24-
25-
import javax.servlet.http.HttpServletRequest;
26-
27-
import com.fasterxml.jackson.databind.ObjectMapper;
28-
import org.springdoc.core.SwaggerUiConfigProperties;
29-
import org.springdoc.core.SwaggerUiOAuthProperties;
30-
import org.springdoc.ui.AbstractSwaggerIndexTransformer;
31-
32-
import org.springframework.core.io.Resource;
33-
import org.springframework.util.AntPathMatcher;
34-
import org.springframework.util.CollectionUtils;
3523
import org.springframework.web.servlet.resource.ResourceTransformer;
36-
import org.springframework.web.servlet.resource.ResourceTransformerChain;
37-
import org.springframework.web.servlet.resource.TransformedResource;
3824

3925
/**
40-
* The type Swagger index transformer.
41-
* @author bnasslahsen
26+
* The type Swagger index page transformer.
27+
* @author pverdage
4228
*/
43-
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements ResourceTransformer {
44-
45-
/**
46-
* Instantiates a new Swagger index transformer.
47-
*
48-
* @param swaggerUiConfig the swagger ui config
49-
* @param swaggerUiOAuthProperties the swagger ui o auth properties
50-
* @param objectMapper the object mapper
51-
*/
52-
public SwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
53-
super(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
54-
}
55-
56-
@Override
57-
public Resource transform(HttpServletRequest request, Resource resource,
58-
ResourceTransformerChain transformerChain) throws IOException {
59-
final AntPathMatcher antPathMatcher = new AntPathMatcher();
60-
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);
75-
return new TransformedResource(resource, html.getBytes());
76-
}
77-
else
78-
return resource;
79-
}
29+
public interface SwaggerIndexTransformer extends ResourceTransformer {
8030

8131
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ SwaggerWebFluxConfigurer swaggerWebFluxConfigurer(SwaggerUiConfigParameters swag
7171
@Bean
7272
@ConditionalOnMissingBean
7373
SwaggerIndexTransformer indexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig ,SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
74-
return new SwaggerIndexTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
74+
return new SwaggerIndexPageTransformer(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
7575
}
7676

7777
/**
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.web.reactive.resource.ResourceTransformerChain;
33+
import org.springframework.web.reactive.resource.TransformedResource;
34+
import org.springframework.web.server.ServerWebExchange;
35+
36+
/**
37+
* The type Swagger index transformer.
38+
* @author bnasslahsen
39+
*/
40+
public class SwaggerIndexPageTransformer extends AbstractSwaggerIndexTransformer implements SwaggerIndexTransformer {
41+
42+
/**
43+
* Instantiates a new Swagger index transformer.
44+
*
45+
* @param swaggerUiConfig the swagger ui config
46+
* @param swaggerUiOAuthProperties the swagger ui o auth properties
47+
* @param objectMapper the object mapper
48+
*/
49+
public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
50+
super(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
51+
}
52+
53+
@Override
54+
public Mono<Resource> transform(ServerWebExchange serverWebExchange, Resource resource, ResourceTransformerChain resourceTransformerChain) {
55+
final AntPathMatcher antPathMatcher = new AntPathMatcher();
56+
57+
try {
58+
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
59+
if (isIndexFound && hasDefaultTransformations()) {
60+
String html = defaultTransformations(resource.getInputStream());
61+
return Mono.just(new TransformedResource(resource, html.getBytes()));
62+
}
63+
else {
64+
return Mono.just(resource);
65+
}
66+
}
67+
catch (Exception e) {
68+
throw new SpringDocUIException("Failed to transform Index", e);
69+
}
70+
}
71+
72+
}

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

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,12 @@
2020

2121
package org.springdoc.webflux.ui;
2222

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;
3323
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;
3724

3825
/**
3926
* The type Swagger index transformer.
40-
* @author bnasslahsen
27+
* @author pverdage
4128
*/
42-
public class SwaggerIndexTransformer extends AbstractSwaggerIndexTransformer implements ResourceTransformer {
43-
44-
/**
45-
* Instantiates a new Swagger index transformer.
46-
*
47-
* @param swaggerUiConfig the swagger ui config
48-
* @param swaggerUiOAuthProperties the swagger ui o auth properties
49-
* @param objectMapper the object mapper
50-
*/
51-
public SwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapper objectMapper) {
52-
super(swaggerUiConfig, swaggerUiOAuthProperties, objectMapper);
53-
}
54-
55-
@Override
56-
public Mono<Resource> transform(ServerWebExchange serverWebExchange, Resource resource, ResourceTransformerChain resourceTransformerChain) {
57-
final AntPathMatcher antPathMatcher = new AntPathMatcher();
58-
boolean isIndexFound = false;
59-
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);
75-
return Mono.just(new TransformedResource(resource, html.getBytes()));
76-
}
77-
else {
78-
return Mono.just(resource);
79-
}
80-
}
81-
catch (Exception e) {
82-
throw new SpringDocUIException("Failed to transform Index", e);
83-
}
84-
}
29+
public interface SwaggerIndexTransformer extends ResourceTransformer {
8530

8631
}

0 commit comments

Comments
 (0)