Skip to content

Commit

Permalink
fix: correctly handle nullable: false (#915)
Browse files Browse the repository at this point in the history
| 🚥 Resolves RM-11473 |
| :------------------- |

## 🧰 Changes
Currently, if a schema has `nullable: false`, (idk why since it defaults
to false? maybe some tooling explicitly defines it as such) we aren't
properly handling it as we're only checking for the existence of a
`nullable` property.

I added a condition to only add the `null` type if `nullable: true`. If
it's `false`, we don't want to do anything with it, but we still want to
delete it so it doesn't show up in the schema. Added a test too.


## 🧬 QA & Testing

Provide as much information as you can on how to test what you've done.
  • Loading branch information
darrenyong authored Nov 20, 2024
1 parent a44e5b6 commit 927ed2d
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 927ed2d

Please sign in to comment.