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

Nullable object fields don't get generated correctly #2058

Open
valzam opened this issue Feb 26, 2023 · 1 comment
Open

Nullable object fields don't get generated correctly #2058

valzam opened this issue Feb 26, 2023 · 1 comment

Comments

@valzam
Copy link

valzam commented Feb 26, 2023

Describe the bug
A TS type like this

interface A {
 foo: string;
}

interface B {
 bar: A | null
}

results in the following OpenAPI spec:

components:
 schema:
    B:
      type: object
      properties:
        bar:
          $ref: '#/components/schemas/A'
          nullable: true

This doesn't work as expected in OpenAPI 3.0 (the nullable is simply ignored)
OAI/OpenAPI-Specification#1368

Expected behavior
The field definition needs to use allOf

bar:
  nullable: true
  allOf:
  - $ref: '#/components/schemas/A'

https://stackoverflow.com/questions/40920441/how-to-specify-a-property-can-be-null-or-a-reference-with-swagger

Desktop (please complete the following information):
Spot version 1.9.0

@lfportal
Copy link
Contributor

lfportal commented Feb 27, 2023

I do remember this one. To add further context:

export interface ReferenceObject {
$ref: string;
/**
* WARNING
*
* `nullable: true` will occur when exactly one type reference is combined with null.
*
* Example:
*
* `MyType | null`
*
* https://swagger.io/docs/specification/using-ref/#considerations
*
* Schema references cannot contain sibling elements. `nullable` therefore should
* not be combined with schema reference objects. This rule was misunderstood during
* development on the OpenAPI 3 generator at Airtasker. This will be removed in a
* future version of Spot when Airtasker's tooling supports an alternative valid
* representation for the above scenario.
*
* TODO: Find a way to remove this
* A possible seemingly accepted workaround to this is to wrap the schema reference
* into an allOf.
*
* Example:
*
* ```
* nullable: true
* allOf:
* - $ref: #/components/schemas/MyType
* ```
*/
nullable?: boolean;
}

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

2 participants