ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Issue Generating JSON Schema for Types with Indexed Access References

Open hash004 opened this issue 2 years ago • 0 comments

Description

When attempting to generate a JSON schema for a TypeScript type that uses indexed access references from another file, ts-json-schema-generator does not produce the expected output. Instead, it returns a schema with an empty not object.

Reproducible Example

intermediary.ts

import { paths, components } from "./api";

export type MetadateDto = components["schemas"]["MetadateDto"];

api.d.ts

export interface components {
  schemas: {
    MetadateDto: {
      /** Format: uuid */
      id?: string;
      name?: string | null;
    };
  }
}

Command Used:

npx ts-json-schema-generator --path './types/intermediary.ts' --type 'MetadateDto'

Expected Output:

A valid JSON schema representing the MetadateDto type.

{
  "$ref": "#/definitions/MetadateDto",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "MetadateDto": {
      "additionalProperties": false,
      "properties": {
        "id": {
          "description": "Format: uuid",
          "type": "string"
        },
        "name": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "type": "object"
    }
  }
}

Actual Output:

{
  "$ref": "#/definitions/MetadateDto",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "MetadateDto": {
      "not": {}
    }
  }
}

Workaround

Flattening the type references (i.e., defining the type directly in intermediary.ts without referencing api.d.ts) does produce the expected schema. However, this is not a sustainable solution for larger projects with interrelated types.

hash004 avatar Sep 06 '23 15:09 hash004