bcherny/json-schema-to-typescript

Nullable $ref

Open

#410 opened on Sep 20, 2021

View on GitHub
 (2 comments) (0 reactions) (0 assignees)TypeScript (449 forks)github user discovery
bughelp wanted

Repository metrics

Stars
 (3,302 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

If I specify a property as "either a schema reference or null", the null type is ignore. For example, the following schemas:

components:
  schemas:
    Foo:
      type: object
      properties:
        bar:
          $ref: "#/components/schemas/Bar"
          nullable: true
      required:
        - bar
    Bar:
      type: object
      properties:
        baz:
          type: boolean

Generates the following models:

export interface Foo {
  bar: {
    baz?: boolean;
    [k: string]: unknown;
  };
  [k: string]: unknown;
}

export interface Bar {
  baz?: boolean;
  [k: string]: unknown;
}

But what I expected is the following:

export interface Foo {
  bar: Bar | null;
  [k: string]: unknown;
}

export interface Bar {
  baz?: boolean;
  [k: string]: unknown;
}

Contributor guide