-
-
Notifications
You must be signed in to change notification settings - Fork 528
Fix 3.1 nullable types #1221
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
Fix 3.1 nullable types #1221
Conversation
🦋 Changeset detectedLatest commit: a260520 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Deploying with
|
Latest commit: |
a260520
|
Status: | ✅ Deploy successful! |
Preview URL: | https://97a99f96.openapi-ts.pages.dev |
Branch Preview URL: | https://nullable-fixes.openapi-ts.pages.dev |
6f59347
to
dbbee15
Compare
return schemaObject.nullable ? tsUnionOf(itemType, "null") : itemType; | ||
} | ||
|
||
// polymorphic, or 3.1 nullable | ||
if (Array.isArray(schemaObject.type)) { |
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.
moving this is arbitrary; just a little easier to reason with.
|
||
// polymorphic, or 3.1 nullable | ||
if (Array.isArray(schemaObject.type)) { | ||
return tsUnionOf(...schemaObject.type.map((t) => transformSchemaObject({ ...schemaObject, type: t }, { path, ctx }))); |
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.
Big change: tsUnionOf
rather than tsOneOf
here is more correct, and now handles 3.1 nullable types correctly. There shouldn’t be any performance impact from this.
| { allOf: (SchemaObject | ReferenceObject)[]; anyOf?: (SchemaObject | ReferenceObject)[]; required?: string[] } | ||
| { allOf?: (SchemaObject | ReferenceObject)[]; anyOf: (SchemaObject | ReferenceObject)[]; required?: string[] } | ||
| {} | ||
); | ||
|
||
export interface StringSubtype { |
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.
Unnecessary pulling out the subtypes into their own names; I had hoped it would help TS inference a little better, but no noticeable impact. Still, may come in handy in the future.
@@ -49,12 +49,6 @@ describe("Schema Object", () => { | |||
status?: "complete" | "incomplete"; | |||
}`); | |||
}); | |||
|
|||
test("nullable", () => { |
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.
Removed the disparate nullable tests and grouped them together (which helped identify several missing test cases)
describe("polymorphic", () => { | ||
test("nullish primitive", () => { | ||
const generated = transformSchemaObject({ type: ["string", "boolean", "number", "null"] }, options); | ||
expect(generated).toBe("OneOf<[string, boolean, number, null]>"); | ||
expect(generated).toBe("string | boolean | number | 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.
this is much better 🎉
Changes
Fixes #1215, #1197. Adds better tests for 3.1 nullable types and improves handling. Also removes the
OneOf<>
helper for simple types.How to Review
Tests should pass.
Checklist
examples/
directory updated (only applicable for openapi-typescript)