Skip to content

Commit

Permalink
avoid npe when handling prefixItems in 3.1 spec
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Oct 1, 2024
1 parent 8ef3118 commit a83fecf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,9 @@ private Schema processNormalize31Spec(Schema schema, Set<Schema> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> inputRules = Map.of("NORMALIZE_31SPEC", "true");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules);
openAPINormalizer.normalize();
Expand All @@ -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
Expand Down
17 changes: 15 additions & 2 deletions modules/openapi-generator/src/test/resources/3_1/issue_18291.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a83fecf

Please sign in to comment.