typescript-json-schema icon indicating copy to clipboard operation
typescript-json-schema copied to clipboard

Optional array / tuple elements

Open fanf2 opened this issue 6 years ago • 8 comments

I have defined a tuple type with an optional element like this:

type tuple23 = [ number, string, string | undefined ];

The translation I get is:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "additionalItems": {
        "anyOf": [
            {
                "type": "number"
            },
            {
                "type": "string"
            },
            {
                "type": "string"
            }
        ]
    },
    "items": [
        {
            "type": "number"
        },
        {
            "type": "string"
        },
        {
            "type": "string"
        }
    ],
    "minItems": 3,
    "type": "array"
}

The problem is that the undefined has been lost: minItems should be 2. What I expect is:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "items": [
        {
            "type": "number"
        },
        {
            "type": "string"
        },
        {
            "type": "string"
        }
    ],
    "minItems": 2,
    "maxItems": 3,
    "type": "array"
}

fanf2 avatar May 08 '18 13:05 fanf2

Same thing happens for me: When using type Array<SomeInterface | null>, typescript-json-schema ignores the null type in the "items" property.

auroranil avatar Jul 17 '18 01:07 auroranil

same issue for me

Dogyunjeong avatar Nov 30 '18 03:11 Dogyunjeong

+1 To this. I would like Array<string | null> to turn into "items": {"type": ["string", "null"] }

JaredProske avatar Jan 09 '19 23:01 JaredProske

Happy to review PRs for this bug.

domoritz avatar Jan 10 '19 12:01 domoritz

Same issue here. Undefined is missing. something?: number | undefined; in an array becomes

{"type":"object","properties":{"something":{"type":"number"}

and undefined just goes puff!

ali-habibzadeh avatar Jan 15 '21 18:01 ali-habibzadeh

@domoritz @ali-habibzadeh The Issue is still present now. The issue is of general nature:

For a property myProp: number | undefined; (in an interface/class/type alias) the undefined will be swollowed which is not to be expected.

In the https://github.com/vega/ts-json-schema-generator library which is inspired from this lib, this issue of disappearing undefined types doesn't seem to occur (see https://github.com/vega/ts-json-schema-generator/issues/623 - is this true @ali-habibzadeh ? You opened that issue - I couldn't confirm it because of another issue in that lib I'm facing (i.e. https://github.com/vega/ts-json-schema-generator/issues/1456 ))

When using the --required flag, myProp will not be in the required field, although it does not have a question mark (i.e. optional modifier). Using ? or | undefined are different in TypeScript and this library does not take that into account.

andrekovac avatar Jun 17 '23 12:06 andrekovac

@andrekovac I think it would be useful to contribute your stripped use case as a unit test so maintainers or contributors of this library, can easily patch it, or if you want to also contribute the patch.

If you follow that method a simple unit test with:

interface UnionTypeWithNull {
   myParam: string | null;
}

This test should not be blocked by the issue: https://github.com/vega/ts-json-schema-generator/issues/1456

Also the case for undefined does not seem relevant to me, myParam: string | undefined is in fact equal to myParam?: string which is managed by this lib to my knowledge by not adding the myParam to the required fields

The other case from the issue is about within an array which is different as the array item definition is handled differently.

loopingz avatar Jun 17 '23 14:06 loopingz

@loopingz There's a crucial difference with undefined. Facing an issue concerning this difference in my codebase lead me to find the issue here. In fact the case myParam: string | undefined is NOT equal to myParam?: string. I created this TS playground as an example.

So this difference is not covered by this library. I created a separate issue for it because it is different to this one: https://github.com/YousefED/typescript-json-schema/issues/551 There I also mentioned that it might be difficult to fix because undefined is not a valid value in JSON.

andrekovac avatar Jun 18 '23 13:06 andrekovac