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

Bug | null dropping alias annotations

Open EelcoHoogendoorn opened this issue 6 years ago • 9 comments

/**
 * @minimum 1
 * @TJS-type integer
 */
type natural = number;

interface Schema {
    id: natural | null;
}

This results in json for a plain unannotated number being generated. With @nullable, it does work correctly.

EelcoHoogendoorn avatar Jun 06 '19 11:06 EelcoHoogendoorn

I've seen this without the nullable aspect. It seems to have recently broken. Possibly due to a TypeScript update.

ForbesLindesay avatar Jun 12 '19 08:06 ForbesLindesay

Hey guys, I have encountered the same issue :

Initial type to convert

interface MissingNull {
  prop: string | null
}

Output json schema

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "prop": {
            "type": "string"
        }
    },
    "type": "object"
}

Expected behavior

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "prop": {
            "type": ["string", "null"]
        }
    },
    "type": "object"
}

Is there any investigation on this issue?

Thanks!

cc @ForbesLindesay

bduron avatar Jul 18 '19 18:07 bduron

Actually, when using the --strictNullChecks option the correct json schema is outputed

bduron avatar Jul 18 '19 18:07 bduron

As you discovered, this is expected behavior given the Typescript semantics.

domoritz avatar Jul 19 '19 10:07 domoritz

same for me,--strictNullChecks not work

lvyangxu avatar Dec 17 '20 08:12 lvyangxu

Please describe the minimal program you encounter the issue with.

domoritz avatar Dec 17 '20 08:12 domoritz

/**
 * @minimum 1
 * @TJS-type integer
 */
type natural = number;

interface Schema {
  n1: natural | null;
  n2: natural;
}

Becomes:

    "Schema": {
      "type": "object",
      "properties": {
        "n1": {
          "type": [
            "null",
            "number"
          ]
        },
        "n2": {
          "minimum": 1,
          "type": "integer"
        }
      },
      "required": [
        "n1",
        "n2"
      ]
    },

While I would expect:

    "Schema": {
      "type": "object",
      "properties": {
        "n1": {
          "type": {
            "anyOf": [
              {
                type: "null" 
              },
              {
                "minimum": 1,
                "type": "integer"
              }
          ]
        },
        "n2": {
          "minimum": 1,
          "type": "integer"
        }
      },
      "required": [
        "n1",
        "n2"
      ]
    },

marneborn avatar Jun 08 '21 06:06 marneborn

Any updates?

jameslinimk avatar Aug 18 '22 02:08 jameslinimk