Skip to content

Commit

Permalink
fix: recursion for boolean items (#2097)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk authored Jul 26, 2022
1 parent 2384c5a commit a5804db
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,13 @@ export class OpenAPIParser {
}

if (items !== undefined && !isCircular) {
// FIXME: this is invalid here, we need to fix it in separate PR
const receiverItems =
typeof receiver.items === 'boolean'
? { items: receiver.items }
: receiver.items
? (Object.assign({}, receiver.items) as OpenAPISchema)
: {};
? {}
: (Object.assign({}, receiver.items) as OpenAPISchema);
const subSchemaItems =
typeof subSchema.items === 'boolean'
? { items: subSchema.items }
? {}
: (Object.assign({}, subSchema.items) as OpenAPISchema);
// merge inner properties
receiver.items = this.mergeAllOf(
Expand Down
12 changes: 12 additions & 0 deletions src/services/__tests__/fixtures/3.1/prefixItems.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@
"$ref": "#/components/schemas/Tag"
}
},
"Case7": {
"type": "object",
"properties": {
"array_field": {
"$ref": "#/components/schemas/AnyArray"
}
}
},
"Cat": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -178,6 +186,10 @@
"type": "integer",
"format": "int64",
"readOnly": true
},
"AnyArray": {
"type": "array",
"items": true
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/services/__tests__/fixtures/arrayItems.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@
"$ref": "#/components/schemas/Tag"
}
},
"Case7": {
"type": "object",
"properties": {
"array_field": {
"$ref": "#/components/schemas/AnyArray"
}
}
},
"Cat": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -178,6 +186,10 @@
"type": "integer",
"format": "int64",
"readOnly": true
},
"AnyArray": {
"type": "array",
"items": true
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/services/__tests__/models/Schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,21 @@ describe('Models', () => {
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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class SchemaModel {
} else if (this.hasType('array')) {
if (isArray(schema.items) || isArray(schema.prefixItems)) {
this.fields = buildFields(parser, schema, this.pointer, this.options, this.refsStack);
} else if (isObject(schema.items)) {
} else if (schema.items) {
this.items = new SchemaModel(
parser,
schema.items as OpenAPISchema,
Expand Down

0 comments on commit a5804db

Please sign in to comment.