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

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

Open
wizardpisces opened this issue Jul 31, 2023 · 2 comments

Comments

@wizardpisces
Copy link

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 wizardpisces changed the title Type override when compile same type name from different file Type override when compile same type name from different ts file to one json schema Jul 31, 2023
@domoritz
Copy link
Member

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?

@wizardpisces
Copy link
Author

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"]
}

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

2 participants