Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -723,15 +723,17 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
if (skipNormalization(schema, visitedSchemas)) {
return schema;
}

if (ModelUtils.isNullTypeSchema(openAPI, schema)) {
return schema;
}

markSchemaAsVisited(schema, visitedSchemas);

if (ModelUtils.isArraySchema(schema)) { // array
Schema result = normalizeArraySchema(schema);
normalizeSchema(result.getItems(), visitedSchemas);
return result;
} else if (schema.getAdditionalProperties() instanceof Schema) { // map
normalizeMapSchema(schema);
normalizeSchema((Schema) schema.getAdditionalProperties(), visitedSchemas);
} else if (ModelUtils.isOneOf(schema)) { // oneOf
return normalizeOneOf(schema, visitedSchemas);
} else if (ModelUtils.isAnyOf(schema)) { // anyOf
Expand Down Expand Up @@ -768,6 +770,9 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
return schema;
} else if (schema.getProperties() != null && !schema.getProperties().isEmpty()) {
normalizeProperties(schema.getProperties(), visitedSchemas);
} else if (schema.getAdditionalProperties() instanceof Schema) { // map
normalizeMapSchema(schema);
normalizeSchema((Schema) schema.getAdditionalProperties(), visitedSchemas);
} else if (schema instanceof BooleanSchema) {
normalizeBooleanSchema(schema, visitedSchemas);
} else if (schema instanceof IntegerSchema) {
Expand Down Expand Up @@ -1011,6 +1016,7 @@ protected Schema normalizeAnyOf(Schema schema, Set<Schema> visitedSchemas) {
if (schema.getAnyOf() == null) {
return schema;
}

for (int i = 0; i < schema.getAnyOf().size(); i++) {
// normalize anyOf sub schemas one by one
Object item = schema.getAnyOf().get(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,11 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() {
Schema schema21 = openAPI.getComponents().getSchemas().get("SingleAnyOfTest");
assertEquals(schema21.getAnyOf().size(), 1);

Schema schema23 = openAPI.getComponents().getSchemas().get("PropertiesWithAnyOf");
assertEquals(((Schema) schema23.getProperties().get("anyof_nullable_string")).getAnyOf().size(), 2);
assertEquals(((Schema) schema23.getProperties().get("anyof_nullable_number")).getAnyOf().size(), 2);

// start the normalization
Map<String, String> options = new HashMap<>();
options.put("SIMPLIFY_ONEOF_ANYOF", "true");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
Expand Down Expand Up @@ -1007,6 +1012,14 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() {
assertEquals(schema22.getAnyOf(), null);
assertEquals(schema22.getTypes(), Set.of("string"));
assertEquals(schema22.getEnum().size(), 2);

Schema schema24 = openAPI.getComponents().getSchemas().get("PropertiesWithAnyOf");
assertEquals(((Schema) schema24.getProperties().get("anyof_nullable_string")).getAnyOf(), null);
assertEquals(((Schema) schema24.getProperties().get("anyof_nullable_string")).getNullable(), true);
assertEquals(((Schema) schema24.getProperties().get("anyof_nullable_string")).getTypes().size(), 1);
assertEquals(((Schema) schema24.getProperties().get("anyof_nullable_number")).getAnyOf(), null);
assertEquals(((Schema) schema24.getProperties().get("anyof_nullable_number")).getNullable(), true);
assertEquals(((Schema) schema24.getProperties().get("anyof_nullable_number")).getTypes().size(), 1);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,47 @@ public void isNullTypeSchemaTest() {
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
}

@Test
public void isNullTypeSchemaTestWith31Spec() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/null_schema_test.yaml");
Map<String, String> options = new HashMap<>();
Schema schema = openAPI.getComponents().getSchemas().get("AnyOfStringArrayOfString");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));

schema = openAPI.getComponents().getSchemas().get("IntegerRef");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));

schema = openAPI.getComponents().getSchemas().get("OneOfAnyType");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));

schema = openAPI.getComponents().getSchemas().get("AnyOfAnyType");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));

schema = openAPI.getComponents().getSchemas().get("Parent");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
// the dummy property is a ref to integer
assertFalse(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getProperties().get("dummy")));

schema = openAPI.getComponents().getSchemas().get("AnyOfTest");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
// first element (getAnyOf().get(0)) is a string. no need to test
assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(1)));
assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(2)));
assertTrue(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getAnyOf().get(3)));

schema = openAPI.getComponents().getSchemas().get("OneOfRef");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
assertFalse(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getOneOf().get(0)));

schema = openAPI.getComponents().getSchemas().get("OneOfMultiRef");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
assertFalse(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getOneOf().get(0)));
assertFalse(ModelUtils.isNullTypeSchema(openAPI, (Schema) schema.getOneOf().get(1)));

schema = openAPI.getComponents().getSchemas().get("JustDescription");
assertFalse(ModelUtils.isNullTypeSchema(openAPI, schema));
}

@Test
public void isUnsupportedSchemaTest() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/unsupported_schema_test.yaml");
Expand Down
107 changes: 107 additions & 0 deletions modules/openapi-generator/src/test/resources/3_1/null_schema_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
openapi: 3.1.0
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/person/display/{personId}:
get:
parameters:
- name: personId
in: path
required: true
description: The id of the person to retrieve
schema:
type: string
operationId: list
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/AnyOfTest"
components:
schemas:
AnyOfTest:
description: to test anyOf
anyOf:
- type: string
- type: 'null'
- type: null
- $ref: null
OneOfTest:
description: to test oneOf
oneOf:
- type: integer
- type: 'null'
- type: null
- $ref: null
OneOfTest2:
description: to test oneOf
oneOf:
- type: string
- type: 'null'
OneOfNullableTest:
description: to test oneOf nullable
oneOf:
- type: integer
- type: string
- $ref: null
Parent:
type: object
properties:
dummy:
$ref: '#/components/schemas/IntegerRef'
string_ref:
anyOf:
- $ref: '#/components/schemas/StringRef'
AnyOfStringArrayOfString:
anyOf:
- type: string
- type: array
items:
type: string
AnyOfAnyType:
anyOf:
- type: boolean
- type: array
items: {}
- type: object
- type: string
- type: number
- type: integer
AnyOfAnyTypeWithRef:
anyOf:
- type: boolean
- type: array
items: { }
- type: object
- type: string
- type: number
- $ref: '#/components/schemas/IntegerRef'
IntegerRef:
type: integer
StringRef:
type: string
OneOfAnyType:
oneOf:
- type: object
- type: boolean
- type: number
- type: string
- type: integer
- type: array
items: {}
OneOfRef:
oneOf:
- $ref: '#/components/schemas/IntegerRef'
OneOfMultiRef:
oneOf:
- $ref: '#/components/schemas/IntegerRef'
- $ref: '#/components/schemas/StringRef'
JustDescription:
description: A schema with just description
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ components:
oneOf:
- $ref: '#/components/schemas/Number'
- $ref: '#/components/schemas/Number2'
PropertiesWithAnyOf:
additionalProperties: false
type: object
properties:
anyof_nullable_string:
anyOf:
- type: string
- type: null
anyof_nullable_number:
anyOf:
- type: number
- type: null
Number:
enum:
- one
Expand Down
Loading