Skip to content

Commit ceb4a5b

Browse files
committed
ui code refactoring
1 parent a9e91ca commit ceb4a5b

File tree

5 files changed

+70
-90
lines changed

5 files changed

+70
-90
lines changed

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.util.CollectionUtils;
3131
import org.springframework.web.util.UriComponentsBuilder;
3232

33-
import static org.springdoc.core.Constants.SWAGGGER_CONFIG_FILE;
3433
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
3534

3635

@@ -55,6 +54,15 @@ public abstract class AbstractSwaggerWelcome implements InitializingBean {
5554
*/
5655
protected final SwaggerUiConfigParameters swaggerUiConfigParameters;
5756

57+
/**
58+
* The Swagger config url.
59+
*/
60+
protected String swaggerConfigUrl;
61+
62+
/**
63+
* The Api docs url.
64+
*/
65+
protected String apiDocsUrl;
5866

5967
/**
6068
* Instantiates a new Abstract swagger welcome.
@@ -98,20 +106,19 @@ protected String buildUrl(String contextPath, String docsUrl) {
98106
* @param uriComponentsBuilder the uri components builder
99107
*/
100108
protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriComponentsBuilder) {
101-
String apiDocsUrl = springDocConfigProperties.getApiDocs().getPath();
102109
if (StringUtils.isEmpty(swaggerUiConfig.getConfigUrl())) {
103-
String url = buildUrl(contextPath, apiDocsUrl);
104-
String swaggerConfigUrl = url + DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
110+
apiDocsUrl = StringUtils.defaultIfEmpty(apiDocsUrl, buildApiDocUrl(contextPath));
111+
swaggerConfigUrl = StringUtils.defaultIfEmpty(swaggerConfigUrl, buildSwaggerConfigUrl(contextPath));
105112
swaggerUiConfigParameters.setConfigUrl(swaggerConfigUrl);
106113
if (CollectionUtils.isEmpty(swaggerUiConfigParameters.getUrls())) {
107114
String swaggerUiUrl = swaggerUiConfig.getUrl();
108115
if (StringUtils.isEmpty(swaggerUiUrl))
109-
swaggerUiConfigParameters.setUrl(url);
116+
swaggerUiConfigParameters.setUrl(apiDocsUrl);
110117
else
111118
swaggerUiConfigParameters.setUrl(swaggerUiUrl);
112119
}
113120
else
114-
swaggerUiConfigParameters.addUrl(url);
121+
swaggerUiConfigParameters.addUrl(apiDocsUrl);
115122
}
116123
calculateOauth2RedirectUrl(uriComponentsBuilder);
117124
}
@@ -178,4 +185,24 @@ protected void calculateUiRootCommon(StringBuilder sbUrl, StringBuilder[] sbUrls
178185
sbUrl.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR));
179186
swaggerUiConfigParameters.setUiRootPath(sbUrl.toString());
180187
}
188+
189+
/**
190+
* Build api doc url string.
191+
*
192+
* @param contextPath the context path
193+
* @return the string
194+
*/
195+
protected String buildApiDocUrl(String contextPath) {
196+
return this.apiDocsUrl;
197+
}
198+
199+
/**
200+
* Build swagger config url string.
201+
*
202+
* @param contextPath the context path
203+
* @return the string
204+
*/
205+
protected String buildSwaggerConfigUrl(String contextPath) {
206+
return this.swaggerConfigUrl;
207+
}
181208
}

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
import javax.servlet.http.HttpServletRequest;
66

77
import io.swagger.v3.oas.annotations.Operation;
8-
import org.apache.commons.lang3.StringUtils;
98
import org.springdoc.core.SpringDocConfigProperties;
109
import org.springdoc.core.SwaggerUiConfigParameters;
1110
import org.springdoc.core.SwaggerUiConfigProperties;
1211

1312
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
1413
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpoint;
1514
import org.springframework.http.MediaType;
16-
import org.springframework.util.CollectionUtils;
1715
import org.springframework.web.bind.annotation.GetMapping;
1816
import org.springframework.web.bind.annotation.ResponseBody;
19-
import org.springframework.web.util.UriComponentsBuilder;
2017

2118
import static org.springdoc.core.Constants.DEFAULT_API_DOCS_ACTUATOR_URL;
2219
import static org.springdoc.core.Constants.DEFAULT_SWAGGER_UI_ACTUATOR_PATH;
@@ -83,26 +80,15 @@ protected void calculateUiRootPath(StringBuilder... sbUrls) {
8380
}
8481

