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

Type override when compile same type name from different ts file to one json schema

Open wizardpisces opened this issue 2 years ago • 2 comments

How to produce

`ts-json-schema-generator -f './tsconfig.json' -p './type/' -o './scheme.json'

./type/a.ts

export type TypeNameOne = {
    prop1: number
}

./type/b.ts

export type TypeNameOne = {
    prop2: string
}

What to expect?

program gives warning: Warning: file _./type/b.ts_ and _./type/a.ts_ contains the same name TypeNameOne with different schema will overrides each other

What is actually happening?

TypeNameOne from file b.ts overrides TypeNameOne from a.ts silently ./schema.json

{
  "$id": "api",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "api#/definitions/TypeNameOne",
  "definitions": {
    "TypeNameOne": {
      "type": "object",
      "properties": {
        "prop2": {
          "type": "string"
        }
      },
      "required": [
        "prop2"
      ],
      "additionalProperties": false
    }
  }
}

Reason: It lacks filename namespace to differentiate same name when we generate json from many ts files to just one

Some Suggestion:

  1. Is it possible to generate one type to one json file, a.ts-> a.json and b.ts->b.json
  2. Give warning for user to determine

wizardpisces avatar Jul 31 '23 10:07 wizardpisces

What's the tsconfig? I'm not sure yet this is actually a bug because type script would resolve what type is actually being used when you have another file that actually uses those two types. Does that make sense?

domoritz avatar Jul 31 '23 13:07 domoritz

It has nothing to do with how to use type, I also think it makes sense to define same name in different file

My doubts is that when ts-json-schema-generator silently override same name when compile directory to one schema.json just make no sense, it would lose info

By the way tsconfig

./tsconfig.json

{
    "compilerOptions": {
        "target": "esnext",
        "useDefineForClassFields": true,
        "module": "esnext",
        "moduleResolution": "node",
        "strict": true,
        "jsx": "preserve",
        "skipLibCheck": true,
        "sourceMap": true,
        "resolveJsonModule": true,
        "noImplicitAny": false,
        "isolatedModules": true,
        "esModuleInterop": true,
        "types": ["node"],
        "lib": ["esnext", "dom"],
        "baseUrl": "./",
        "paths": {
            "@/*": ["./src/*"]
        }
    },
    "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx"]
}

wizardpisces avatar Aug 01 '23 01:08 wizardpisces