Skip to content

Commit fd022ee

Browse files
committed
Reverse proxy context path aware support for manually provided files. Fixes #1453.
1 parent ce39c7b commit fd022ee

File tree

9 files changed

+156
-4
lines changed

9 files changed

+156
-4
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/SwaggerUiConfigParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public SwaggerUiConfigParameters(SwaggerUiConfigProperties swaggerUiConfig) {
129129
this.showExtensions = swaggerUiConfig.getShowExtensions();
130130
this.supportedSubmitMethods = swaggerUiConfig.getSupportedSubmitMethods();
131131
this.url = swaggerUiConfig.getUrl();
132-
this.urls = swaggerUiConfig.getUrls() == null ? new HashSet<>() : swaggerUiConfig.getUrls();
132+
this.urls = swaggerUiConfig.getUrls() == null ? new HashSet<>() : swaggerUiConfig.cloneUrls();
133133
this.urlsPrimaryName = swaggerUiConfig.getUrlsPrimaryName();
134134
this.groupsOrder = swaggerUiConfig.getGroupsOrder();
135135
this.tryItOutEnabled = swaggerUiConfig.getTryItOutEnabled();

springdoc-openapi-common/src/main/java/org/springdoc/core/SwaggerUiConfigProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
package org.springdoc.core;
2222

23+
import java.util.Set;
24+
import java.util.stream.Collectors;
25+
2326
import org.apache.commons.lang3.StringUtils;
2427

2528
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -372,4 +375,14 @@ public SyntaxHighlight getSyntaxHighlight() {
372375
public void setSyntaxHighlight(SyntaxHighlight syntaxHighlight) {
373376
this.syntaxHighlight = syntaxHighlight;
374377
}
378+
379+
/**
380+
* Clone urls set.
381+
*
382+
* @return the set
383+
*/
384+
public Set<SwaggerUrl> cloneUrls(){
385+
return this.urls.stream().map(swaggerUrl -> new SwaggerUrl(swaggerUrl.getName(), swaggerUrl.getUrl())).collect(Collectors.toSet());
386+
}
387+
375388
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,32 @@ protected void buildConfigUrl(UriComponentsBuilder uriComponentsBuilder) {
120120
String swaggerUiUrl = swaggerUiConfig.getUrl();
121121
if (StringUtils.isEmpty(swaggerUiUrl))
122122
swaggerUiConfigParameters.setUrl(apiDocsUrl);
123-
else
123+
else if (swaggerUiConfigParameters.isValidUrl(swaggerUiUrl))
124124
swaggerUiConfigParameters.setUrl(swaggerUiUrl);
125+
else
126+
swaggerUiConfigParameters.setUrl(buildUrlWithContextPath(swaggerUiUrl));
125127
}
126128
else
127129
swaggerUiConfigParameters.addUrl(apiDocsUrl);
130+
if (!CollectionUtils.isEmpty(swaggerUiConfig.getUrls())) {
131+
swaggerUiConfigParameters.setUrls(swaggerUiConfig.cloneUrls());
132+
swaggerUiConfigParameters.getUrls().forEach(swaggerUrl -> {
133+
if (!swaggerUiConfigParameters.isValidUrl(swaggerUrl.getUrl()))
134+
swaggerUrl.setUrl(buildUrlWithContextPath(swaggerUrl.getUrl()));
135+
});
136+
}
128137
}
129138
calculateOauth2RedirectUrl(uriComponentsBuilder);
130139
}
131140

141+
/**
142+
* Build swagger ui url string.
143+
*
144+
* @param swaggerUiUrl the swagger ui url
145+
* @return the string
146+
*/
147+
protected abstract String buildUrlWithContextPath(String swaggerUiUrl);
148+
132149
/**
133150
* Gets uri components builder.
134151
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ protected String buildApiDocUrl() {
8585
return buildUrl(contextPath + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL);
8686
}
8787

88+
@Override
89+
protected String buildUrlWithContextPath(String swaggerUiUrl) {
90+
return buildUrl(contextPath + webEndpointProperties.getBasePath(), swaggerUiUrl);
91+
}
92+
8893
@Override
8994
protected String buildSwaggerConfigUrl() {
9095
return contextPath + webEndpointProperties.getBasePath()

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,14 @@ protected String buildUrl(String contextPath, final String docsUrl) {
122122
*/
123123
@Override
124124
protected String buildApiDocUrl() {
125+
return buildUrlWithContextPath(springDocConfigProperties.getApiDocs().getPath());
126+
}
127+
128+
@Override
129+
protected String buildUrlWithContextPath(String swaggerUiUrl) {
125130
if (this.pathPrefix == null)
126131
this.pathPrefix = springWebProvider.findPathPrefix(springDocConfigProperties);
127-
return buildUrl(contextPath + pathPrefix, springDocConfigProperties.getApiDocs().getPath());
132+
return buildUrl(contextPath + pathPrefix, swaggerUiUrl);
128133
}
129134

