Skip to content
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 handling of nullable schemas #1959

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

mikebolt
Copy link

Changes

Allow all types except UNKNOWN to be nullable.
Fixes #1821 and #1940 .

This could be considered a breaking change. Whenever users are processing a request in a server or processing a response in the client, there could be a type error due to the unhandled null case. However, this is a good change because it helps TypeScript prevent bugs.

How to Review

The actual code changes are limited to the end of the transformSchemaObjectWithComposition function in schema-object.ts.

I have added a new test containing the schema in issue #1940 . You can check that this output looks good.

I have also changed the expected values in the various github and digitalocean .ts test files (although they were possibly overwritten by the update:examples script). Wherever I added | null to a type, its corresponding schema is nullable. This is something you can double-check, although there are a few hundred cases.

Checklist

  • [ Yes ] Unit tests updated
  • [ N/A ] docs/ updated (if necessary)
  • [ Yes ] pnpm run update:examples run (only applicable for openapi-typescript)

@mikebolt mikebolt requested a review from a team as a code owner October 23, 2024 20:53
Copy link

changeset-bot bot commented Oct 23, 2024

⚠️ No Changeset found

Latest commit: 3a725fd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@drwpow drwpow added the openapi-ts Relevant to the openapi-typescript library label Oct 26, 2024
@drwpow
Copy link
Contributor

drwpow commented Nov 27, 2024

Thanks for adding this! While I agree—this may be breaking, I think it’s a sensible enough change, and people have asked for this, that I think it’s fine to introduce. Be sure to add a minor changeset patch so the version gets bumped! (see changeset comment)

The only change I’d request that we need tests for the “new” preferred syntax of oneOf: [SchemaFoo, { type: "null" }] (as opposed to the “old” syntax of nullable: true, which is still technically supported but discouraged). With those tests in there for this feature (you’ll see related tests like this), then I’d be happy to approve & ship.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[openapi-typescript] Incorrect type generated for nullable objects
2 participants