Skip to content

Commit 0d7dc37

Browse files
committed
Calling Swagger UI via different context paths fails. Fixes #2642
1 parent 5e42cf5 commit 0d7dc37

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

springdoc-openapi-starter-webflux-ui/src/main/java/org/springdoc/webflux/ui/SwaggerIndexPageTransformer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, Sw
6969

7070
@Override
7171
public Mono<Resource> transform(ServerWebExchange serverWebExchange, Resource resource, ResourceTransformerChain resourceTransformerChain) {
72-
if (swaggerUiConfigParameters.getConfigUrl() == null)
73-
swaggerWelcomeCommon.buildFromCurrentContextPath(serverWebExchange.getRequest());
72+
swaggerWelcomeCommon.buildFromCurrentContextPath(serverWebExchange.getRequest());
7473

7574
final AntPathMatcher antPathMatcher = new AntPathMatcher();
7675
try {

springdoc-openapi-starter-webmvc-ui/src/main/java/org/springdoc/webmvc/ui/SwaggerIndexPageTransformer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public SwaggerIndexPageTransformer(SwaggerUiConfigProperties swaggerUiConfig, Sw
6969
@Override
7070
public Resource transform(HttpServletRequest request, Resource resource,
7171
ResourceTransformerChain transformerChain) throws IOException {
72-
if (swaggerUiConfigParameters.getConfigUrl() == null)
73-
swaggerWelcomeCommon.buildFromCurrentContextPath(request);
72+
swaggerWelcomeCommon.buildFromCurrentContextPath(request);
7473

7574
final AntPathMatcher antPathMatcher = new AntPathMatcher();
7675
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/" + SWAGGER_INITIALIZER_JS, resource.getURL().toString());

springdoc-openapi-starter-webmvc-ui/src/test/java/test/org/springdoc/ui/AbstractSpringDocTest.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package test.org.springdoc.ui;
2020

21+
import org.apache.commons.lang3.StringUtils;
2122
import org.springdoc.core.utils.Constants;
2223

2324
import org.springframework.boot.test.context.SpringBootTest;
@@ -33,22 +34,28 @@ public abstract class AbstractSpringDocTest extends AbstractCommonTest {
3334

3435
public static String className;
3536

36-
protected void checkJS(String fileName, String uri) throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get(uri)).andExpect(status().isOk()).andReturn();
37+
protected void checkJS(String fileName, String uri, String contextPath) throws Exception {
38+
MvcResult mvcResult = mockMvc.perform(get(contextPath+uri).contextPath(contextPath)).andExpect(status().isOk()).andReturn();
3839
String transformedIndex = mvcResult.getResponse().getContentAsString();
3940
assertTrue(transformedIndex.contains("window.ui"));
4041
assertEquals("no-store", mvcResult.getResponse().getHeader("Cache-Control"));
4142
assertEquals(this.getContent(fileName), transformedIndex.replace("\r", ""));
4243
}
4344

44-
protected void chekJS(String fileName) throws Exception {
45-
checkJS(fileName, Constants.SWAGGER_INITIALIZER_URL);
45+
protected void chekJS(String fileName,String contextPath) throws Exception {
46+
checkJS(fileName, Constants.SWAGGER_INITIALIZER_URL,contextPath);
47+
}
48+
49+
protected void chekJS(String contextPath) throws Exception {
50+
className = getClass().getSimpleName();
51+
String testNumber = className.replaceAll("[^0-9]", "");
52+
checkJS("results/app" + testNumber, Constants.SWAGGER_INITIALIZER_URL,contextPath);
4653
}
4754

4855
protected void chekJS() throws Exception {
4956
className = getClass().getSimpleName();
5057
String testNumber = className.replaceAll("[^0-9]", "");
51-
checkJS("results/app" + testNumber, Constants.SWAGGER_INITIALIZER_URL);
58+
checkJS("results/app" + testNumber, Constants.SWAGGER_INITIALIZER_URL, StringUtils.EMPTY);
5259
}
5360

5461
protected void checkJSResult(String fileName, String htmlResult) throws Exception {

springdoc-openapi-starter-webmvc-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocAppRedirectWithPrefixTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package test.org.springdoc.ui.app1;
2020

21+
import org.apache.commons.lang3.StringUtils;
2122
import org.junit.jupiter.api.Test;
2223
import org.springdoc.core.utils.Constants;
2324
import test.org.springdoc.ui.AbstractSpringDocTest;
@@ -45,7 +46,7 @@ public void shouldRedirectWithPrefix() throws Exception {
4546
.andExpect(jsonPath("validatorUrl", equalTo("")))
4647
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/documentation/swagger-ui/oauth2-redirect.html")));
4748

48-
super.checkJS("results/app1-prefix", "/documentation" + Constants.SWAGGER_INITIALIZER_URL);
49+
super.checkJS("results/app1-prefix", "/documentation" + Constants.SWAGGER_INITIALIZER_URL, StringUtils.EMPTY);
4950
}
5051

5152
@SpringBootApplication

springdoc-openapi-starter-webmvc-ui/src/test/java/test/org/springdoc/ui/app5/SpringDocApp5Test.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@
2828
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
2929
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3030
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
import static test.org.springdoc.ui.app5.SpringDocApp5Test.CONTEXT_PATH;
3132

32-
@TestPropertySource(properties = "server.servlet.context-path=/context-path")
33+
@TestPropertySource(properties = "server.servlet.context-path="+CONTEXT_PATH)
3334
public class SpringDocApp5Test extends AbstractSpringDocTest {
3435

36+
public static final String CONTEXT_PATH = "/context-path";
37+
3538
@Test
3639
public void oauth2_redirect_url_calculated_with_context_path() throws Exception {
37-
mockMvc.perform(get("/context-path/v3/api-docs/swagger-config").contextPath("/context-path"))
40+
mockMvc.perform(get(CONTEXT_PATH+"/v3/api-docs/swagger-config").contextPath("/context-path"))
3841
.andExpect(status().isOk())
3942
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/context-path/swagger-ui/oauth2-redirect.html")));
40-
super.chekJS();
43+
super.chekJS(CONTEXT_PATH);
4144
}
4245

4346
@SpringBootApplication

0 commit comments

Comments
 (0)