fix: recursion for boolean items (#2097)

This commit is contained in:
Alex Varchuk 2022-07-26 18:25:18 +03:00 committed by GitHub
parent 2384c5afe9
commit a5804db1ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 7 deletions

View File

@ -271,16 +271,13 @@ export class OpenAPIParser {
} }
if (items !== undefined && !isCircular) { if (items !== undefined && !isCircular) {
// FIXME: this is invalid here, we need to fix it in separate PR
const receiverItems = const receiverItems =
typeof receiver.items === 'boolean' typeof receiver.items === 'boolean'
? { items: receiver.items } ? {}
: receiver.items : (Object.assign({}, receiver.items) as OpenAPISchema);
? (Object.assign({}, receiver.items) as OpenAPISchema)
: {};
const subSchemaItems = const subSchemaItems =
typeof subSchema.items === 'boolean' typeof subSchema.items === 'boolean'
? { items: subSchema.items } ? {}
: (Object.assign({}, subSchema.items) as OpenAPISchema); : (Object.assign({}, subSchema.items) as OpenAPISchema);
// merge inner properties // merge inner properties
receiver.items = this.mergeAllOf( receiver.items = this.mergeAllOf(

View File

@ -140,6 +140,14 @@
"$ref": "#/components/schemas/Tag" "$ref": "#/components/schemas/Tag"
} }
}, },
"Case7": {
"type": "object",
"properties": {
"array_field": {
"$ref": "#/components/schemas/AnyArray"
}
}
},
"Cat": { "Cat": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -178,6 +186,10 @@
"type": "integer", "type": "integer",
"format": "int64", "format": "int64",
"readOnly": true "readOnly": true
},
"AnyArray": {
"type": "array",
"items": true
} }
} }
} }

View File

@ -140,6 +140,14 @@
"$ref": "#/components/schemas/Tag" "$ref": "#/components/schemas/Tag"
} }
}, },
"Case7": {
"type": "object",
"properties": {
"array_field": {
"$ref": "#/components/schemas/AnyArray"
}
}
},
"Cat": { "Cat": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -178,6 +186,10 @@
"type": "integer", "type": "integer",
"format": "int64", "format": "int64",
"readOnly": true "readOnly": true
},
"AnyArray": {
"type": "array",
"items": true
} }
} }
} }

View File

@ -242,6 +242,21 @@ describe('Models', () => {
expect(schema.minItems).toBe(1); expect(schema.minItems).toBe(1);
}, },
); );
test.each(eachArray)(
'schemaDefinition should resolve items with boolean type',
specFixture => {
const spec = require(specFixture);
const parser = new OpenAPIParser(spec, undefined, opts);
const schema = new SchemaModel(parser, spec.components.schemas.Case7, '', opts);
expect(schema.fields?.[0].schema?.type).toBe('array');
expect(schema.fields?.[0].schema?.typePrefix).toBe('Array of ');
expect(schema.fields?.[0].schema.items?.displayType).toBe('any');
expect(schema?.fields).toHaveLength(1);
expect(schema.fields?.[0].schema.pointer).toEqual('#/components/schemas/AnyArray');
expect(schema.fields?.[0].schema.isPrimitive).toBe(true);
},
);
}); });
test('should get correct fields data if it includes allOf', () => { test('should get correct fields data if it includes allOf', () => {

View File

@ -195,7 +195,7 @@ export class SchemaModel {
} else if (this.hasType('array')) { } else if (this.hasType('array')) {
if (isArray(schema.items) || isArray(schema.prefixItems)) { if (isArray(schema.items) || isArray(schema.prefixItems)) {
this.fields = buildFields(parser, schema, this.pointer, this.options, this.refsStack); this.fields = buildFields(parser, schema, this.pointer, this.options, this.refsStack);
} else if (isObject(schema.items)) { } else if (schema.items) {
this.items = new SchemaModel( this.items = new SchemaModel(
parser, parser,
schema.items as OpenAPISchema, schema.items as OpenAPISchema,