Skip to content

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

Merged
merged 1 commit into from
Jul 10, 2023
Merged

Fix 3.1 nullable types #1221

merged 1 commit into from
Jul 10, 2023

Conversation

drwpow
Copy link
Contributor

@drwpow drwpow commented Jul 10, 2023

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

  • Unit tests updated
  • README updated
  • examples/ directory updated (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Jul 10, 2023

🦋 Changeset detected

Latest commit: a260520

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-typescript Patch

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

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 10, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: a260520
Status: ✅  Deploy successful!
Preview URL: https://97a99f96.openapi-ts.pages.dev
Branch Preview URL: https://nullable-fixes.openapi-ts.pages.dev

View logs

@drwpow drwpow force-pushed the nullable-fixes branch 7 times, most recently from 6f59347 to dbbee15 Compare July 10, 2023 15:13
return schemaObject.nullable ? tsUnionOf(itemType, "null") : itemType;
}

// polymorphic, or 3.1 nullable
if (Array.isArray(schemaObject.type)) {
Copy link
Contributor Author

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 })));
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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", () => {
Copy link
Contributor Author

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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is much better 🎉

@drwpow drwpow merged commit 4e96e9d into main Jul 10, 2023
@drwpow drwpow deleted the nullable-fixes branch July 10, 2023 15:22
@github-actions github-actions bot mentioned this pull request Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support null in OpenAPI 3.1 union types
1 participant