[OpenAPI 3.1] Avoid NPE when handling prefixItems (#19735)

* avoid npe when handling prefixItems in 3.1 spec

* update samples
This commit is contained in:
William Cheng 2024-10-01 17:36:11 +08:00 committed by GitHub
parent 60d0888898
commit d7ac1e4337
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 44 additions and 22 deletions

View File

@ -1280,6 +1280,9 @@ public class OpenAPINormalizer {
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;

View File

@ -600,6 +600,11 @@ public class OpenAPINormalizerTest {
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<String, String> inputRules = Map.of("NORMALIZE_31SPEC", "true");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules);
openAPINormalizer.normalize();
@ -622,6 +627,11 @@ public class OpenAPINormalizerTest {
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

View File

@ -30,7 +30,20 @@ components:
type: array
items:
type: string
Bar:
allOf:
- $ref: '#/components/schemas/Foo'
- $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

View File

@ -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

View File

@ -9,7 +9,7 @@
|------------ | ------------- | ------------- | -------------|
|**anyTypeProperty** | **Object** | | [optional] |
|**arrayProp** | **List&lt;String&gt;** | test array in 3.1 spec | [optional] |
|**refArrayPrefixItems** | **List&lt;String&gt;** | An item that was added to the queue. | [optional] |
|**refArrayPrefixItems** | **List&lt;Object&gt;** | An item that was added to the queue. | [optional] |

View File

@ -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<String> refArrayPrefixItems = new ArrayList<>();
private List<Object> refArrayPrefixItems = new ArrayList<>();
public AnyTypeTest() {
}
@ -114,12 +114,12 @@ public class AnyTypeTest {
}
public AnyTypeTest refArrayPrefixItems(List<String> refArrayPrefixItems) {
public AnyTypeTest refArrayPrefixItems(List<Object> 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 class AnyTypeTest {
* @return refArrayPrefixItems
*/
@javax.annotation.Nullable
public List<String> getRefArrayPrefixItems() {
public List<Object> getRefArrayPrefixItems() {
return refArrayPrefixItems;
}
public void setRefArrayPrefixItems(List<String> refArrayPrefixItems) {
public void setRefArrayPrefixItems(List<Object> refArrayPrefixItems) {
this.refArrayPrefixItems = refArrayPrefixItems;
}

View File

@ -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 {