From e7b1f5c917b25d79558e70b5d4b24c2b19c7f36e Mon Sep 17 00:00:00 2001 From: Darren Yong Date: Wed, 20 Nov 2024 14:24:30 -0800 Subject: [PATCH] fix: correctly handle `nullable: false` --- packages/oas/src/lib/openapi-to-json-schema.ts | 10 ++++++---- .../oas/test/lib/openapi-to-json-schema.test.ts | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/oas/src/lib/openapi-to-json-schema.ts b/packages/oas/src/lib/openapi-to-json-schema.ts index 9ba8e1ecb..9151bd505 100644 --- a/packages/oas/src/lib/openapi-to-json-schema.ts +++ b/packages/oas/src/lib/openapi-to-json-schema.ts @@ -432,10 +432,12 @@ export function toJSONSchema(data: RMOAS.SchemaObject | boolean, opts: toJSONSch // `nullable` isn't a thing in JSON Schema but it was in OpenAPI 3.0 so we should retain and // translate it into something that's compatible with JSON Schema. if ('nullable' in schema) { - if (Array.isArray(schema.type)) { - schema.type.push('null'); - } else if (schema.type !== null && schema.type !== 'null') { - schema.type = [schema.type, 'null']; + if (schema.nullable) { + if (Array.isArray(schema.type)) { + schema.type.push('null'); + } else if (schema.type !== null && schema.type !== 'null') { + schema.type = [schema.type, 'null']; + } } delete schema.nullable; diff --git a/packages/oas/test/lib/openapi-to-json-schema.test.ts b/packages/oas/test/lib/openapi-to-json-schema.test.ts index 425a7cbcf..f603202e2 100644 --- a/packages/oas/test/lib/openapi-to-json-schema.test.ts +++ b/packages/oas/test/lib/openapi-to-json-schema.test.ts @@ -222,6 +222,23 @@ describe('`type` support', () => { }); }); + it('should correctly handle `nullable: false`', () => { + expect( + toJSONSchema({ + type: 'object', + properties: { + buster: { + type: 'string', + nullable: false, + }, + }, + }), + ).toStrictEqual({ + type: 'object', + properties: { buster: { type: 'string' } }, + }); + }); + it('should not duplicate `null` into a schema type', () => { expect(toJSONSchema({ type: ['string', 'null'], nullable: true })).toStrictEqual({ type: ['string', 'null'],