Skip to content

Commit 2100570

Browse files
dgswanDmitry Lebedko
authored and
Dmitry Lebedko
committed
Fix SpringDocApp193Test for Java 21 and above. In Java 21 the getFirst() and getLast() methods were added to the java.util.List` interface. Those are the POJO getters, therefore Jackson will add them during serialization. So there are two different expected results for Java prior 21 and starting from Java 21. Fixes #2442.
1 parent 71a0684 commit 2100570

File tree

2 files changed

+186
-3
lines changed

2 files changed

+186
-3
lines changed

Diff for: springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app193/SpringDocApp193Test.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,37 @@
2323
package test.org.springdoc.api.v30.app193;
2424

2525

26-
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
26+
import org.apache.commons.lang3.JavaVersion;
27+
import org.junit.jupiter.api.Test;
28+
import org.springdoc.core.utils.Constants;
29+
import org.springframework.boot.test.context.SpringBootTest;
30+
import org.springframework.test.web.servlet.MvcResult;
31+
import test.org.springdoc.api.AbstractCommonTest;
2732

2833
import org.springframework.boot.autoconfigure.SpringBootApplication;
2934

30-
31-
public class SpringDocApp193Test extends AbstractSpringDocV30Test {
35+
import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast;
36+
import static org.hamcrest.Matchers.is;
37+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
38+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
39+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
40+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
41+
42+
@SpringBootTest
43+
public class SpringDocApp193Test extends AbstractCommonTest {
44+
45+
@Test
46+
public void testApp() throws Exception {
47+
final MvcResult mockMvcResult = mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL)).andExpect(status().isOk())
48+
.andExpect(jsonPath("$.openapi", is("3.0.1"))).andReturn();
49+
final String result = mockMvcResult.getResponse().getContentAsString();
50+
// In Java 21 the getFirst() and getLast() methods were added to the List interface.
51+
// Those are the POJO getters, therefore Jackson will add them during serialization.
52+
// So there are two different expected results for Java prior 21 and starting from Java 21.
53+
final var expectedResponseFile = isJavaVersionAtLeast(JavaVersion.JAVA_21) ? "app193-1.json" : "app193.json";
54+
final String expected = getContent("results/3.0.1/" + expectedResponseFile);
55+
assertEquals(expected, result, true);
56+
}
3257

3358
@SpringBootApplication
3459
static class SpringDocTestApp {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/test": {
15+
"get": {
16+
"tags": [
17+
"basic-controller"
18+
],
19+
"summary": "get",
20+
"description": "Provides a list of books.",
21+
"operationId": "get",
22+
"responses": {
23+
"200": {
24+
"description": "OK",
25+
"content": {
26+
"*/*": {
27+
"schema": {
28+
"oneOf": [
29+
{
30+
"$ref": "#/components/schemas/Books"
31+
}
32+
]
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
},
40+
"/test1": {
41+
"get": {
42+
"tags": [
43+
"basic-controller"
44+
],
45+
"summary": "get1",
46+
"description": "Provides an animal.",
47+
"operationId": "get1",
48+
"responses": {
49+
"200": {
50+
"description": "OK",
51+
"content": {
52+
"*/*": {
53+
"schema": {
54+
"oneOf": [
55+
{
56+
"$ref": "#/components/schemas/Cat"
57+
},
58+
{
59+
"$ref": "#/components/schemas/Dog"
60+
}
61+
]
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
},
70+
"components": {
71+
"schemas": {
72+
"Book":
73+
{
74+
"type": "object",
75+
"properties":
76+
{
77+
"title":
78+
{
79+
"type": "string"
80+
}
81+
},
82+
"description": "Represents a Book."
83+
},
84+
"Books": {
85+
"type": "array",
86+
"description": "Represents a list of Books.",
87+
"allOf": [
88+
{
89+
"$ref": "#/components/schemas/Knowledge"
90+
},
91+
{
92+
"type": "object",
93+
"properties": {
94+
"empty": {
95+
"type": "boolean"
96+
},
97+
"first":
98+
{
99+
"$ref": "#/components/schemas/Book"
100+
},
101+
"last":
102+
{
103+
"$ref": "#/components/schemas/Book"
104+
}
105+
}
106+
}
107+
]
108+
},
109+
"Knowledge": {
110+
"type": "object",
111+
"description": "Represents the knowledge."
112+
},
113+
"Animal": {
114+
"type": "object",
115+
"description": "Represents an Animal class."
116+
},
117+
"Cat": {
118+
"type": "object",
119+
"description": "Represents a Cat class.",
120+
"allOf": [
121+
{
122+
"$ref": "#/components/schemas/Animal"
123+
},
124+
{
125+
"type": "object",
126+
"properties": {
127+
"speed": {
128+
"type": "integer",
129+
"format": "int32"
130+
}
131+
}
132+
}
133+
]
134+
},
135+
"Dog": {
136+
"type": "object",
137+
"description": "Represents a Dog class.",
138+
"allOf": [
139+
{
140+
"$ref": "#/components/schemas/Animal"
141+
},
142+
{
143+
"type": "object",
144+
"properties": {
145+
"name": {
146+
"type": "string"
147+
},
148+
"age": {
149+
"type": "integer",
150+
"format": "int32"
151+
}
152+
}
153+
}
154+
]
155+
}
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)