ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Crash on a union type in an intersection type

Open tuner opened this issue 3 years ago • 1 comments

This is similar to #1038 but with slightly more complex types.

export interface Container {
	child: Union;
}

export interface Dummy {
    x: number;
}

export interface Silly {
    y: number;
}

export type Union = Container | Silly;

export type Intersection = Union & Dummy;

The produced schema (with removeUnreachable() disabled) produces a schema with a missing Union definition:

{
  "$ref": "#/definitions/Intersection",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Container": {
      "additionalProperties": false,
      "properties": {
        "child": {
          "$ref": "#/definitions/Union"
        }
      },
      "required": [
        "child"
      ],
      "type": "object"
    },
    "Dummy": {
      "additionalProperties": false,
      "properties": {
        "x": {
          "type": "number"
        }
      },
      "required": [
        "x"
      ],
      "type": "object"
    },
    "Intersection": {
      "anyOf": [
        {
          "additionalProperties": false,
          "properties": {
            "x": {
              "type": "number"
            },
            "y": {
              "type": "number"
            }
          },
          "required": [
            "x",
            "y"
          ],
          "type": "object"
        },
        {
          "additionalProperties": false,
          "properties": {
            "child": {
              "$ref": "#/definitions/Union"
            },
            "x": {
              "type": "number"
            }
          },
          "required": [
            "child",
            "x"
          ],
          "type": "object"
        }
      ]
    },
    "Silly": {
      "additionalProperties": false,
      "properties": {
        "y": {
          "type": "number"
        }
      },
      "required": [
        "y"
      ],
      "type": "object"
    }
  }
}

I couldn't find anything obvious for this, but I guess the problem is again on the formatter side. The type hierarchy in the rootTypes variable has the Union type buried there somewhere.

I'll continue with this at a later time. Have to focus on other things for a while...

Btw, @domoritz, it could be useful to have a command-line flag for disabling the removal of unreachable types. Also, removeUnreachable() could throw a descriptive error when it encounters a reference to a missing definition. I can make a PR if these are ok.

tuner avatar Dec 01 '21 08:12 tuner

I think an additional warning is good. I'd prefer not to add a flag since it increases the api surface and it's easy enough to comment out code when debugging.

domoritz avatar Dec 01 '21 14:12 domoritz