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

`unreachableDefinitions` not working with non plain object root node

Open CoalZombik opened this issue 3 years ago • 2 comments

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;
}

CoalZombik avatar Feb 17 '22 20:02 CoalZombik

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;
}

LogansUA avatar May 26 '23 09:05 LogansUA

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.

albanm avatar Apr 30 '24 19:04 albanm