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)

Choose a reason for hiding this comment

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

Thank you for the quick reply.

Well you are right with the fact that in OAS 3.1 spec there are both the SchemaObject (a thing of JSON Schema spec) and the ReferenceObject (a thing of OAS spec). And that the ReferenceObject "cannot be extended with additional properties".

But, and this is what confused me initially, having a $ref in an object doesn't make it a ReferenceObject. I think this and this comments resume it best.

So in the end, here:

  • obj1 is a SchemaObject
  • additionnal properties are allowed
  • the merging of properties should be done following JSON schema's spec

What I propose is to keep this PR only for the nullable subject. And I'll open another issue for the ref handling subject. What do you think ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I do see those comments, but that does seem to conflict with what I’m reading in the specs themselves.

OpenAPI 3.1 has language like the following:

In case a Path Item Object field appears both in the defined object and the referenced object, the behavior is undefined.

Also JSON Schema has language like so:

When an object contains a $ref property, the object is considered a reference, not a schema. Therefore, any other properties you put in that object will not be treated as JSON Schema keywords and will be ignored by the validator.

The fact that I can’t find this is explicitly allowed and has defined behavior means I’d rather not support it, mainly because going beyond the spec tends to come back to cause other unexpected behavior in the weirdest ways that are hard to backpedal from.

What I propose is to keep this PR only for the nullable subject. And I'll open another issue for the ref handling subject. What do you think ?

That sounds great to me! I think an issue explicitly discussing this behavior would be great. Like with anything I could be wrong. I would just like additional input and arguments that make a good case for what the behavior should be, and point to sources (and more importantly, how it differs from anyOf/allOf composition!)

Choose a reason for hiding this comment

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

Ok I'll take care of opening the new issue and finishing this PR when I have a computer at hand.

For your 1st quote, on the Path Item Object: it is not a Schema Object. For some reason they introduced a 3rd use of the $ref here, instead of proposing to use a Reference Object. In places like the response body schema, the only definition of the $ref we should consider is the one of the Schema Object, and it is defined in JSON Schema's spec.

For your second quote: it is taken from an insert about previous draft versions (Draft 4 to Draft 7). Openapi 3.1 is relying on Draft 2020-12, which introduced the new way to handle $ref.
However openapi versions 3.0.x are relying on Draft 7, so the change came with the 3.1

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