YousefED / typescript-json-schema

Generate json-schema from your Typescript sources
BSD 3-Clause "New" or "Revised" License
3.14k stars 322 forks source link

noExtraProps flag not working with recursive type #562

Open netanel-mce opened 1 year ago

netanel-mce commented 1 year ago

Steps to Reproduce: version: 0.59.0 create schema for Foo type, and turn on the flag noExtraProps.

interface ChildFoo {}

interface Foo {
    childFoo: Foo & ChildFoo;
}

The error: Maximum call stack size exceeded.

Because we merging the object, instead of use in allOf (see #107), in recursive type we trying to build schema for parent and child again and again.

netanel-mce commented 1 year ago

@domoritz what you want to got for following type, when flag noExtraProps turn on?

Type:

type Sub = {
   subChild: string;
};

type Main = {
   mainChild: Main & Sub;
}

if we generating a schema like this:

{
  "$ref": "#/definitions/Main",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Sub": {
      "additionalProperties": false,
      "type": "object",
      "properties": {
        "childSub": {
          "type": "number"
        }
      }
    },
    "Main": {
      "additionalProperties": false,
      "properties": {
        "childMain": {
          "allOf": [
            {
              "$ref": "#/definitions/Main"
            },
            {
              "$ref": "#/definitions/Sub"
            }
          ]
        }
      },
      "required": [
        "childMain"
      ],
      "type": "object"
    }
  }
}

so we will got the issue like #107 but we cannot merge the schemas, because it recursive type, so we must to use in $ref

I middle in write a PR for resolve this issue, but your should to answer of this product question

netanel-mce commented 1 year ago

@domoritz I tried this case also on vega but I got this schema

{
    "$ref": "#/definitions/Foo",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "Foo": {
            "additionalProperties": false,
            "properties": {
                "childFoo": {
                    "additionalProperties": false,
                    "properties": {},
                    "type": "object"
                }
            },
            "required": [
                "childFoo"
            ],
            "type": "object"
        }
    }
}

without recursive.

So please help me to decide what we want to be the output for this case.

netanel-mce commented 1 year ago

@erezMCE what you think?

netanel-mce commented 1 year ago

this type is endless, and no data to be valid for this type. so we not support it. image

thanks @erezMCE

Bewinxed commented 2 months ago

Facing this as well, any workarounds? I'm ok with having a max-depth option if all else fails? (Mainly prisma types are problematic)