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

The @discriminator tag does not handle `ref` type discriminators

Open PLkolek opened this issue 2 years ago • 0 comments

Very similar (hence I copied the description and just substituted inputs and outputs) issue to https://github.com/vega/ts-json-schema-generator/issues/1682, but now caused by using an exported type as a discriminator.

For example,

export enum AEnum {
    a = "a",
    A = "A"
}

type A = { kind: AEnum, a: string }
type B = { kind: "b" | "B", b: string }

/**
 * @discriminator kind
 */
export type AB = A | B;

results in the following schema for the type AB:

Schema result
{
  "$id": "api.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "AB": {
      "allOf": [
        {
          "if": {
            "properties": {
              "kind": {
                "$ref": "#/definitions/AEnum"
              }
            }
          },
          "then": {
            "additionalProperties": false,
            "properties": {
              "a": {
                "type": "string"
              },
              "kind": {
                "$ref": "#/definitions/AEnum"
              }
            },
            "required": [
              "kind",
              "a"
            ],
            "type": "object"
          }
        },
        {
          "if": {
            "properties": {
              "kind": {
                "enum": [
                  "b",
                  "B"
                ],
                "type": "string"
              }
            }
          },
          "then": {
            "additionalProperties": false,
            "properties": {
              "b": {
                "type": "string"
              },
              "kind": {
                "enum": [
                  "b",
                  "B"
                ],
                "type": "string"
              }
            },
            "required": [
              "kind",
              "b"
            ],
            "type": "object"
          }
        }
      ],
      "properties": {
        "kind": {
          "enum": [
            "b",
            "B"
          ]
        }
      },
      "required": [
        "kind"
      ],
      "type": "object"
    },
    "AEnum": {
      "enum": [
        "a",
        "A"
      ],
      "type": "string"
    }
  }
}

Where the set of accepted values for the outermost kind enum is does not contain the values originating from AEnum, whereas it should list all possible options.

PLkolek avatar Jul 09 '23 09:07 PLkolek