bcherny / json-schema-to-typescript

Compile JSON Schema to TypeScript type declarations
https://bcherny.github.io/json-schema-to-typescript-browser/
MIT License
2.86k stars 380 forks source link

`unreachableDefinitions` not working with non plain object root node #439

Open CoalZombik opened 2 years ago

CoalZombik commented 2 years ago

If root node is not plain object type, unreachableDefinitions doesn't work and unreached definitions is not exported.

Calling: json2ts -i test.schema.json -o test.d.ts --unreachableDefinitions

Package version: 10.1.5 Node version: 17.3.1

Number test

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "number",
  "definitions": {
    "unreached": {
      "type": "boolean"
    }
  }
}

Received

export type TestSchema = number;

Expected

export type TestSchema = number;
export type Unreached = boolean;

Array test

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "array",
  "items": {
    "type": "number"
  },
  "definitions": {
    "unreached": {
      "type": "boolean"
    }
  }
}

Received

export type TestSchema = unknown[];

Expected

export type TestSchema = unknown[];
export type Unreached = boolean;

Logic subschemas test

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "oneOf": [
    {
      "type": "number"
    },
    {
      "type": "string"
    }
  ],
  "definitions": {
    "unreached": {
      "type": "boolean"
    }
  }
}

Received

export type TestSchema = number | string;

Expected

export type TestSchema = number | string;
export type Unreached = boolean;

Object (only currently working)

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "test": {
      "type": "number"
    }
  },
  "definitions": {
    "unreached": {
      "type": "boolean"
    }
  }
}

Received and expected

export type Unreached = boolean;

export interface TestSchema {
  test?: number;
  [k: string]: unknown;
}
LogansUA commented 1 year ago

Hey, any updates on this one? Because I'm also looking for the unreachableDefinitions feature, to generate reusable type. Basically, I'm having a similar problem. Here's the example:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "definitions": {
    "Foo": {
      "type": "object",
      "properties": {
        "baz": { "type": "string" }
      }
    },
    "Bar": {
      "type": "object",
      "properties": {
        "bum": { "type": "string" }
      }
    }
  },
  "oneOf": [
    { "$ref": "#/definitions/Bar" }
  ]
}

Received:

export interface Bar {
  bum?: string;
  [k: string]: unknown;
}

Expected:

export interface Bar {
  bum?: string;
  [k: string]: unknown;
}
export interface Foo {
  baz?: string;
  [k: string]: unknown;
}
albanm commented 2 months ago

I think this code fragment is incorrectly placed in the parseSchema function https://github.com/bcherny/json-schema-to-typescript/blob/master/src/parser.ts#L432

It would be better placed in the higher level "parse" function. Or maybe even in a new parseRoot function that would only be called on a top level schema and not recursively as parse is.