130135
/**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.ui.app29;
20+
21+
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.test.context.TestPropertySource;
26+
27+
import static org.hamcrest.CoreMatchers.equalTo;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
@TestPropertySource(properties ={
33+
"springdoc.swagger-ui.url=/api-docs/xxx/v1/openapi.yml",
34+
"server.servlet.context-path=/context-path"
35+
} )
36+
public class SpringDocApp29Test extends AbstractSpringDocTest {
37+
38+
@SpringBootApplication
39+
static class SpringDocTestApp {}
40+
41+
@Test
42+
public void test_url_with_context() throws Exception {
43+
mockMvc.perform(get("/context-path/v3/api-docs/swagger-config").contextPath("/context-path"))
44+
.andExpect(status().isOk())
45+
.andExpect(jsonPath("url", equalTo("/context-path/api-docs/xxx/v1/openapi.yml")))
46+
.andExpect(jsonPath("configUrl", equalTo("/context-path/v3/api-docs/swagger-config")))
47+
.andExpect(jsonPath("validatorUrl", equalTo("")))
48+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/context-path/swagger-ui/oauth2-redirect.html")));
49+
}
50+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.ui.app30;
20+
21+
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.test.context.TestPropertySource;
26+
27+
import static org.hamcrest.CoreMatchers.equalTo;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
@TestPropertySource(properties ={
33+
"springdoc.swagger-ui..urls[0].url=/api-docs/xxx/v1/openapi.yml",
34+
"springdoc.swagger-ui.urls[0].name=toto",
35+
"server.servlet.context-path=/context-path"
36+
} )
37+
public class SpringDocApp30Test extends AbstractSpringDocTest {
38+
39+
@SpringBootApplication
40+
static class SpringDocTestApp {}
41+
42+
@Test
43+
public void test_urls_with_context() throws Exception {
44+
mockMvc.perform(get("/context-path/v3/api-docs/swagger-config").contextPath("/context-path"))
45+
.andExpect(status().isOk())
46+
.andExpect(jsonPath("url").doesNotExist())
47+
.andExpect(jsonPath("urls[0].url", equalTo("/context-path/api-docs/xxx/v1/openapi.yml")))
48+
.andExpect(jsonPath("urls[0].name", equalTo("toto")))
49+
.andExpect(jsonPath("validatorUrl", equalTo("")))
50+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/context-path/swagger-ui/oauth2-redirect.html")));
51+
}
52+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ protected String buildApiDocUrl() {
133133
return buildUrl(contextPath + webEndpointProperties.getBasePath(), DEFAULT_API_DOCS_ACTUATOR_URL);
134134
}
135135

136+
@Override
137+
protected String buildUrlWithContextPath(String swaggerUiUrl) {
138+
return buildUrl(contextPath + webEndpointProperties.getBasePath(), swaggerUiUrl);
139+
}
140+
136141
@Override
137142
protected String buildSwaggerConfigUrl() {
138143
return contextPath + webEndpointProperties.getBasePath()

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ protected void calculateOauth2RedirectUrl(UriComponentsBuilder uriComponentsBuil
115115
*/
116116
@Override
117117
protected String buildApiDocUrl() {
118+
return buildUrlWithContextPath(springDocConfigProperties.getApiDocs().getPath());
119+
}
120+
121+
@Override
122+
protected String buildUrlWithContextPath(String swaggerUiUrl) {
118123
if (this.pathPrefix == null)
119124
this.pathPrefix = springWebProvider.findPathPrefix(springDocConfigProperties);
120-
return buildUrl(this.contextPath + this.pathPrefix, springDocConfigProperties.getApiDocs().getPath());
125+
return buildUrl(this.contextPath + this.pathPrefix,swaggerUiUrl );
121126
}
122127

123128
/**

0 commit comments

Comments
 (0)