From 97e16208d5dfb7a6bc52490c4494f48deb1b5b92 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Wed, 8 Aug 2018 11:37:08 +0300 Subject: [PATCH] fix: schemes without type: object are not expandable fixes #599 --- src/services/models/Schema.ts | 2 +- src/utils/__tests__/openapi.test.ts | 11 +++++++++++ src/utils/openapi.ts | 6 +++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index 309133a7..1b9abfd0 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -104,7 +104,7 @@ export class SchemaModel { this.constraints = humanizeConstraints(schema); this.displayType = this.type; this.displayFormat = this.format; - this.isPrimitive = isPrimitiveType(schema); + this.isPrimitive = isPrimitiveType(schema, this.type); this.default = schema.default; this.readOnly = !!schema.readOnly; this.writeOnly = !!schema.writeOnly; diff --git a/src/utils/__tests__/openapi.test.ts b/src/utils/__tests__/openapi.test.ts index 9e61c908..34e847d2 100644 --- a/src/utils/__tests__/openapi.test.ts +++ b/src/utils/__tests__/openapi.test.ts @@ -187,6 +187,17 @@ describe('Utils', () => { }; expect(isPrimitiveType(schema)).toEqual(false); }); + + it('should work with externally provided type', () => { + const schema = { + properties: { + a: { + type: 'string', + }, + }, + }; + expect(isPrimitiveType(schema, 'object')).toEqual(false); + }); }); describe('openapi mergeParams', () => { diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index 2aecb645..080d010f 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -105,18 +105,18 @@ export function detectType(schema: OpenAPISchema): string { return 'any'; } -export function isPrimitiveType(schema: OpenAPISchema) { +export function isPrimitiveType(schema: OpenAPISchema, type: string | undefined = schema.type) { if (schema.oneOf !== undefined || schema.anyOf !== undefined) { return false; } - if (schema.type === 'object') { + if (type === 'object') { return schema.properties !== undefined ? Object.keys(schema.properties).length === 0 : schema.additionalProperties === undefined; } - if (schema.type === 'array') { + if (type === 'array') { if (schema.items === undefined) { return true; }