Skip to content

Commit e51e3a9

Browse files
authored
Merge pull request #1012 from kaibra/feature/forward-query-params-when-redirecting-to-swagger-ui
Feature/forward query params when redirecting to swagger UI
2 parents 43efad0 + 0e94617 commit e51e3a9

11 files changed

+124
-84
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ protected String redirectToUi(HttpServletRequest request) {
3030
buildConfigUrl(request.getContextPath(), ServletUriComponentsBuilder.fromCurrentContextPath());
3131
String sbUrl = swaggerUiConfigParameters.getUiRootPath() + SWAGGER_UI_URL;
3232
UriComponentsBuilder uriBuilder = getUriComponentsBuilder(sbUrl);
33+
34+
// forward all queryParams from original request
35+
request.getParameterMap().forEach(uriBuilder::queryParam);
36+
3337
return UrlBasedViewResolver.REDIRECT_URL_PREFIX + uriBuilder.build().encode().toString();
3438
}
3539

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocApp1Test.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,33 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22-
import test.org.springdoc.ui.AbstractSpringDocTest;
23-
2422
import org.springframework.boot.autoconfigure.SpringBootApplication;
25-
import org.springframework.test.web.servlet.MvcResult;
23+
import test.org.springdoc.ui.AbstractSpringDocTest;
2624

27-
import static org.junit.jupiter.api.Assertions.assertEquals;
28-
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.hamcrest.Matchers.containsString;
2926
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3028
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3129

3230

3331
public class SpringDocApp1Test extends AbstractSpringDocTest {
3432

35-
@Test
36-
public void shouldDisplaySwaggerUiPage() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/index.html")).andExpect(status().isOk()).andReturn();
38-
String contentAsString = mvcResult.getResponse().getContentAsString();
39-
assertTrue(contentAsString.contains("Swagger UI"));
40-
}
41-
42-
@Test
43-
public void originalIndex() throws Exception {
44-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/index.html")).andExpect(status().isOk()).andReturn();
45-
String transformedIndex = mvcResult.getResponse().getContentAsString();
46-
assertTrue(transformedIndex.contains("Swagger UI"));
47-
assertEquals(getExpectedResult(), transformedIndex);
48-
}
49-
50-
@SpringBootApplication
51-
static class SpringDocTestApp {}
33+
@Test
34+
public void shouldDisplaySwaggerUiPage() throws Exception {
35+
mockMvc.perform(get("/swagger-ui/index.html"))
36+
.andExpect(status().isOk())
37+
.andExpect(content().string(containsString("Swagger UI")));
38+
}
39+
40+
@Test
41+
public void originalIndex() throws Exception {
42+
mockMvc.perform(get("/swagger-ui/index.html"))
43+
.andExpect(status().isOk())
44+
.andExpect(content().string(containsString("Swagger UI")))
45+
.andExpect(content().string(getExpectedResult()));
46+
}
47+
48+
@SpringBootApplication
49+
static class SpringDocTestApp {
50+
}
5251
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocConfigPathsTest.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,32 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22-
import test.org.springdoc.ui.AbstractSpringDocTest;
23-
2422
import org.springframework.boot.autoconfigure.SpringBootApplication;
2523
import org.springframework.test.context.TestPropertySource;
26-
import org.springframework.test.web.servlet.MvcResult;
24+
import test.org.springdoc.ui.AbstractSpringDocTest;
2725

28-
import static org.junit.jupiter.api.Assertions.assertTrue;
26+
import static org.hamcrest.Matchers.containsString;
2927
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
28+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3029
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3130

