YousefED / typescript-json-schema

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

Bug | null dropping alias annotations #297

Open EelcoHoogendoorn opened 5 years ago

EelcoHoogendoorn commented 5 years ago
/**
 * @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.

ForbesLindesay commented 5 years ago

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

bduron commented 5 years ago

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 commented 5 years ago

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

domoritz commented 5 years ago

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

lvyangxu commented 3 years ago

same for me,--strictNullChecks not work

domoritz commented 3 years ago

Please describe the minimal program you encounter the issue with.

marneborn commented 3 years ago
/**
 * @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"
      ]
    },
jameslinimk commented 2 years ago

Any updates?