8582
@Override
86-
protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriComponentsBuilder) {
87-
String apiDocsUrl = DEFAULT_API_DOCS_ACTUATOR_URL;
88-
if (StringUtils.isEmpty(swaggerUiConfig.getConfigUrl())) {
89-
String url = buildUrl(contextPath + webEndpointProperties.getBasePath(), apiDocsUrl);
90-
String swaggerConfigUrl = contextPath + webEndpointProperties.getBasePath()
91-
+ DEFAULT_PATH_SEPARATOR + DEFAULT_SWAGGER_UI_ACTUATOR_PATH
92-
+ DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
83+
protected String buildApiDocUrl(String contextPath) {
84+
return buildUrl(contextPath + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL);
85+
}
9386

94-
swaggerUiConfigParameters.setConfigUrl(swaggerConfigUrl);
95-
if (CollectionUtils.isEmpty(swaggerUiConfigParameters.getUrls())) {
96-
String swaggerUiUrl = swaggerUiConfig.getUrl();
97-
if (StringUtils.isEmpty(swaggerUiUrl))
98-
swaggerUiConfigParameters.setUrl(url);
99-
else
100-
swaggerUiConfigParameters.setUrl(swaggerUiUrl);
101-
}
102-
else
103-
swaggerUiConfigParameters.addUrl(url);
104-
}
105-
calculateOauth2RedirectUrl(uriComponentsBuilder);
87+
@Override
88+
protected String buildSwaggerConfigUrl(String contextPath) {
89+
return contextPath + webEndpointProperties.getBasePath()
90+
+ DEFAULT_PATH_SEPARATOR + DEFAULT_SWAGGER_UI_ACTUATOR_PATH
91+
+ DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
10692
}
10793

10894
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import static org.springdoc.core.Constants.MVC_SERVLET_PATH;
4040
import static org.springdoc.core.Constants.SWAGGER_CONFIG_URL;
4141
import static org.springdoc.core.Constants.SWAGGER_UI_PATH;
42+
import static org.springdoc.core.Constants.SWAGGGER_CONFIG_FILE;
43+
import static org.springframework.util.AntPathMatcher.DEFAULT_PATH_SEPARATOR;
4244

4345
/**
4446
* The type Swagger welcome.
@@ -107,4 +109,13 @@ protected String buildUrl(String contextPath, final String docsUrl) {
107109
return super.buildUrl(contextPath, docsUrl);
108110
}
109111

112+
@Override
113+
protected String buildApiDocUrl(String contextPath) {
114+
return buildUrl(contextPath, springDocConfigProperties.getApiDocs().getPath());
115+
}
116+
117+
@Override
118+
protected String buildSwaggerConfigUrl(String contextPath) {
119+
return apiDocsUrl + DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
120+
}
110121
}

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

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Map;
2424

2525
import io.swagger.v3.oas.annotations.Operation;
26-
import org.apache.commons.lang3.StringUtils;
2726
import org.springdoc.core.SpringDocConfigProperties;
2827
import org.springdoc.core.SwaggerUiConfigParameters;
2928
import org.springdoc.core.SwaggerUiConfigProperties;
@@ -35,7 +34,6 @@
3534
import org.springframework.http.MediaType;
3635
import org.springframework.http.server.reactive.ServerHttpRequest;
3736
import org.springframework.http.server.reactive.ServerHttpResponse;
38-
import org.springframework.util.CollectionUtils;
3937
import org.springframework.web.bind.annotation.GetMapping;
4038
import org.springframework.web.bind.annotation.ResponseBody;
4139
import org.springframework.web.util.UriComponentsBuilder;
@@ -74,15 +72,16 @@ public class SwaggerWelcomeActuator extends SwaggerWelcomeCommon {
7472
* @param springDocConfigProperties the spring doc config properties
7573
* @param swaggerUiConfigParameters the swagger ui config parameters
7674
* @param webEndpointProperties the web endpoint properties
75+
* @param managementServerProperties the management server properties
7776
*/
7877
public SwaggerWelcomeActuator(SwaggerUiConfigProperties swaggerUiConfig
7978
, SpringDocConfigProperties springDocConfigProperties,
8079
SwaggerUiConfigParameters swaggerUiConfigParameters,
8180
WebEndpointProperties webEndpointProperties,
8281
ManagementServerProperties managementServerProperties) {
83-
super(swaggerUiConfig, springDocConfigProperties,swaggerUiConfigParameters);
82+
super(swaggerUiConfig, springDocConfigProperties, swaggerUiConfigParameters);
8483
this.webEndpointProperties = webEndpointProperties;
85-
this.managementServerProperties=managementServerProperties;
84+
this.managementServerProperties = managementServerProperties;
8685
}
8786

8887
/**
@@ -96,7 +95,7 @@ public SwaggerWelcomeActuator(SwaggerUiConfigProperties swaggerUiConfig
9695
@GetMapping(DEFAULT_PATH_SEPARATOR)
9796
@Override
9897
public Mono<Void> redirectToUi(ServerHttpRequest request, ServerHttpResponse response) {
99-
return super.redirectToUi(request,response);
98+
return super.redirectToUi(request, response);
10099
}
101100

102101

@@ -114,34 +113,6 @@ public Map<String, Object> getSwaggerUiConfig(ServerHttpRequest request) {
114113
return super.getSwaggerUiConfig(request);
115114
}
116115

117-
/**
118-
* Build config url.
119-
*
120-
* @param contextPath the context path
121-
* @param uriComponentsBuilder the uri components builder
122-
*/
123-
@Override
124-
protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriComponentsBuilder) {
125-
if (StringUtils.isEmpty(swaggerUiConfig.getConfigUrl())) {
126-
String url = buildUrl(contextPath + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL);
127-
String swaggerConfigUrl = contextPath + webEndpointProperties.getBasePath()
128-
+ DEFAULT_PATH_SEPARATOR + DEFAULT_SWAGGER_UI_ACTUATOR_PATH
129-
+ DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
130-
131-
swaggerUiConfigParameters.setConfigUrl(swaggerConfigUrl);
132-
if (CollectionUtils.isEmpty(swaggerUiConfigParameters.getUrls())) {
133-
String swaggerUiUrl = swaggerUiConfig.getUrl();
134-
if (StringUtils.isEmpty(swaggerUiUrl))
135-
swaggerUiConfigParameters.setUrl(url);
136-
else
137-
swaggerUiConfigParameters.setUrl(swaggerUiUrl);
138-
}
139-
else
140-
swaggerUiConfigParameters.addUrl(url);
141-
}
142-
calculateOauth2RedirectUrl(uriComponentsBuilder);
143-
}
144-
145116
@Override
146117
protected void calculateUiRootPath(StringBuilder... sbUrls) {
147118
StringBuilder sbUrl = new StringBuilder();
@@ -152,8 +123,21 @@ protected void calculateUiRootPath(StringBuilder... sbUrls) {
152123
@Override
153124
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
154125
if (oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) {
155-
this.oauthPrefix = uriComponentsBuilder.path(managementServerProperties.getBasePath()+swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl);
126+
this.oauthPrefix = uriComponentsBuilder.path(managementServerProperties.getBasePath() + swaggerUiConfigParameters.getUiRootPath()).path(webJarsPrefixUrl);
156127
swaggerUiConfigParameters.setOauth2RedirectUrl(this.oauthPrefix.path(swaggerUiConfigParameters.getOauth2RedirectUrl()).build().toString());
157128
}
158129
}
130+
131+
@Override
132+
protected String buildApiDocUrl(String contextPath) {
133+
return buildUrl(contextPath + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL);
134+
}
135+
136+
@Override
137+
protected String buildSwaggerConfigUrl(String contextPath) {
138+
return contextPath + webEndpointProperties.getBasePath()
139+
+ DEFAULT_PATH_SEPARATOR + DEFAULT_SWAGGER_UI_ACTUATOR_PATH
140+
+ DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
141+
}
142+
159143
}

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.springframework.http.server.reactive.ServerHttpRequest;
4242
import org.springframework.http.server.reactive.ServerHttpResponse;
4343
import org.springframework.stereotype.Controller;
44-
import org.springframework.util.CollectionUtils;
4544
import org.springframework.web.bind.annotation.GetMapping;
4645
import org.springframework.web.bind.annotation.ResponseBody;
4746
import org.springframework.web.method.HandlerMethod;
@@ -67,16 +66,6 @@ public class SwaggerWelcomeWebFlux extends SwaggerWelcomeCommon {
6766
*/
6867
private String webfluxBasePath = StringUtils.EMPTY;
6968

70-
/**
71-
* The Swagger config url.
72-
*/
73-
private String swaggerConfigUrl;
74-
75-
/**
76-
* The Api docs url.
77-
*/
78-
private String apiDocsUrl;
79-
8069
/**
8170
* The Request mapping handler mapping.
8271
*/
@@ -153,23 +142,6 @@ protected void calculateUiRootPath(StringBuilder... sbUrls) {
153142
calculateUiRootCommon(sbUrl, sbUrls);
154143
}
155144

156-
@Override
157-
protected void buildConfigUrl(String contextPath, UriComponentsBuilder uriComponentsBuilder) {
158-
if (StringUtils.isEmpty(swaggerUiConfig.getConfigUrl())) {
159-
swaggerUiConfigParameters.setConfigUrl(swaggerConfigUrl);
160-
if (CollectionUtils.isEmpty(swaggerUiConfigParameters.getUrls())) {
161-
String swaggerUiUrl = swaggerUiConfig.getUrl();
162-
if (StringUtils.isEmpty(swaggerUiUrl))
163-
swaggerUiConfigParameters.setUrl(apiDocsUrl);
164-
else
165-
swaggerUiConfigParameters.setUrl(swaggerUiUrl);
166-
}
167-
else
168-
swaggerUiConfigParameters.addUrl(apiDocsUrl);
169-
}
170-
calculateOauth2RedirectUrl(uriComponentsBuilder);
171-
}
172-
173145
@Override
174146
protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder) {
175147
if (oauthPrefix == null && !swaggerUiConfigParameters.isValidUrl(swaggerUiConfigParameters.getOauth2RedirectUrl())) {

0 commit comments

Comments
 (0)