Skip to content

Commit d7ac1e4

Browse files
authored
[OpenAPI 3.1] Avoid NPE when handling prefixItems (#19735)
* avoid npe when handling prefixItems in 3.1 spec * update samples
1 parent 60d0888 commit d7ac1e4

File tree

7 files changed

+44
-22
lines changed

7 files changed

+44
-22
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,9 @@ private Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchemas)
12801280
Schema updatedItems = normalizeSchema(schema.getItems(), visitedSchemas);
12811281
as.setItems(updatedItems);
12821282
}
1283+
} else {
1284+
// when items is not defined, default to any type
1285+
as.setItems(new Schema());
12831286
}
12841287

12851288
return as;

modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,11 @@ public void testOpenAPINormalizerProcessingArraySchema31Spec() {
600600
assertEquals(((Schema) schema5.getProperties().get("arrayOfStrings")).getItems().getType(), null);
601601
assertEquals(((Schema) schema5.getProperties().get("arrayOfStrings")).getItems().getTypes().contains("string"), true);
602602

603+
Schema schema7 = openAPI.getComponents().getSchemas().get("ArrayWithPrefixItems");
604+
assertEquals(((Schema) schema7.getProperties().get("with_prefixitems")).getItems(), null);
605+
assertNotEquals(((Schema) schema7.getProperties().get("with_prefixitems")).getPrefixItems(), null);
606+
assertEquals(((Schema) schema7.getProperties().get("without_items")).getItems(), null);
607+
603608
Map<String, String> inputRules = Map.of("NORMALIZE_31SPEC", "true");
604609
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules);
605610
openAPINormalizer.normalize();
@@ -622,6 +627,11 @@ public void testOpenAPINormalizerProcessingArraySchema31Spec() {
622627
assertEquals(((Schema) schema6.getProperties().get("arrayOfStrings")).getItems().getTypes().contains("string"), true);
623628
assertEquals(((Schema) schema6.getProperties().get("arrayOfStrings")).getItems().getType(), "string");
624629
assertEquals(((Schema) schema6.getProperties().get("arrayOfStrings")).getType(), "array");
630+
631+
Schema schema8 = openAPI.getComponents().getSchemas().get("ArrayWithPrefixItems");
632+
assertNotEquals(((Schema) schema8.getProperties().get("with_prefixitems")).getItems(), null);
633+
assertEquals(((Schema) schema8.getProperties().get("with_prefixitems")).getPrefixItems(), null);
634+
assertNotEquals(((Schema) schema8.getProperties().get("without_items")).getItems(), null);
625635
}
626636

627637
@Test

modules/openapi-generator/src/test/resources/3_1/issue_18291.yaml

+15-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,20 @@ components:
3030
type: array
3131
items:
3232
type: string
33-
3433
Bar:
3534
allOf:
36-
- $ref: '#/components/schemas/Foo'
35+
- $ref: '#/components/schemas/Foo'
36+
ArrayWithPrefixItems:
37+
type: object
38+
properties:
39+
with_prefixitems:
40+
type: array
41+
prefixItems:
42+
- type: number
43+
title: Longitude
44+
- type: number
45+
title: Latitude
46+
maxItems: 2
47+
minItems: 2
48+
without_items:
49+
type: array

samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml

+2-6
Original file line numberDiff line numberDiff line change
@@ -1099,9 +1099,7 @@ components:
10991099
ref_array_prefix_items:
11001100
description: |
11011101
An item that was added to the queue.
1102-
items:
1103-
description: TODO default missing array inner type to string
1104-
type: string
1102+
items: {}
11051103
maxItems: 5
11061104
minItems: 3
11071105
type: array
@@ -1112,9 +1110,7 @@ components:
11121110
ArrayPrefixItems:
11131111
description: |
11141112
An item that was added to the queue.
1115-
items:
1116-
description: TODO default missing array inner type to string
1117-
type: string
1113+
items: {}
11181114
maxItems: 5
11191115
minItems: 3
11201116
type: array

samples/client/petstore/java/okhttp-gson-3.1/docs/AnyTypeTest.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
|------------ | ------------- | ------------- | -------------|
1010
|**anyTypeProperty** | **Object** | | [optional] |
1111
|**arrayProp** | **List&lt;String&gt;** | test array in 3.1 spec | [optional] |
12-
|**refArrayPrefixItems** | **List&lt;String&gt;** | An item that was added to the queue. | [optional] |
12+
|**refArrayPrefixItems** | **List&lt;Object&gt;** | An item that was added to the queue. | [optional] |
1313

1414

1515

samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/AnyTypeTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class AnyTypeTest {
6363

6464
public static final String SERIALIZED_NAME_REF_ARRAY_PREFIX_ITEMS = "ref_array_prefix_items";
6565
@SerializedName(SERIALIZED_NAME_REF_ARRAY_PREFIX_ITEMS)
66-
private List<String> refArrayPrefixItems = new ArrayList<>();
66+
private List<Object> refArrayPrefixItems = new ArrayList<>();
6767

6868
public AnyTypeTest() {
6969
}
@@ -114,12 +114,12 @@ public void setArrayProp(List<String> arrayProp) {
114114
}
115115

116116

117-
public AnyTypeTest refArrayPrefixItems(List<String> refArrayPrefixItems) {
117+
public AnyTypeTest refArrayPrefixItems(List<Object> refArrayPrefixItems) {
118118
this.refArrayPrefixItems = refArrayPrefixItems;
119119
return this;
120120
}
121121

122-
public AnyTypeTest addRefArrayPrefixItemsItem(String refArrayPrefixItemsItem) {
122+
public AnyTypeTest addRefArrayPrefixItemsItem(Object refArrayPrefixItemsItem) {
123123
if (this.refArrayPrefixItems == null) {
124124
this.refArrayPrefixItems = new ArrayList<>();
125125
}
@@ -132,11 +132,11 @@ public AnyTypeTest addRefArrayPrefixItemsItem(String refArrayPrefixItemsItem) {
132132
* @return refArrayPrefixItems
133133
*/
134134
@javax.annotation.Nullable
135-
public List<String> getRefArrayPrefixItems() {
135+
public List<Object> getRefArrayPrefixItems() {
136136
return refArrayPrefixItems;
137137
}
138138

139-
public void setRefArrayPrefixItems(List<String> refArrayPrefixItems) {
139+
public void setRefArrayPrefixItems(List<Object> refArrayPrefixItems) {
140140
this.refArrayPrefixItems = refArrayPrefixItems;
141141
}
142142

samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/models/numeric_enum_testing.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ pub enum NumericEnumTesting {
2424

2525
}
2626

27-
impl ToString for NumericEnumTesting {
28-
fn to_string(&self) -> String {
29-
match self {
30-
Self::Variant0 => String::from("0"),
31-
Self::Variant1 => String::from("1"),
32-
Self::Variant2 => String::from("2"),
33-
Self::Variant3 => String::from("3"),
34-
}
27+
impl std::fmt::Display for NumericEnumTesting {
28+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29+
write!(f, "{}", match self {
30+
Self::Variant0 => "0",
31+
Self::Variant1 => "1",
32+
Self::Variant2 => "2",
33+
Self::Variant3 => "3",
34+
})
3535
}
3636
}
3737
impl Default for NumericEnumTesting {

0 commit comments

Comments
 (0)