Skip to content

Commit 03349cf

Browse files
committed
Calling Swagger UI via different context paths fails. Fixes #2642
1 parent 7a4d09e commit 03349cf

22 files changed

+351
-316
lines changed

Diff for: springdoc-openapi-starter-common/src/main/java/org/springdoc/core/properties/AbstractSwaggerUiConfigProperties.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import com.fasterxml.jackson.annotation.JsonIgnore;
3232
import com.fasterxml.jackson.annotation.JsonProperty;
3333
import org.apache.commons.lang3.StringUtils;
34+
import org.springdoc.core.utils.Constants;
3435

3536
import static org.springdoc.core.utils.Constants.GROUP_NAME_NOT_NULL_OR_EMPTY;
37+
import static org.springdoc.core.utils.Constants.SWAGGER_UI_OAUTH_REDIRECT_URL;
3638

3739
/**
3840
* Please refer to the swagger
@@ -45,7 +47,7 @@ public abstract class AbstractSwaggerUiConfigProperties {
4547
/**
4648
* The path for the Swagger UI pages to load. Will redirect to the springdoc.webjars.prefix property.
4749
*/
48-
protected String path;
50+
protected String path = Constants.DEFAULT_SWAGGER_UI_PATH;
4951

5052
/**
5153
* The name of a component available via the plugin system to use as the top-level layout for Swagger UI.
@@ -138,7 +140,7 @@ public abstract class AbstractSwaggerUiConfigProperties {
138140
/**
139141
* OAuth redirect URL.
140142
*/
141-
protected String oauth2RedirectUrl;
143+
protected String oauth2RedirectUrl = SWAGGER_UI_OAUTH_REDIRECT_URL;
142144

