vega / ts-json-schema-generator

Generate JSON schema from your Typescript sources
MIT License
1.44k stars 190 forks source link

Incorrect schema generation when allow additional properties #1309

Open george-haroun opened 2 years ago

george-haroun commented 2 years ago

Hi there i get an empty objects once i allow additional properties..

Given the following set of types:

type TSelector = 'foo' | 'bar';

interface IFieldConditionScope {
    selector?: TSelector;
}

type IValueInCondition<TConditionSource = IFieldConditionScope> = TConditionSource & {
    type: 'valueIn';
};

type ICondition = Omit<IValueInCondition<IFieldConditionScope>, 'type'> & {
    type: 'fieldValueIn';
};

export interface IType {
    conditions?: ICondition;
}

Schema result

With allowing additionalProperties --> generate incorrect data in "valueSelector" node e.g. "ts-json-schema-generator -p file.ts -t IType --no-ref-encode --no-top-ref --out ./schema.json"

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "IFieldValueInCondition": {
      "properties": {
        "type": {
          "const": "fieldValueIn",
          "type": "string"
        },
        "valueSelector": {
          "anyOf": [
            {
              "properties": {},
              "type": "object"
            },
            {
              "properties": {},
              "type": "object"
            }
          ]
        }
      },
      "required": [
        "type"
      ],
      "type": "object"
    }
  },
  "properties": {
    "conditions": {
      "$ref": "#/definitions/IFieldValueInCondition"
    }
  },
  "type": "object"
}

with disallow additional properties --> correct schema e.g. "ts-json-schema-generator -p file.ts -t IType --additional-properties --no-ref-encode --no-top-ref --out ./schema.json"

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "additionalProperties": false,
  "definitions": {
    "IFieldValueInCondition": {
      "additionalProperties": false,
      "properties": {
        "type": {
          "const": "fieldValueIn",
          "type": "string"
        },
        "valueSelector": {
          "$ref": "#/definitions/TCorruptedValues"
        }
      },
      "required": [
        "type"
      ],
      "type": "object"
    },
    "TCorruptedValues": {
      "enum": [
        "foo",
        "bar"
      ],
      "type": "string"
    }
  },
  "properties": {
    "conditions": {
      "$ref": "#/definitions/IFieldValueInCondition"
    }
  },
  "type": "object"
}

please have a look and try to fix this issue because as i see the lib is very good and it will be nice to avoid such errors in generating schema

domoritz commented 2 years ago

Thank you for filing the issue. If you send a pull request with a fix, please add a test case with a minimal example that reproduces the issue. The example here looks a bit complex.

george-haroun commented 2 years ago

Hi @domoritz, thanks for your replay and sorry for my mistake in typing that i will try to fix it, i have corrected my last sentence

ReneWerner87 commented 2 years ago

Can anyone help with a fix pull request?

ReneWerner87 commented 2 years ago

any progress on this ?

domoritz commented 2 years ago

If there was, I think there would be a comment or a pull request here. Can you help with a pull request?

ReneWerner87 commented 2 years ago

don't think i have time as i maintain another github open source project(https://github.com/gofiber/fiber) myself

will see what i can do, thought someone from the community with more code background knowledge could take this on and develop a fix directly before i get involved