vega / ts-json-schema-generator

Generate JSON schema from your Typescript sources
MIT License
1.47k stars 195 forks source link

(CLI only) ts-json-schema-generator --path src/**/*.ts only proceed one source file, rather than multiple source files #1472

Closed grimmer0125 closed 1 year ago

grimmer0125 commented 2 years ago

Thank you for supplying this great tool.

I notice that the CLI only proceed one file. I'd like to know if this is the expected behavior. If so, is there any other way to parse multiple source files at once and output the json schema file?

domoritz commented 2 years ago

Thanks for the report. Can you provide a minimal reproducible example that demonstrates the issue?

grimmer0125 commented 2 years ago

@domoritz,

I push the example into this repo, https://github.com/grimmer0125/nestj-ts-json-schema-generator-example.

  1. Use NestJS boilerplate
  2. Add two files that are supposed to get the json schemas. You can check this commit
    // src/dto/sum.dto.ts
    export class SumDto {
    numbers: number[];
    }
    // src/dto/subtract.dto.ts
    export class SubtractDto {
    numberArray: number[];
    }
  3. Add "gen-json-schema": "ts-json-schema-generator --path src/**/*.dto.ts --no-type-check --type \"*\" -o json.schema" in npm script

After invoking this script, there is only SubtractDto generated in the json schema file.

grimmer0125 commented 1 year ago

@domoritz , I made another minimized repo, https://github.com/grimmer0125/ts-json-schema-generator-example https://github.com/vega/ts-json-schema-generator that does not include NestJS and other npm packages. But it still gets only SubtractDto in json.schema.

I also noticed that this issue only happened in CLI interface. To use programmatic usage, there is no this issue.

domoritz commented 1 year ago

I just tried it locally and this works:

$ yarn --silent run run --path 'test.ts' --type '*'
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "SubtractDto": {
      "additionalProperties": false,
      "properties": {
        "numberArray": {
          "items": {
            "type": "number"
          },
          "type": "array"
        }
      },
      "required": [
        "numberArray"
      ],
      "type": "object"
    },
    "SumDto": {
      "additionalProperties": false,
      "properties": {
        "numbers": {
          "items": {
            "type": "number"
          },
          "type": "array"
        }
      },
      "required": [
        "numbers"
      ],
      "type": "object"
    }
  }
}

Let me know if you see different output when you run this without an npm script.

grimmer0125 commented 1 year ago

@domoritz I'm sorry that I posted the wrong repo URL in the prevoius comment. Here is my new repo to reproduce this issue, just use npm start to test. https://github.com/grimmer0125/ts-json-schema-generator-example

Let me know if you see different output when you run this without an npm script.

If I put my testing source files in the cloned repo, https://github.com/vega/ts-json-schema-generator, and execute yarn --silent run run --path 'src/dto/*.ts' --type '*' -o json.schema, and I got the correct result.

domoritz commented 1 year ago

So you're saying it works in this repo but not your test repo? Can you see what's different?

grimmer0125 commented 1 year ago

@domoritz ,

Thank you for the follow-up and the reminder about run this without an npm script.

I tried to do a comprehensive test (& a little tracing code). I noted the conclusions in the readme of this https://github.com/grimmer0125/ts-json-schema-generator-example. Please take a look.

The conclusion is the npm script "gen-json-schema": "ts-json-schema-generator --path src/*.dto.ts -o json.schema" will convert src/*.dto.ts as multiple files name arguments, but commander package can not handle this. After I try "gen-json-schema": "ts-json-schema-generator --path 'src/*.dto.ts' -o json.schema" and it works.

Thank you again for this great library.

github-actions[bot] commented 1 year ago

:rocket: Issue was released in v1.2.0-next.4 :rocket:

github-actions[bot] commented 1 year ago

:rocket: Issue was released in v1.2.0-next.4 :rocket: