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(openapi-typescript): handle nullable schemas #2059

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gduliscouet-ubitransport

Changes

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

How to Review

I re-applied #1959 (it didn't move for a while)

I added tests for the new syntax (oneOf: [SchemaFoo, { type: "null" }]).

I also wanted to add test for both syntax, mainly for our use case.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)
  • add a minor changeset patch

Copy link

changeset-bot bot commented Dec 19, 2024

⚠️ No Changeset found

Latest commit: 4339d72

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

@gduliscouet-ubitransport
Copy link
Author

Hello @drwpow ,
Could I get your input for this schema ?
I added it to the tests, but it doesn't pass yet.
From what I see in the code, it seems to me that if we have a $ref in an object schema, all other keys are ignored. Is it me that misunderstood how $ref work ? Or is it a bug we should fix ?

Once we resolve that I'll finalize the PR

@drwpow
Copy link
Contributor

drwpow commented Dec 19, 2024

it seems to me that if we have a $ref in an object schema, all other keys are ignored. Is it me that misunderstood how $ref work ? Or is it a bug we should fix ?

That’s my understanding. If you look at the OpenAPI 3.1 specification, you’ll often see typings like this: SchemaObject | ReferenceObject. The fact that a reference object IS NOT a SchemaObject, or any other type for that matter, means they aren’t to be mixed-and-matched. If you want to combine multiple objects, you MUST use allOf/oneOf/anyOf to compose an object (but within those arrays, you are allowed to have some object be ReferenceObject and some SchemaObjects, but there’s no such thing as combining the two together.

obj1: {
type: "object",
nullable: true,
$ref: "#/components/schemas/obj1Ref",
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed, this is simply an invalid type. We shouldn’t be testing this. To combine obj1Ref and these properties, it must be within allOf or anyOf. We can also simply remove this test if it’s the only one failing (haven’t looked a the failure logs yet)

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.

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