vega / ts-json-schema-generator

Generate JSON schema from your Typescript sources
MIT License
1.47k stars 195 forks source link

When I use discriminator, I am getting an error where it can't find the specific field #2010

Open davidli108 opened 5 months ago

davidli108 commented 5 months ago
type A1 = {
  id_type: 'a1';
  id: string;
};
type A2 = {
  id_type: 'a2';
  id: string;
};
type C = {
  name: string;
  address: string;
}

type A1c = A1 & C
type A2c = A2 & C

/**
 * @discriminator id_type
 */
export type A = A1c | A2c

When i generate the json schema from this typescript, the json is

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/A",
  "definitions": {
    "A": {
      "type": "object",
      "discriminator": {
        "propertyName": "id_type"
      },
      "required": [
        "id_type"
      ],
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "name": {
              "type": "string"
            },
            "address": {
              "type": "string"
            },
            "id_type": {
              "type": "string",
              "const": "a1"
            },
            "id": {
              "type": "string"
            }
          },
          "required": [
            "address",
            "id",
            "id_type",
            "name"
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "name": {
              "type": "string"
            },
            "address": {
              "type": "string"
            },
            "id_type": {
              "type": "string",
              "const": "a2"
            },
            "id": {
              "type": "string"
            }
          },
          "required": [
            "address",
            "id",
            "id_type",
            "name"
          ]
        }
      ]
    }
  }
}

In this case, I am getting an error and it says it cannot find the "name" or "address" field in A As you can see, A has "type": "object" inside of it and oneOf property also has "type": "object" in each property. I think one of these should be removed from A

Can you please check?

mdesousa commented 5 months ago

the issue seems to be that the children under oneOf should have the type property see this example