Skip to content

Handling of nullable for $ref in OpenAPI 3.0 #39

Closed
@bluenote10

Description

@bluenote10

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions