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

Handling of nullable for $ref in OpenAPI 3.0 #39

Closed
bluenote10 opened this issue Mar 26, 2020 · 7 comments
Closed

Handling of nullable for $ref in OpenAPI 3.0 #39

bluenote10 opened this issue Mar 26, 2020 · 7 comments

Comments

@bluenote10
Copy link

bluenote10 commented Mar 26, 2020

I have an API that needs to differentiate between properties that are T | null and T | undefined. It looks like using nullable works for basic types but is ignored for $ref:

schemas:
  TestObject:
    type: object
    properties:
      stringMaybeUndefined:
        type: string
      stringMaybeNullA:
        type: string
        nullable: true
      stringMaybeNullB:
        anyOf:
        - type: string
        nullable: true
      otherObjectMaybeUndefined:
        $ref: "#/components/schemas/OtherObject"
      otherObjectMaybeNullA:
        $ref: "#/components/schemas/OtherObject"
        nullable: true
      otherObjectMaybeNullB:
        anyOf:
        - $ref: "#/components/schemas/OtherObject"
        nullable: true
    required:
    - stringMaybeNullA
    - stringMaybeNullB
    - otherObjectMaybeNullA
    - otherObjectMaybeNullB

  OtherObject:
    type: object

Generates:

export interface TestObject {
  stringMaybeUndefined?: string;
  stringMaybeNullA?: string | null;
  stringMaybeNullB?: string;
  otherObjectMaybeUndefined?: OtherObject;
  otherObjectMaybeNullA?: OtherObject;
  otherObjectMaybeNullB?: OtherObject;
}

Note: The OpenAPI 3.1 syntax

oneOf:
- $ref: "#/components/schemas/OtherObject"
- type: 'null'

works, but I'm forced to use OpenAPI 3.0 because of constraints from other tooling.

Can this be achieved somehow?

Edit, relevant resources:

If I interpret these (long) discussions correctly, at least the allOf wrapping should work?

@js2me
Copy link
Member

js2me commented Mar 26, 2020

I know about this problem
nullable works not very well, I'll fix it today, probably I need 1 hour to fix that

With my local changes tool currently generates this TestObject
image

@bluenote10
Copy link
Author

Looks good, thanks for the super fast maintenance!

@js2me
Copy link
Member

js2me commented Mar 26, 2020

@bluenote10 currently I have problem with parsing this case

        otherObjectMaybeNullD:
          anyOf:
          - $ref: "#/components/schemas/OtherObject"
          - type: 'null'

I guess it should be like

otherObjectMaybeNullD: OtherObject | null;

@bluenote10
Copy link
Author

Yes combining null in anyOf is a bit awkward, because in this case there cannot be multiple matches. I agree that handling it identical to oneOf makes the most sense.

@js2me js2me mentioned this issue Mar 26, 2020
@js2me
Copy link
Member

js2me commented Mar 26, 2020

Should be fixed in release 1.6.3
If someone still have same problems, please reopen issue and attach part of swagger schema

@Akimyou
Copy link

Akimyou commented Jun 22, 2022

I think I covered most cases with usage nullable option. Schema: https://github.com/acacode/swagger-typescript-api/blob/796d1cc7f8fc9a554e30a06b0d6512fd72444b3d/tests/schemas/v3.0/nullable-refs.yaml Generated ouput: https://github.com/acacode/swagger-typescript-api/blob/796d1cc7f8fc9a554e30a06b0d6512fd72444b3d/tests/generated/v3.0/nullable-refs.ts

can you handle this part?

                      "oneOf": [
                        {
                          "$ref": "#/definitions/348",
                          "x-apifox-overrides": {}
                        },
                        {
                          "type": "null"
                        }
                      ],

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

No branches or pull requests

3 participants