From 22e52f2b3fb6f42fa0748a303f33f3545725ac9e Mon Sep 17 00:00:00 2001 From: Anton Kozachuk <54616703+AntonKozachuk@users.noreply.github.com> Date: Sat, 13 Feb 2021 21:57:24 +0200 Subject: [PATCH] feat: add nullable lable for OAS 3.1 JSON schema --- src/services/models/Schema.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/services/models/Schema.ts b/src/services/models/Schema.ts index dd4a4d6f..0be02720 100644 --- a/src/services/models/Schema.ts +++ b/src/services/models/Schema.ts @@ -104,9 +104,17 @@ export class SchemaModel { this.title = schema.title || (isNamedDefinition(this.pointer) && JsonPointer.baseName(this.pointer)) || ''; this.description = schema.description || ''; - this.type = schema.type || detectType(schema); + + if (Array.isArray(schema.type) && schema.type.includes('null')) { + this.nullable = true; + const schemaType = schema.type.filter((t) => t !== 'null'); + this.type = schemaType.length === 1 ? schemaType[0] : schemaType; + } else { + this.nullable = !!schema.nullable; + this.type = schema.type || detectType(schema); + } + this.format = schema.format; - this.nullable = !!schema.nullable; this.enum = schema.enum || []; this.example = schema.example; this.deprecated = !!schema.deprecated; @@ -221,9 +229,12 @@ export class SchemaModel { if (name.indexOf(' or ') > -1) { name = `(${name})`; } + if (name.indexOf('null') !== -1) { + this.nullable = true; + } return name; }) - .join(' or '); + .filter((t) => t !== 'null').join(' or '); } }