Skip to content

Commit daa33e0

Browse files
committed
fix CR suggestion and add tests
1 parent 7151bb3 commit daa33e0

2 files changed

Lines changed: 155 additions & 1 deletion

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringPageableScanUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static Map<String, List<String>> scanSortValidationEnums(
164164
Schema<?> enumSchema = schema;
165165
if (ModelUtils.isArraySchema(schema)) {
166166
enumSchema = schema.getItems();
167-
if (enumSchema.get$ref() != null) {
167+
if (enumSchema != null && enumSchema.get$ref() != null) {
168168
enumSchema = ModelUtils.getReferencedSchema(openAPI, enumSchema);
169169
}
170170
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package org.openapitools.codegen.languages;
2+
3+
import io.swagger.v3.oas.models.OpenAPI;
4+
import io.swagger.v3.oas.models.Operation;
5+
import io.swagger.v3.oas.models.PathItem;
6+
import io.swagger.v3.oas.models.Paths;
7+
import io.swagger.v3.oas.models.media.ArraySchema;
8+
import io.swagger.v3.oas.models.media.Schema;
9+
import io.swagger.v3.oas.models.media.StringSchema;
10+
import io.swagger.v3.oas.models.parameters.Parameter;
11+
import org.testng.annotations.Test;
12+
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.assertThatCode;
18+
19+
/**
20+
* Unit tests for {@link SpringPageableScanUtils}.
21+
*/
22+
public class SpringPageableScanUtilsTest {
23+
24+
// -------------------------------------------------------------------------
25+
// Helpers
26+
// -------------------------------------------------------------------------
27+
28+
/**
29+
* Builds an OpenAPI doc with a single GET /items operation marked x-spring-paginated.
30+
*/
31+
private static OpenAPI buildPageableOperation(Parameter sortParam) {
32+
Operation op = new Operation();
33+
op.setOperationId("listItems");
34+
op.addExtension("x-spring-paginated", true);
35+
op.addParametersItem(sortParam);
36+
37+
PathItem pathItem = new PathItem();
38+
pathItem.setGet(op);
39+
40+
Paths paths = new Paths();
41+
paths.addPathItem("/items", pathItem);
42+
43+
OpenAPI openAPI = new OpenAPI();
44+
openAPI.setPaths(paths);
45+
return openAPI;
46+
}
47+
48+
// -------------------------------------------------------------------------
49+
// scanSortValidationEnums — NPE regression for array schema without items
50+
// -------------------------------------------------------------------------
51+
52+
/**
53+
* Regression: array sort parameter with no {@code items} must not throw NPE.
54+
* {@code isArraySchema()} returns {@code true} but {@code schema.getItems()} returns
55+
* {@code null}, which would NPE on the subsequent {@code enumSchema.get$ref()} call
56+
* before the fix.
57+
*
58+
* <pre>
59+
* parameters:
60+
* - name: sort
61+
* in: query
62+
* schema:
63+
* type: array
64+
* # items: intentionally absent
65+
* </pre>
66+
*/
67+
@Test
68+
public void scanSortValidationEnums_arraySchemaWithNoItems_doesNotThrow_and_returnsEmptyMap() {
69+
// sort param: type=array but items intentionally absent
70+
Schema<?> sortSchema = new ArraySchema();
71+
// getItems() == null
72+
assertThat(sortSchema.getItems()).isNull();
73+
Parameter sortParam = new Parameter().name("sort").schema(sortSchema);
74+
OpenAPI openAPI = buildPageableOperation(sortParam);
75+
76+
// does not throw NPE
77+
assertThatCode(() -> SpringPageableScanUtils.scanSortValidationEnums(openAPI, false))
78+
.doesNotThrowAnyException();
79+
80+
// and returns empty map
81+
Map<String, List<String>> result = SpringPageableScanUtils.scanSortValidationEnums(openAPI, false);
82+
assertThat(result).isEmpty();
83+
}
84+
85+
// -------------------------------------------------------------------------
86+
// scanSortValidationEnums — happy path
87+
// -------------------------------------------------------------------------
88+
89+
/**
90+
* <pre>
91+
* parameters:
92+
* - name: sort
93+
* in: query
94+
* schema:
95+
* type: array # sort as multi-column
96+
* items:
97+
* type: string
98+
* enum: ["name,asc", "name,desc", "id,asc"]
99+
* </pre>
100+
*/
101+
@Test
102+
public void scanSortValidationEnums_arraySchemaWithEnumItems_returnsMappedEnums() {
103+
Schema<?> items = new StringSchema()._enum(List.of("name,asc", "name,desc", "id,asc"));
104+
Schema<?> sortSchema = new ArraySchema().items(items);
105+
Parameter sortParam = new Parameter().name("sort").schema(sortSchema);
106+
OpenAPI openAPI = buildPageableOperation(sortParam);
107+
108+
Map<String, List<String>> result = SpringPageableScanUtils.scanSortValidationEnums(openAPI, false);
109+
assertThat(result)
110+
.containsKey("listItems")
111+
.satisfies(m -> assertThat(m.get("listItems"))
112+
.containsExactly("name,asc", "name,desc", "id,asc"));
113+
}
114+
115+
/**
116+
* <pre>
117+
* parameters:
118+
* - name: sort
119+
* in: query
120+
* schema:
121+
* type: string # sort as single-column
122+
* enum: ["id,asc", "id,desc"]
123+
* </pre>
124+
*/
125+
@Test
126+
public void scanSortValidationEnums_nonArraySortSchemaWithEnum_returnsIt() {
127+
Schema<?> sortSchema = new StringSchema()._enum(List.of("id,asc", "id,desc"));
128+
Parameter sortParam = new Parameter().name("sort").schema(sortSchema);
129+
OpenAPI openAPI = buildPageableOperation(sortParam);
130+
131+
Map<String, List<String>> result = SpringPageableScanUtils.scanSortValidationEnums(openAPI, false);
132+
assertThat(result)
133+
.containsKey("listItems")
134+
.satisfies(m -> assertThat(m.get("listItems")).containsExactly("id,asc", "id,desc"));
135+
}
136+
137+
/**
138+
* <pre>
139+
* parameters:
140+
* - name: sort
141+
* in: query
142+
* schema:
143+
* type: string # sort as single-column
144+
* # enum: absent — no validation constraint
145+
* </pre>
146+
*/
147+
@Test
148+
public void scanSortValidationEnums_sortSchemaWithNoEnum_returnsEmptyMap() {
149+
Parameter sortParam = new Parameter().name("sort").schema(new StringSchema());
150+
OpenAPI openAPI = buildPageableOperation(sortParam);
151+
152+
assertThat(SpringPageableScanUtils.scanSortValidationEnums(openAPI, false)).isEmpty();
153+
}
154+
}

0 commit comments

Comments
 (0)