Skip to content

Commit

Permalink
fix: correctly handle nullable: false
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenyong committed Nov 20, 2024
1 parent a44e5b6 commit e7b1f5c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/oas/src/lib/openapi-to-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions packages/oas/test/lib/openapi-to-json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit e7b1f5c

Please sign in to comment.