-
Notifications
You must be signed in to change notification settings - Fork 430
Add support for nullable primitives #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,6 +145,30 @@ rules.set('Transform const to singleton enum', schema => { | |
| } | ||
| }) | ||
|
|
||
| rules.set('Transform nullable -> anyOf', schema => { | ||
| if (schema.nullable === true) { | ||
| delete schema.nullable | ||
|
|
||
| const copiedSchema = {...schema} | ||
|
|
||
| // This stuff should not be in `anyOf`. | ||
| delete copiedSchema.id | ||
| delete copiedSchema.nullable | ||
|
|
||
| // This stuff will be in `anyOf` instead. | ||
| delete schema.format | ||
| delete schema.type | ||
|
|
||
| schema.anyOf = [ | ||
| copiedSchema, | ||
| // @ts-expect-error | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you rm this?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get a TS error without it:
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! Can you find a way to fix the error? eg. to add an empty |
||
| { | ||
| type: 'null' | ||
| } | ||
| ] | ||
| } | ||
| }) | ||
|
|
||
| export function normalize(rootSchema: LinkedJSONSchema, filename: string, options: Options): NormalizedJSONSchema { | ||
| rules.forEach(rule => traverse(rootSchema, schema => rule(schema, filename, options))) | ||
| return rootSchema as NormalizedJSONSchema | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "Normalize nullable to anyOf", | ||
| "in": { | ||
| "id": "foo", | ||
| "type": "integer", | ||
| "format": "int32", | ||
| "nullable": true | ||
| }, | ||
| "out": { | ||
| "id": "foo", | ||
| "anyOf": [ | ||
| { | ||
| "type": "integer", | ||
| "format": "int32" | ||
| }, | ||
| { | ||
| "type": "null" | ||
| } | ||
| ] | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove all of these
deletes? Unclear why you need them (156 is already included in 150; 155+159+160 I'm not sure what they're here for).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bcherny
I think we need all of these deletes. After normalizing, we no longer want
nullable,format, ortypeto appear in the root of the schema. That's what theanyOfis replacing.For example, the following schema:
{ "id": "foo", "type": "integer", "format": "int32", "nullable": true }Should be normalized to this:
Notice how
nullableis gone, andtypeandformathave moved into theanyOf