143145
/**
144146
* The Url.

Diff for: springdoc-openapi-starter-common/src/main/java/org/springdoc/core/properties/SwaggerUiConfigParameters.java

+70-6
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@
5353
* The type Swagger ui config parameters.
5454
* @author bnasslahsen
5555
*/
56-
@Lazy(false)
57-
@Configuration(proxyBeanMethods = false)
58-
@ConditionalOnProperty(name = SPRINGDOC_SWAGGER_UI_ENABLED, matchIfMissing = true)
59-
@ConditionalOnBean(SpringDocConfiguration.class)
6056
public class SwaggerUiConfigParameters extends AbstractSwaggerUiConfigProperties {
6157

6258
/**
@@ -149,6 +145,20 @@ public class SwaggerUiConfigParameters extends AbstractSwaggerUiConfigProperties
149145
*/
150146
private String uiRootPath;
151147

148+
/**
149+
* The Context path.
150+
*/
151+
private String contextPath;
152+
153+
/**
154+
* The Context path.
155+
*/
156+
private String apiDocsUrl;
157+
158+
/**
159+
* The Path prefix.
160+
*/
161+
private String pathPrefix;
152162

153163
/**
154164
* Instantiates a new Swagger ui config parameters.
@@ -157,8 +167,8 @@ public class SwaggerUiConfigParameters extends AbstractSwaggerUiConfigProperties
157167
*/
158168
public SwaggerUiConfigParameters(SwaggerUiConfigProperties swaggerUiConfig) {
159169
this.swaggerUiConfig = swaggerUiConfig;
160-
this.path = StringUtils.defaultIfBlank(swaggerUiConfig.getPath(), Constants.DEFAULT_SWAGGER_UI_PATH);
161-
this.oauth2RedirectUrl = StringUtils.defaultIfBlank(swaggerUiConfig.getOauth2RedirectUrl(), SWAGGER_UI_OAUTH_REDIRECT_URL);
170+
this.path = swaggerUiConfig.getPath();
171+
this.oauth2RedirectUrl = swaggerUiConfig.getOauth2RedirectUrl();
162172
this.layout = swaggerUiConfig.getLayout();
163173
this.configUrl = swaggerUiConfig.getConfigUrl();
164174
this.validatorUrl = swaggerUiConfig.getValidatorUrl();
@@ -325,4 +335,58 @@ private boolean isSwaggerUrlDefined(String name) {
325335
return swaggerUiConfig.getUrls().stream().anyMatch(swaggerUrl -> name.equals(swaggerUrl.getName()) && StringUtils.isNotBlank(swaggerUrl.getUrl()));
326336
return false;
327337
}
338+
339+
/**
340+
* Gets context path.
341+
*
342+
* @return the context path
343+
*/
344+
public String getContextPath() {
345+
return contextPath;
346+
}
347+
348+
/**
349+
* Sets context path.
350+
*
351+
* @param contextPath the context path
352+
*/
353+
public void setContextPath(String contextPath) {
354+
this.contextPath = contextPath;
355+
}
356+
357+
/**
358+
* Gets api docs url.
359+
*
360+
* @return the api docs url
361+
*/
362+
public String getApiDocsUrl() {
363+
return apiDocsUrl;
364+
}
365+
366+
/**
367+
* Sets api docs url.
368+
*
369+
* @param apiDocsUrl the api docs url
370+
*/
371+
public void setApiDocsUrl(String apiDocsUrl) {
372+
this.apiDocsUrl = apiDocsUrl;
373+
}
374+
375+
/**
376+
* Gets path prefix.
377+
*
378+
* @return the path prefix
379+
*/
380+
public String getPathPrefix() {
381+
return pathPrefix;
382+
}
383+
384+
/**
385+
* Sets path prefix.
386+
*
387+
* @param pathPrefix the path prefix
388+
*/
389+
public void setPathPrefix(String pathPrefix) {
390+
this.pathPrefix = pathPrefix;
391+
}
328392
}

Diff for: springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerIndexTransformer.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ public class AbstractSwaggerIndexTransformer {
6161
*/
6262
protected SwaggerUiOAuthProperties swaggerUiOAuthProperties;
6363

64-
/**
65-
* The Swagger ui config parameters.
66-
*/
67-
protected SwaggerUiConfigParameters swaggerUiConfigParameters;
68-
6964
/**
7065
* The Object mapper.
7166
*/
@@ -81,13 +76,11 @@ public class AbstractSwaggerIndexTransformer {
8176
*
8277
* @param swaggerUiConfig the swagger ui config
8378
* @param swaggerUiOAuthProperties the swagger ui o auth properties
84-
* @param swaggerUiConfigParameters the swagger ui config parameters
8579
* @param objectMapperProvider the object mapper provider
8680
*/
87-
public AbstractSwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, SwaggerUiConfigParameters swaggerUiConfigParameters, ObjectMapperProvider objectMapperProvider) {
81+
public AbstractSwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig, SwaggerUiOAuthProperties swaggerUiOAuthProperties, ObjectMapperProvider objectMapperProvider) {
8882
this.swaggerUiConfig = swaggerUiConfig;
8983
this.swaggerUiOAuthProperties = swaggerUiOAuthProperties;
90-
this.swaggerUiConfigParameters = swaggerUiConfigParameters;
9184
this.objectMapper = objectMapperProvider.jsonMapper();
9285
}
9386

@@ -148,11 +141,12 @@ protected String setConfiguredApiDocsUrl(String html){
148141
/**
149142
* Default transformations string.
150143
*
151-
* @param inputStream the input stream
144+
* @param swaggerUiConfigParameters the swagger ui config parameters
145+
* @param inputStream the input stream
152146
* @return the string
153147
* @throws IOException the io exception
154148
*/
155-
protected String defaultTransformations(InputStream inputStream) throws IOException {
149+
protected String defaultTransformations(SwaggerUiConfigParameters swaggerUiConfigParameters, InputStream inputStream) throws IOException {
156150
String html = readFullyAsString(inputStream);
157151
if (!CollectionUtils.isEmpty(swaggerUiOAuthProperties.getConfigParameters()))
158152
html = addInitOauth(html);
@@ -170,7 +164,7 @@ else if (swaggerUiConfig.getCsrf().isUseSessionStorage())
170164
html = addSyntaxHighlight(html);
171165

172166
if (swaggerUiConfig.getQueryConfigEnabled() == null || !swaggerUiConfig.getQueryConfigEnabled())
173-
html = addParameters(html);
167+
html = addParameters(html, swaggerUiConfigParameters);
174168
else
175169
html = addParameter(html, QUERY_CONFIG_ENABLED_PROPERTY, swaggerUiConfig.getQueryConfigEnabled().toString());
176170

@@ -191,7 +185,7 @@ else if (swaggerUiConfig.getCsrf().isUseSessionStorage())
191185
* @return the string
192186
* @throws JsonProcessingException the json processing exception
193187
*/
194-
protected String addParameters(String html) throws JsonProcessingException {
188+
protected String addParameters(String html, SwaggerUiConfigParameters swaggerUiConfigParameters) throws JsonProcessingException {
195189
String layout = swaggerUiConfigParameters.getLayout() != null ? swaggerUiConfigParameters.getLayout() : "StandaloneLayout";
196190
StringBuilder stringBuilder = new StringBuilder("layout: \"" + layout + "\" ,\n");
197191

Diff for: springdoc-openapi-starter-common/src/main/java/org/springdoc/ui/AbstractSwaggerWelcome.java

+33-50
Original file line numberDiff line numberDiff line change
@@ -57,45 +57,23 @@ public abstract class AbstractSwaggerWelcome {
5757
*/
5858
protected final SpringDocConfigProperties springDocConfigProperties;
5959

60-
/**
61-
* The Swagger ui calculated config.
62-
*/
63-
protected final SwaggerUiConfigParameters swaggerUiConfigParameters;
64-
65-
/**
66-
* The Swagger config url.
67-
*/
68-
protected String swaggerConfigUrl;
69-
70-
/**
71-
* The Api docs url.
72-
*/
73-
protected String apiDocsUrl;
74-
75-
/**
76-
* The Context path.
77-
*/
78-
protected String contextPath;
79-
8060
/**
8161
* Instantiates a new Abstract swagger welcome.
8262
*
8363
* @param swaggerUiConfig the swagger ui config
8464
* @param springDocConfigProperties the spring doc config properties
85-
* @param swaggerUiConfigParameters the swagger ui config parameters
8665
*/
87-
public AbstractSwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties, SwaggerUiConfigParameters swaggerUiConfigParameters) {
66+
public AbstractSwaggerWelcome(SwaggerUiConfigProperties swaggerUiConfig, SpringDocConfigProperties springDocConfigProperties) {
8867
this.swaggerUiConfig = swaggerUiConfig;
8968
this.springDocConfigProperties = springDocConfigProperties;
90-
this.swaggerUiConfigParameters = swaggerUiConfigParameters;
9169
}
9270

9371
/**
9472
* Init.
9573
*/
96-
protected void init() {
74+
protected void init(SwaggerUiConfigParameters swaggerUiConfigParameters) {
9775
springDocConfigProperties.getGroupConfigs().forEach(groupConfig -> swaggerUiConfigParameters.addGroup(groupConfig.getGroup(), groupConfig.getDisplayName()));
98-
calculateUiRootPath();
76+
calculateUiRootPath(swaggerUiConfigParameters);
9977
}
10078

10179
/**
@@ -117,30 +95,30 @@ protected String buildUrl(String contextPath, String docsUrl) {
11795
/**
11896
* Build config url.
11997
*
120-
* @param uriComponentsBuilder the uri components builder
98+
* @param swaggerUiConfigParameters the swagger ui config parameters
99+
* @param uriComponentsBuilder the uri components builder
121100
*/
122-
protected void buildConfigUrl(UriComponentsBuilder uriComponentsBuilder) {
101+
protected void buildConfigUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder) {
123102
if (StringUtils.isEmpty(swaggerUiConfig.getConfigUrl())) {
124-
apiDocsUrl = buildApiDocUrl();
125-
swaggerConfigUrl = buildSwaggerConfigUrl();
126-
swaggerUiConfigParameters.setConfigUrl(swaggerConfigUrl);
103+
buildApiDocUrl(swaggerUiConfigParameters);
104+
buildSwaggerConfigUrl(swaggerUiConfigParameters);
127105
if (CollectionUtils.isEmpty(swaggerUiConfigParameters.getUrls())) {
128106
String swaggerUiUrl = swaggerUiConfig.getUrl();
129107
if (StringUtils.isEmpty(swaggerUiUrl))
130-
swaggerUiConfigParameters.setUrl(apiDocsUrl);
108+
swaggerUiConfigParameters.setUrl(swaggerUiConfigParameters.getApiDocsUrl());
131109
else if (swaggerUiConfigParameters.isValidUrl(swaggerUiUrl))
132110
swaggerUiConfigParameters.setUrl(swaggerUiUrl);
133111
else
134-
swaggerUiConfigParameters.setUrl(buildUrlWithContextPath(swaggerUiUrl));
112+
swaggerUiConfigParameters.setUrl(buildUrlWithContextPath(swaggerUiConfigParameters, swaggerUiUrl));
135113
}
136114
else
137-
swaggerUiConfigParameters.addUrl(apiDocsUrl);
115+
swaggerUiConfigParameters.addUrl(swaggerUiConfigParameters.getApiDocsUrl());
138116
if (!CollectionUtils.isEmpty(swaggerUiConfig.getUrls())) {
139117
swaggerUiConfig.cloneUrls()
140118
.stream()
141119
.filter(swaggerUrl -> !swaggerUiConfigParameters.isValidUrl(swaggerUrl.getUrl()))
142120
.forEach(swaggerUrl -> {
143-
final var url = buildUrlWithContextPath(swaggerUrl.getUrl());
121+
final var url = buildUrlWithContextPath(swaggerUiConfigParameters, swaggerUrl.getUrl());
144122
if (!Objects.equals(url, swaggerUrl.getUrl())) {
145123
swaggerUiConfigParameters.getUrls()
146124
.stream()
@@ -150,24 +128,26 @@ else if (swaggerUiConfigParameters.isValidUrl(swaggerUiUrl))
150128
});
151129
}
152130
}
153-
calculateOauth2RedirectUrl(uriComponentsBuilder);
131+
calculateOauth2RedirectUrl(swaggerUiConfigParameters, uriComponentsBuilder);
154132
}
155133

156134
/**
157135
* Build swagger ui url string.
158136
*
159-
* @param swaggerUiUrl the swagger ui url
137+
* @param swaggerUiConfigParameters the swagger ui config parameters
138+
* @param swaggerUiUrl the swagger ui url
160139
* @return the string
161140
*/
162-
protected abstract String buildUrlWithContextPath(String swaggerUiUrl);
141+
protected abstract String buildUrlWithContextPath(SwaggerUiConfigParameters swaggerUiConfigParameters, String swaggerUiUrl);
163142

164143
/**
165144
* Gets uri components builder.
166145
*
167-
* @param sbUrl the sb url
146+
* @param swaggerUiConfigParameters the swagger ui config parameters
147+
* @param sbUrl the sb url
168148
* @return the uri components builder
169149
*/
170-
protected UriComponentsBuilder getUriComponentsBuilder(String sbUrl) {
150+
protected UriComponentsBuilder getUriComponentsBuilder(SwaggerUiConfigParameters swaggerUiConfigParameters, String sbUrl) {
171151
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl);
172152
if ((swaggerUiConfig.getQueryConfigEnabled() != null && swaggerUiConfig.getQueryConfigEnabled())) {
173153
swaggerUiConfigParameters.getConfigParameters().entrySet().stream()
@@ -185,24 +165,27 @@ protected UriComponentsBuilder getUriComponentsBuilder(String sbUrl) {
185165
/**
186166
* Calculate oauth 2 redirect url.
187167
*
188-
* @param uriComponentsBuilder the uri components builder
168+
* @param swaggerUiConfigParameters the swagger ui config parameters
169+
* @param uriComponentsBuilder the uri components builder
189170
*/
190-
protected abstract void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuilder);
171+
protected abstract void calculateOauth2RedirectUrl(SwaggerUiConfigParameters swaggerUiConfigParameters, UriComponentsBuilder uriComponentsBuilder);
191172

192173
/**
193174
* Calculate ui root path.
194175
*
195-
* @param sbUrls the sb urls
176+
* @param swaggerUiConfigParameters the swagger ui config parameters
177+
* @param sbUrls the sb urls
196178
*/
197-
protected abstract void calculateUiRootPath(StringBuilder... sbUrls);
179+
protected abstract void calculateUiRootPath(SwaggerUiConfigParameters swaggerUiConfigParameters,StringBuilder... sbUrls);
198180

199181
/**
200182
* Calculate ui root common.
201183
*
202-
* @param sbUrl the sb url
203-
* @param sbUrls the sb urls
184+
* @param swaggerUiConfigParameters the swagger ui config parameters
185+
* @param sbUrl the sb url
186+
* @param sbUrls the sb urls
204187
*/
205-
protected void calculateUiRootCommon(StringBuilder sbUrl, StringBuilder[] sbUrls) {
188+
protected void calculateUiRootCommon(SwaggerUiConfigParameters swaggerUiConfigParameters, StringBuilder sbUrl, StringBuilder[] sbUrls) {
206189
if (ArrayUtils.isNotEmpty(sbUrls))
207190
sbUrl = sbUrls[0];
208191
String swaggerPath = swaggerUiConfigParameters.getPath();
@@ -214,16 +197,16 @@ protected void calculateUiRootCommon(StringBuilder sbUrl, StringBuilder[] sbUrls
214197
/**
215198
* Build api doc url string.
216199
*
217-
* @return the string
200+
* @param swaggerUiConfigParameters the swagger ui config parameters
218201
*/
219-
protected abstract String buildApiDocUrl();
202+
protected abstract void buildApiDocUrl(SwaggerUiConfigParameters swaggerUiConfigParameters);
220203

221204
/**
222205
* Build swagger config url string.
223206
*
224-
* @return the string
207+
* @param swaggerUiConfigParameters the swagger ui config parameters
225208
*/
226-
protected abstract String buildSwaggerConfigUrl();
209+
protected abstract void buildSwaggerConfigUrl(SwaggerUiConfigParameters swaggerUiConfigParameters);
227210

228211
/**
229212
* Gets oauth2 redirect url.

0 commit comments

Comments
 (0)