diff --git a/packages/oas/src/lib/openapi-to-json-schema.ts b/packages/oas/src/lib/openapi-to-json-schema.ts index 9ba8e1ec..9151bd50 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 425a7cbc..f603202e 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'],