From a83fecf4ae410f4669fb4bf60753a6752b7930cd Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 1 Oct 2024 16:12:56 +0800 Subject: [PATCH 1/2] avoid npe when handling prefixItems in 3.1 spec --- .../openapitools/codegen/OpenAPINormalizer.java | 3 +++ .../codegen/OpenAPINormalizerTest.java | 10 ++++++++++ .../src/test/resources/3_1/issue_18291.yaml | 17 +++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java index d3317375321b..1b1dd2c6538e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java @@ -1280,6 +1280,9 @@ private Schema processNormalize31Spec(Schema schema, Set visitedSchemas) Schema updatedItems = normalizeSchema(schema.getItems(), visitedSchemas); as.setItems(updatedItems); } + } else { + // when items is not defined, default to any type + as.setItems(new Schema()); } return as; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java index 70c04397e233..3537cc5b8974 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java @@ -600,6 +600,11 @@ public void testOpenAPINormalizerProcessingArraySchema31Spec() { assertEquals(((Schema) schema5.getProperties().get("arrayOfStrings")).getItems().getType(), null); assertEquals(((Schema) schema5.getProperties().get("arrayOfStrings")).getItems().getTypes().contains("string"), true); + Schema schema7 = openAPI.getComponents().getSchemas().get("ArrayWithPrefixItems"); + assertEquals(((Schema) schema7.getProperties().get("with_prefixitems")).getItems(), null); + assertNotEquals(((Schema) schema7.getProperties().get("with_prefixitems")).getPrefixItems(), null); + assertEquals(((Schema) schema7.getProperties().get("without_items")).getItems(), null); + Map inputRules = Map.of("NORMALIZE_31SPEC", "true"); OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules); openAPINormalizer.normalize(); @@ -622,6 +627,11 @@ public void testOpenAPINormalizerProcessingArraySchema31Spec() { assertEquals(((Schema) schema6.getProperties().get("arrayOfStrings")).getItems().getTypes().contains("string"), true); assertEquals(((Schema) schema6.getProperties().get("arrayOfStrings")).getItems().getType(), "string"); assertEquals(((Schema) schema6.getProperties().get("arrayOfStrings")).getType(), "array"); + + Schema schema8 = openAPI.getComponents().getSchemas().get("ArrayWithPrefixItems"); + assertNotEquals(((Schema) schema8.getProperties().get("with_prefixitems")).getItems(), null); + assertEquals(((Schema) schema8.getProperties().get("with_prefixitems")).getPrefixItems(), null); + assertNotEquals(((Schema) schema8.getProperties().get("without_items")).getItems(), null); } @Test diff --git a/modules/openapi-generator/src/test/resources/3_1/issue_18291.yaml b/modules/openapi-generator/src/test/resources/3_1/issue_18291.yaml index 8f5b9db4e1a8..8ea8a90357f6 100644 --- a/modules/openapi-generator/src/test/resources/3_1/issue_18291.yaml +++ b/modules/openapi-generator/src/test/resources/3_1/issue_18291.yaml @@ -30,7 +30,20 @@ components: type: array items: type: string - Bar: allOf: - - $ref: '#/components/schemas/Foo' \ No newline at end of file + - $ref: '#/components/schemas/Foo' + ArrayWithPrefixItems: + type: object + properties: + with_prefixitems: + type: array + prefixItems: + - type: number + title: Longitude + - type: number + title: Latitude + maxItems: 2 + minItems: 2 + without_items: + type: array From 29ba17149c8a30ba77ce354eb4b0f46b172adb33 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 1 Oct 2024 16:35:09 +0800 Subject: [PATCH 2/2] update samples --- .../java/okhttp-gson-3.1/api/openapi.yaml | 8 ++------ .../java/okhttp-gson-3.1/docs/AnyTypeTest.md | 2 +- .../openapitools/client/model/AnyTypeTest.java | 10 +++++----- .../src/models/numeric_enum_testing.rs | 16 ++++++++-------- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml b/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml index 489ebd8ff4ba..28503edd8b0d 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml +++ b/samples/client/petstore/java/okhttp-gson-3.1/api/openapi.yaml @@ -1099,9 +1099,7 @@ components: ref_array_prefix_items: description: | An item that was added to the queue. - items: - description: TODO default missing array inner type to string - type: string + items: {} maxItems: 5 minItems: 3 type: array @@ -1112,9 +1110,7 @@ components: ArrayPrefixItems: description: | An item that was added to the queue. - items: - description: TODO default missing array inner type to string - type: string + items: {} maxItems: 5 minItems: 3 type: array diff --git a/samples/client/petstore/java/okhttp-gson-3.1/docs/AnyTypeTest.md b/samples/client/petstore/java/okhttp-gson-3.1/docs/AnyTypeTest.md index a9f7efb0ff66..a96a098853db 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/docs/AnyTypeTest.md +++ b/samples/client/petstore/java/okhttp-gson-3.1/docs/AnyTypeTest.md @@ -9,7 +9,7 @@ |------------ | ------------- | ------------- | -------------| |**anyTypeProperty** | **Object** | | [optional] | |**arrayProp** | **List<String>** | test array in 3.1 spec | [optional] | -|**refArrayPrefixItems** | **List<String>** | An item that was added to the queue. | [optional] | +|**refArrayPrefixItems** | **List<Object>** | An item that was added to the queue. | [optional] | diff --git a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/AnyTypeTest.java b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/AnyTypeTest.java index 5c5bfd2d8003..699cb9a7d40b 100644 --- a/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/AnyTypeTest.java +++ b/samples/client/petstore/java/okhttp-gson-3.1/src/main/java/org/openapitools/client/model/AnyTypeTest.java @@ -63,7 +63,7 @@ public class AnyTypeTest { public static final String SERIALIZED_NAME_REF_ARRAY_PREFIX_ITEMS = "ref_array_prefix_items"; @SerializedName(SERIALIZED_NAME_REF_ARRAY_PREFIX_ITEMS) - private List refArrayPrefixItems = new ArrayList<>(); + private List refArrayPrefixItems = new ArrayList<>(); public AnyTypeTest() { } @@ -114,12 +114,12 @@ public void setArrayProp(List arrayProp) { } - public AnyTypeTest refArrayPrefixItems(List refArrayPrefixItems) { + public AnyTypeTest refArrayPrefixItems(List refArrayPrefixItems) { this.refArrayPrefixItems = refArrayPrefixItems; return this; } - public AnyTypeTest addRefArrayPrefixItemsItem(String refArrayPrefixItemsItem) { + public AnyTypeTest addRefArrayPrefixItemsItem(Object refArrayPrefixItemsItem) { if (this.refArrayPrefixItems == null) { this.refArrayPrefixItems = new ArrayList<>(); } @@ -132,11 +132,11 @@ public AnyTypeTest addRefArrayPrefixItemsItem(String refArrayPrefixItemsItem) { * @return refArrayPrefixItems */ @javax.annotation.Nullable - public List getRefArrayPrefixItems() { + public List getRefArrayPrefixItems() { return refArrayPrefixItems; } - public void setRefArrayPrefixItems(List refArrayPrefixItems) { + public void setRefArrayPrefixItems(List refArrayPrefixItems) { this.refArrayPrefixItems = refArrayPrefixItems; } diff --git a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/models/numeric_enum_testing.rs b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/models/numeric_enum_testing.rs index 9b8f0cb04651..e3681aa61778 100644 --- a/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/models/numeric_enum_testing.rs +++ b/samples/client/petstore/rust/reqwest/petstore-async-tokensource/src/models/numeric_enum_testing.rs @@ -24,14 +24,14 @@ pub enum NumericEnumTesting { } -impl ToString for NumericEnumTesting { - fn to_string(&self) -> String { - match self { - Self::Variant0 => String::from("0"), - Self::Variant1 => String::from("1"), - Self::Variant2 => String::from("2"), - Self::Variant3 => String::from("3"), - } +impl std::fmt::Display for NumericEnumTesting { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", match self { + Self::Variant0 => "0", + Self::Variant1 => "1", + Self::Variant2 => "2", + Self::Variant3 => "3", + }) } } impl Default for NumericEnumTesting {