3231
@TestPropertySource(properties = {
33-
"springdoc.swagger-ui.path=/test/swagger.html",
34-
"server.servlet.context-path=/context-path",
35-
"spring.mvc.servlet.path=/servlet-path"
32+
"springdoc.swagger-ui.path=/test/swagger.html",
33+
"server.servlet.context-path=/context-path",
34+
"spring.mvc.servlet.path=/servlet-path"
3635
})
3736
public class SpringDocConfigPathsTest extends AbstractSpringDocTest {
3837

39-
@Test
40-
public void should_display_swaggerui_page() throws Exception {
41-
mockMvc.perform(get("/context-path/servlet-path/test/swagger.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isFound()).andReturn();
42-
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isOk()).andReturn();
43-
String contentAsString = mvcResult.getResponse().getContentAsString();
44-
assertTrue(contentAsString.contains("Swagger UI"));
45-
}
38+
@Test
39+
public void should_display_swaggerui_page() throws Exception {
40+
mockMvc.perform(get("/context-path/servlet-path/test/swagger.html").contextPath("/context-path").servletPath("/servlet-path"))
41+
.andExpect(status().isFound());
42+
mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").contextPath("/context-path").servletPath("/servlet-path"))
43+
.andExpect(status().isOk())
44+
.andExpect(content().string(containsString("Swagger UI")));
45+
}
4646

47-
@SpringBootApplication
48-
static class SpringDocTestApp {}
47+
@SpringBootApplication
48+
static class SpringDocTestApp {
49+
}
4950
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectConfigUrlTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233
@TestPropertySource(properties = {
@@ -37,11 +38,9 @@ public class SpringDocRedirectConfigUrlTest extends AbstractSpringDocTest {
3738

3839
@Test
3940
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
40-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
41-
.andExpect(status().isFound()).andReturn();
42-
43-
String locationHeader = mvcResult.getResponse().getHeader("Location");
44-
assertEquals("/swagger-ui/index.html?configUrl=/foo/bar", locationHeader);
41+
mockMvc.perform(get("/swagger-ui.html"))
42+
.andExpect(status().isFound())
43+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/foo/bar"));
4544
}
4645

4746
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectDefaultTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@
2626

2727
import static org.junit.jupiter.api.Assertions.assertEquals;
2828
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
2930
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3031

3132
public class SpringDocRedirectDefaultTest extends AbstractSpringDocTest {
3233

3334
@Test
3435
public void shouldRedirectWithDefaultQueryParams() throws Exception {
35-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
36-
.andExpect(status().isFound()).andReturn();
37-
38-
String locationHeader = mvcResult.getResponse().getHeader("Location");
39-
assertEquals("/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config", locationHeader);
36+
mockMvc.perform(get("/swagger-ui.html"))
37+
.andExpect(status().isFound())
38+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config"));
4039
}
4140

4241
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectFilterTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233
@TestPropertySource(properties = "springdoc.swagger-ui.filter=false")
3334
public class SpringDocRedirectFilterTest extends AbstractSpringDocTest {
3435

3536
@Test
3637
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38-
.andExpect(status().isFound()).andReturn();
39-
40-
String locationHeader = mvcResult.getResponse().getHeader("Location");
41-
assertEquals("/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&filter=false", locationHeader);
38+
mockMvc.perform(get("/swagger-ui.html"))
39+
.andExpect(status().isFound())
40+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&filter=false"));
4241
}
4342

4443
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectLayoutTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233
@TestPropertySource(properties = "springdoc.swagger-ui.layout=BaseLayout")
3334
public class SpringDocRedirectLayoutTest extends AbstractSpringDocTest {
3435

3536
@Test
3637
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38-
.andExpect(status().isFound()).andReturn();
39-
40-
String locationHeader = mvcResult.getResponse().getHeader("Location");
41-
assertEquals("/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&layout=BaseLayout", locationHeader);
38+
mockMvc.perform(get("/swagger-ui.html"))
39+
.andExpect(status().isFound())
40+
.andExpect(header().string("Location", "/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&layout=BaseLayout"));
4241
}
4342

4443
@SpringBootApplication
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.app1;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import test.org.springdoc.ui.AbstractSpringDocTest;
24+
25+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
28+
29+
public class SpringDocRedirectOriginalQueryParamsTest extends AbstractSpringDocTest {
30+
31+
@Test
32+
public void shouldRedirectWithOriginalQueryParams() throws Exception {
33+
mockMvc.perform(get("/swagger-ui.html").queryParam("paramA", "123").queryParam("paramB", "e n c o d e d ! % &"))
34+
.andExpect(status().isFound())
35+
.andExpect(header().string("Location",
36+
"/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&paramA=123&paramB=e%20n%20c%20o%20d%20e%20d%20!%20%25%20%26"));
37+
}
38+
39+
@Test
40+
public void shouldRedirectWithOriginalQueryParamsHavingMultipleValues() throws Exception {
41+
mockMvc.perform(get("/swagger-ui.html").queryParam("paramA", "1", "2", "3"))
42+
.andExpect(status().isFound())
43+
.andExpect(header().string("Location",
44+
"/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&paramA=1&paramA=2&paramA=3"));
45+
}
46+
47+
@SpringBootApplication
48+
static class SpringDocTestApp {
49+
}
50+
51+
}

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectQueryParams1Test.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3031
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3132

3233
@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params-without-oauth2=true")
3334
public class SpringDocRedirectQueryParams1Test extends AbstractSpringDocTest {
3435

3536
@Test
3637
public void shouldRedirectWithQueryParamsWithoutOauth2() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38-
.andExpect(status().isFound()).andReturn();
39-
40-
String locationHeader = mvcResult.getResponse().getHeader("Location");
41-
assertEquals("/swagger-ui/index.html?url=/v3/api-docs", locationHeader);
38+
mockMvc.perform(get("/swagger-ui.html"))
39+
.andExpect(status().isFound())
40+
.andExpect(header().string("Location", "/swagger-ui/index.html?url=/v3/api-docs"));
4241
}
4342

4443
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app1/SpringDocRedirectQueryParams2Test.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,22 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22-
import test.org.springdoc.ui.AbstractSpringDocTest;
23-
2422
import org.springframework.boot.autoconfigure.SpringBootApplication;
2523
import org.springframework.test.context.TestPropertySource;
26-
import org.springframework.test.web.servlet.MvcResult;
24+
import test.org.springdoc.ui.AbstractSpringDocTest;
2725

28-
import static org.junit.jupiter.api.Assertions.assertEquals;
2926
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
27+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
3028
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3129

3230
@TestPropertySource(properties = "springdoc.swagger-ui.display-query-params=true")
3331
public class SpringDocRedirectQueryParams2Test extends AbstractSpringDocTest {
3432

3533
@Test
3634
public void shouldRedirectWithQueryParams() throws Exception {
37-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui.html"))
38-
.andExpect(status().isFound()).andReturn();
39-
40-
String locationHeader = mvcResult.getResponse().getHeader("Location");
41-
assertEquals("/swagger-ui/index.html?oauth2RedirectUrl=http://localhost/swagger-ui/oauth2-redirect.html&url=/v3/api-docs", locationHeader);
35+
mockMvc.perform(get("/swagger-ui.html"))
36+
.andExpect(status().isFound())
37+
.andExpect(header().string("Location", "/swagger-ui/index.html?oauth2RedirectUrl=http://localhost/swagger-ui/oauth2-redirect.html&url=/v3/api-docs"));
4238
}
4339

4440
@SpringBootApplication

0 commit comments

Comments
 (0)