Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue Generating JSON Schema for Types with Indexed Access References #1768

Open
hash004 opened this issue Sep 6, 2023 · 0 comments
Open

Comments

@hash004
Copy link

hash004 commented Sep 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant