Skip to content

Commit 008aad6

Browse files
committed
fix failing tests after swagger-ui 4.9.1 upgrade
1 parent 5635e8b commit 008aad6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+532
-1534
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,6 @@ public final class Constants {
115115
*/
116116
public static final String SPRINGDOC_SWAGGER_UI_ENABLED = "springdoc.swagger-ui.enabled";
117117

118-
/**
119-
* The constant SPRING_NATIVE_LISTENER.
120-
*/
121-
public static final String SPRING_NATIVE_LISTENER = "org.springframework.nativex.NativeListener";
122-
123118
/**
124119
* The constant NULL.
125120
*/
@@ -191,6 +186,16 @@ public final class Constants {
191186
*/
192187
public static final String SWAGGER_UI_URL = SWAGGER_UI_PREFIX + INDEX_PAGE;
193188

189+
/**
190+
* The constant SWAGGER_INITIALIZER_JS.
191+
*/
192+
public static final String SWAGGER_INITIALIZER_JS = "swagger-initializer.js";
193+
194+
/**
195+
* The constant SWAGGER_INITIALIZER_URL.
196+
*/
197+
public static final String SWAGGER_INITIALIZER_URL = SWAGGER_UI_PREFIX + "/" + SWAGGER_INITIALIZER_JS;
198+
194199
/**
195200
* The constant SWAGGER_UI_OAUTH_REDIRECT_URL.
196201
*/
@@ -349,12 +354,12 @@ public final class Constants {
349354
/**
350355
* The constant SPRINGDOC_USE_ROOT_PATH.
351356
*/
352-
public static final String SPRINGDOC_USE_ROOT_PATH ="springdoc.swagger-ui.use-root-path";
357+
public static final String SPRINGDOC_USE_ROOT_PATH = "springdoc.swagger-ui.use-root-path";
353358

354359
/**
355360
* The constant DEFAULT_SWAGGER_UI_ACTUATOR_PATH.
356361
*/
357-
public static final String DEFAULT_SWAGGER_UI_ACTUATOR_PATH= "swaggerui";
362+
public static final String DEFAULT_SWAGGER_UI_ACTUATOR_PATH = "swaggerui";
358363

359364
/**
360365
* The constant DEFAULT_API_DOCS_ACTUATOR_URL.
@@ -374,7 +379,7 @@ public final class Constants {
374379
/**
375380
* The constant LINKS_SCHEMA_CUSTOMISER.
376381
*/
377-
public static final String LINKS_SCHEMA_CUSTOMISER = "linksSchemaCustomiser";
382+
public static final String LINKS_SCHEMA_CUSTOMISER = "linksSchemaCustomiser";
378383

379384
/**
380385
* Instantiates a new Constants.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class AbstractSwaggerIndexTransformer {
5252
*/
5353
private static final String PRESETS = "presets: [";
5454

55-
/**
55+
/**<
5656
* The Swagger ui o auth properties.
5757
*/
5858
protected SwaggerUiOAuthProperties swaggerUiOAuthProperties;
@@ -94,12 +94,12 @@ public AbstractSwaggerIndexTransformer(SwaggerUiConfigProperties swaggerUiConfig
9494
* @throws JsonProcessingException the json processing exception
9595
*/
9696
protected String addInitOauth(String html) throws JsonProcessingException {
97-
StringBuilder stringBuilder = new StringBuilder("window.ui = ui\n");
97+
StringBuilder stringBuilder = new StringBuilder(" });\n");
9898
stringBuilder.append("ui.initOAuth(\n");
9999
String json = objectMapper.writeValueAsString(swaggerUiOAuthProperties.getConfigParameters());
100100
stringBuilder.append(json);
101101
stringBuilder.append(")");
102-
return html.replace("window.ui = ui", stringBuilder.toString());
102+
return html.replace(" });", stringBuilder.toString());
103103
}
104104

105105
/**
@@ -197,9 +197,9 @@ protected String addParameters(String html) throws JsonProcessingException {
197197
}
198198

199199
private String addParameter(String html, String key, String value) {
200-
StringBuilder stringBuilder = new StringBuilder("const ui = SwaggerUIBundle({\n");
200+
StringBuilder stringBuilder = new StringBuilder("window.ui = SwaggerUIBundle({\n");
201201
stringBuilder.append(key + ": \"" + value + "\",");
202-
return html.replace("const ui = SwaggerUIBundle({", stringBuilder.toString());
202+
return html.replace("window.ui = SwaggerUIBundle({", stringBuilder.toString());
203203
}
204204

205205
/**

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.springframework.web.servlet.resource.ResourceTransformerChain;
3535
import org.springframework.web.servlet.resource.TransformedResource;
3636

37+
import static org.springdoc.core.Constants.SWAGGER_INITIALIZER_JS;
38+
3739
/**
3840
* The type Swagger index transformer.
3941
* @author bnasslahsen
@@ -64,7 +66,7 @@ public Resource transform(HttpServletRequest request, Resource resource,
6466
swaggerWelcomeCommon.buildFromCurrentContextPath(request);
6567

6668
final AntPathMatcher antPathMatcher = new AntPathMatcher();
67-
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/index.html", resource.getURL().toString());
69+
boolean isIndexFound = antPathMatcher.match("**/swagger-ui/**/" + SWAGGER_INITIALIZER_JS, resource.getURL().toString());
6870

6971
if (isIndexFound) {
7072
String html = defaultTransformations(resource.getInputStream());

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,25 @@ public abstract class AbstractSpringDocTest extends AbstractCommonTest {
3333

3434
public static String className;
3535

36-
private static final String DEFAULT_SWAGGER_UI_URL= Constants.SWAGGER_UI_URL;
37-
38-
protected void checkHTML(String fileName, String uri)throws Exception {
36+
protected void checkJS(String fileName, String uri)throws Exception {
3937
MvcResult mvcResult = mockMvc.perform(get(uri)).andExpect(status().isOk()).andReturn();
4038
String transformedIndex = mvcResult.getResponse().getContentAsString();
41-
assertTrue(transformedIndex.contains("Swagger UI"));
39+
assertTrue(transformedIndex.contains("window.ui"));
4240
assertEquals(this.getContent(fileName), transformedIndex.replace("\r", ""));
4341
}
4442

45-
protected void chekHTML(String fileName) throws Exception {
46-
checkHTML( fileName, DEFAULT_SWAGGER_UI_URL);
43+
protected void chekJS(String fileName) throws Exception {
44+
checkJS( fileName, Constants.SWAGGER_INITIALIZER_URL);
4745
}
4846

49-
protected void chekHTML() throws Exception {
47+
protected void chekJS() throws Exception {
5048
className = getClass().getSimpleName();
5149
String testNumber = className.replaceAll("[^0-9]", "");
52-
checkHTML( "results/app" + testNumber, DEFAULT_SWAGGER_UI_URL);
50+
checkJS( "results/app" + testNumber, Constants.SWAGGER_INITIALIZER_URL);
5351
}
5452

55-
protected void checkHTMLResult(String fileName, String htmlResult)throws Exception {
56-
assertTrue(htmlResult.contains("Swagger UI"));
53+
protected void checkJSResult(String fileName, String htmlResult)throws Exception {
54+
assertTrue(htmlResult.contains("window.ui"));
5755
assertEquals(this.getContent(fileName), htmlResult.replace("\r", ""));
5856
}
5957
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void shouldDisplaySwaggerUiPage() throws Exception {
4040

4141
@Test
4242
public void originalIndex() throws Exception {
43-
super.chekHTML();
43+
super.chekJS();
4444
}
4545

4646
@SpringBootApplication

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.springdoc.core.Constants;
2223
import test.org.springdoc.ui.AbstractSpringDocTest;
2324

2425
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -44,7 +45,7 @@ public void shouldRedirectWithPrefix() throws Exception {
4445
.andExpect(jsonPath("validatorUrl", equalTo("")))
4546
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/documentation/swagger-ui/oauth2-redirect.html")));
4647

47-
super.checkHTML("results/app1-prefix", "/documentation/swagger-ui/index.html");
48+
super.checkJS("results/app1-prefix", "/documentation"+ Constants.SWAGGER_INITIALIZER_URL);
4849
}
4950

5051
@SpringBootApplication

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
package test.org.springdoc.ui.app1;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.springdoc.core.Constants;
2223
import test.org.springdoc.ui.AbstractSpringDocTest;
2324

2425
import org.springframework.boot.autoconfigure.SpringBootApplication;
2526
import org.springframework.test.context.TestPropertySource;
2627
import org.springframework.test.web.servlet.MvcResult;
2728

2829
import static org.hamcrest.Matchers.containsString;
29-
import static org.junit.jupiter.api.Assertions.assertEquals;
30-
import static org.junit.jupiter.api.Assertions.assertTrue;
3130
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3231
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3332
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -48,9 +47,9 @@ public void should_display_swaggerui_page() throws Exception {
4847
.andExpect(status().isOk())
4948
.andExpect(content().string(containsString("Swagger UI")));
5049

51-
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isOk()).andReturn();
50+
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test"+ Constants.SWAGGER_INITIALIZER_URL).contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isOk()).andReturn();
5251
String transformedIndex = mvcResult.getResponse().getContentAsString();
53-
checkHTMLResult("results/app1-contextpath", transformedIndex);
52+
checkJSResult("results/app1-contextpath", transformedIndex);
5453
}
5554

5655
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app10/SpringDocApp10Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void shouldDisplaySwaggerUiPage() throws Exception {
4242

4343
@Test
4444
public void originalIndex() throws Exception {
45-
super.chekHTML();
45+
super.chekJS();
4646
}
4747

4848
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app12/SpringDocApp12Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class SpringDocApp12Test extends AbstractSpringDocTest {
3030

3131
@Test
3232
void transformed_index_with_oauth() throws Exception {
33-
super.chekHTML();
33+
super.chekJS();
3434
}
3535

3636
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app21/SpringDocApp21Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void testAddSwaggerUiVersionToPath() throws Exception {
3737
.andExpect(status().isFound())
3838
.andExpect(header().string("Location", "/swagger-ui/index.html"));
3939

40-
super.chekHTML();
40+
super.chekJS();
4141
}
4242

4343
@Test

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app23/SpringDocApp23Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SpringDocApp23Test extends AbstractSpringDocTest {
2929

3030
@Test
3131
void transformed_index_with_queryConfigEnabled() throws Exception {
32-
super.chekHTML();
32+
super.chekJS();
3333
}
3434

3535
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app24/SpringDocApp24Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
4040
.andExpect(status().isFound())
4141
.andExpect(header().string("Location", "/swagger-ui/index.html"));
4242

43-
super.chekHTML();
43+
super.chekJS();
4444
}
4545

4646
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app25/SpringDocApp25Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void shouldRedirectWithConfiguredParams() throws Exception {
4545
mockMvc.perform(get("/baf/batz/swagger-config"))
4646
.andExpect(status().isOk()).andExpect(jsonPath("$.validatorUrl", is("/foo/validate")));
4747

48-
super.chekHTML();
48+
super.chekJS();
4949
}
5050

5151
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app26/SpringDocApp26Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void shouldRedirectWithBaseLayout() throws Exception {
3737
.andExpect(status().isFound())
3838
.andExpect(header().string("Location", "/swagger-ui/index.html"));
3939

40-
super.chekHTML();
40+
super.chekJS();
4141
}
4242

4343
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app27/SpringDocApp27Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void shouldRedirectWithQueryParams() throws Exception {
3939
.andExpect(status().isFound())
4040
.andExpect(header().string("Location", "/swagger-ui/index.html?operationsSorter=method&configUrl=/v3/api-docs/swagger-config"));
4141

42-
super.chekHTML();
42+
super.chekJS();
4343
}
4444

4545

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app28/SpringDocApp28Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void shouldRedirectWithFilter() throws Exception {
3636
mockMvc.perform(get("/swagger-ui.html"))
3737
.andExpect(status().isFound())
3838
.andExpect(header().string("Location", "/swagger-ui/index.html"));
39-
super.chekHTML();
39+
super.chekJS();
4040
}
4141

4242

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void oauth2_redirect_url_calculated_with_context_path() throws Exception
3737
mockMvc.perform(get("/context-path/v3/api-docs/swagger-config").contextPath("/context-path"))
3838
.andExpect(status().isOk())
3939
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/context-path/swagger-ui/oauth2-redirect.html")));
40-
super.chekHTML();
40+
super.chekJS();
4141
}
4242

4343
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app5/SpringDocOauthServletPathsTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
package test.org.springdoc.ui.app5;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.springdoc.core.Constants;
2223
import test.org.springdoc.ui.AbstractSpringDocTest;
2324

2425
import org.springframework.boot.autoconfigure.SpringBootApplication;
2526
import org.springframework.test.context.TestPropertySource;
2627
import org.springframework.test.web.servlet.MvcResult;
2728

2829
import static org.hamcrest.CoreMatchers.equalTo;
29-
import static org.junit.jupiter.api.Assertions.assertEquals;
30-
import static org.junit.jupiter.api.Assertions.assertTrue;
3130
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3231
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3332
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -45,9 +44,9 @@ public void should_display_oauth2_redirect_page() throws Exception {
4544

4645
mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/oauth2-redirect.html").servletPath("/servlet-path").contextPath("/context-path")).andExpect(status().isOk()).andReturn();
4746

48-
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").servletPath("/servlet-path").contextPath("/context-path")).andExpect(status().isOk()).andReturn();
47+
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test"+ Constants.SWAGGER_INITIALIZER_URL).servletPath("/servlet-path").contextPath("/context-path")).andExpect(status().isOk()).andReturn();
4948
String transformedIndex = mvcResult.getResponse().getContentAsString();
50-
checkHTMLResult("results/app5-contextpath", transformedIndex);
49+
checkJSResult("results/app5-contextpath", transformedIndex);
5150
}
5251

5352
@Test

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app7/SpringDocApp7Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class SpringDocApp7Test extends AbstractSpringDocTest {
3232

3333
@Test
3434
public void transformed_index_with_oauth() throws Exception {
35-
super.chekHTML();
35+
super.chekJS();
3636
}
3737

3838
@SpringBootApplication

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app9/SpringDocApp9Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SpringDocApp9Test extends AbstractSpringDocTest {
3737

3838
@Test
3939
public void transformed_index_with_oauth() throws Exception {
40-
super.chekHTML();
40+
super.chekJS();
4141
}
4242

4343
@SpringBootApplication

0 commit comments

Comments
 (0)