From 1b7638fb329dae4ebfe3c4b80acb338846c35ac1 Mon Sep 17 00:00:00 2001 From: Yuval Tal Date: Wed, 15 Jun 2022 14:01:59 -0400 Subject: [PATCH 1/3] add support for nullable --- src/generateSpec.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/generateSpec.ts b/src/generateSpec.ts index e15602c..3813061 100644 --- a/src/generateSpec.ts +++ b/src/generateSpec.ts @@ -339,27 +339,39 @@ function getParamSchema( object, method )[index] + + const nullableMetadata = Reflect.getMetadata( + 'nullable', + object, + 'method' + ) + + let nullable; + if (nullableMetadata && nullableMetadata[method] && nullableMetadata[method][index.toString()]) { + nullable = true; + } + if (typeof type === 'function' && type.name === 'Array') { const items = explicitType ? { $ref: '#/components/schemas/' + explicitType.name } : { type: 'object' as const } - return { items, type: 'array' } + return { items, type: 'array', nullable } } if (explicitType) { - return { $ref: '#/components/schemas/' + explicitType.name } + return { $ref: '#/components/schemas/' + explicitType.name, nullable: object?.nullable } } if (typeof type === 'function') { if ( type.prototype === String.prototype || type.prototype === Symbol.prototype ) { - return { type: 'string' } + return { type: 'string', nullable } } else if (type.prototype === Number.prototype) { - return { type: 'number' } + return { type: 'number', nullable } } else if (type.prototype === Boolean.prototype) { - return { type: 'boolean' } + return { type: 'boolean', nullable } } else if (type.name !== 'Object') { - return { $ref: '#/components/schemas/' + type.name } + return { $ref: '#/components/schemas/' + type.name, nullable } } } From 0bd60105afefd631be0c7ad85f11b4266f41338b Mon Sep 17 00:00:00 2001 From: Yuval Tal Date: Wed, 15 Jun 2022 14:03:39 -0400 Subject: [PATCH 2/3] make it an SE package --- package-lock.json | 20 ++++++++++---------- package.json | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2e4783c..cc6e8bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "routing-controllers-openapi", - "version": "3.1.0", + "name": "@streamelements/routing-controllers-openapi", + "version": "3.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "routing-controllers-openapi", - "version": "3.1.0", + "name": "@streamelements/routing-controllers-openapi", + "version": "3.2.0", "license": "MIT", "dependencies": { "lodash.capitalize": "^4.2.1", @@ -13032,9 +13032,9 @@ "dev": true }, "path-parse": { - "version": "1.0.7", - "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", - "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", "dev": true }, "path-to-regexp": { @@ -14187,9 +14187,9 @@ "dev": true }, "tmpl": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz", - "integrity": "sha512-3f0uOEAQwIqGuWW2MVzYg8fV/QNnc/IpuJNG837rLuczAaLVHslWHZQj4IGiEl5Hs3kkbhwL9Ab7Hrsmuj+Smw==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/tmpl/-/tmpl-1.0.4.tgz", + "integrity": "sha1-I2QN17QtAEM5ERQIIOXPRA5SHdE=", "dev": true }, "to-fast-properties": { diff --git a/package.json b/package.json index bd0408e..e633291 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "routing-controllers-openapi", - "version": "3.1.0", + "name": "@streamelements/routing-controllers-openapi", + "version": "3.2.0", "description": "Runtime OpenAPI v3 spec generation for routing-controllers", "keywords": [ "routing-controllers", From 158291df934087920053ac36478b50ee4d05c79a Mon Sep 17 00:00:00 2001 From: Yuval Tal Date: Wed, 15 Jun 2022 21:37:14 -0400 Subject: [PATCH 3/3] add decorator to set as nullable --- package.json | 2 +- src/decorators.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e633291..de56456 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@streamelements/routing-controllers-openapi", - "version": "3.2.0", + "version": "3.2.1", "description": "Runtime OpenAPI v3 spec generation for routing-controllers", "keywords": [ "routing-controllers", diff --git a/src/decorators.ts b/src/decorators.ts index ca40ad8..1503210 100644 --- a/src/decorators.ts +++ b/src/decorators.ts @@ -158,3 +158,12 @@ export function ResponseSchema( return OpenAPI(setResponseSchema) } + +// Set the field as nullable +export function OpenAPINullable(target: any, methodKey: string, parameterIndex: number) { + let md = Reflect.getMetadata('nullable', target,'method'); + md = md || {}; + md[methodKey] = md[methodKey] || {}; + md[methodKey][parameterIndex] = { nullable: true }; + Reflect.defineMetadata('nullable', md, target, 'method'); +}