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

Enhancement: support polymorphic arrays #1820

Open
dselman opened this issue Nov 23, 2023 · 0 comments
Open

Enhancement: support polymorphic arrays #1820

dselman opened this issue Nov 23, 2023 · 0 comments

Comments

@dselman
Copy link

dselman commented Nov 23, 2023

Command: npx ts-json-schema-generator --path '*.ts' --type Zoo

Input:

export interface Animal {
    name: string;
}

export interface Dog extends Animal {
    breed: string;
}

export interface Cat extends Animal {
    color: string;
}

export class Zoo {
    animals:Array<Animal>[];
}

Output:

{
  "$ref": "#/definitions/Zoo",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Animal": {
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ],
      "type": "object"
    },
    "Zoo": {
      "additionalProperties": false,
      "properties": {
        "animals": {
          "items": {
            "items": {
              "$ref": "#/definitions/Animal"
            },
            "type": "array"
          },
          "type": "array"
        }
      },
      "required": [
        "animals"
      ],
      "type": "object"
    }
  }
}

In this case I think it would be useful if the definition of items for the animals array was:

"items": {
            "anyOf": [
              {
                "$ref": "#/definitions/Cat"
              },
              {
                "$ref": "#/definitions/Dog"
              },
              {
                "$ref": "#/definitions/Animal"
              }
            ]
          }

And the Cat and Dog definitions were included in the schema.

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

1 participant