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

V2 creates empty definitions after updating from V1 #1941

Closed
SampsaKaskela opened this issue Apr 30, 2024 · 5 comments · Fixed by #1942
Closed

V2 creates empty definitions after updating from V1 #1941

SampsaKaskela opened this issue Apr 30, 2024 · 5 comments · Fixed by #1942

Comments

@SampsaKaskela
Copy link
Contributor

I'm trying to migrate from version 1.5.1 to version 2. Generating schema is working fine in 1.5.1. I'm running following file.

import { createGenerator } from 'ts-json-schema-generator';
import fs from 'fs';

const config = {
  path: `src/configs/tabs.ts`,
  tsconfig: 'tsconfig.json',
  type: '*',
  skipTypeCheck: true
};

const schema = createGenerator(config).createSchema(config.type);

const schemaString = JSON.stringify(schema, null, 2);
fs.writeFile(`src/generated/schema.json`, schemaString, (err) => {
  if (err) throw err;
});

tabs.ts contains following

export interface TabConfig {
  name: string;
  sequence: number;
}

export interface TabsConfigData {
  applicationBanner: string;
  tabs: TabConfig[];
}

export type TabsConfig = TabsConfigData;

But in version 2 the result is:

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

I didn't notice change in README programmatic example so I assume this part of the code should still work like in v1.

@domoritz
Copy link
Member

It should still work. The CLI works for me. yarn run run --path test.ts --type 'TabsConfig'. Can you try with what's in next?

@SampsaKaskela
Copy link
Contributor Author

I was able to get it working with version 2.1.1 by specifying the type in this case TabsConfig like in your CLI command. But using * generates empty definitions. Using next didn't change the outcome. But I really would need to use * because I want everything in one go.

@domoritz
Copy link
Member

domoritz commented May 1, 2024

Huh, that sounds like a bug then. I don't remember changing anything specific to *. Could you step through the code to see what might be wrong?

@SampsaKaskela
Copy link
Contributor Author

I did check the results and compared. In SchemaGenerator.ts getRootNodes function returns empty list in v2. In line 51

const rootFileNames = this.program.getRootFileNames();

V1 returns

[ 'C:/Users/samps/ts-json-schema-generator/test.ts' ]

While V2 returns

[ 'C:\\Users\\samps\\ts-json-schema-generator\\test.ts' ]

This then results to empty array returned from function because the in V2 this string path doesn't match any of the strings in rootSourceFiles.

I went a bit deeper and in factory/program.ts it seems that in line 63

const rootNamesFromPath = config.path ? glob.sync(normalize(path.resolve(config.path))) : [];

returns different result in V2 similar to differences described above. glob.sync seems to be the culprit since that functions receive same input in both versions but once that runs the result is different. It seems V1 uses glob v8 while V2 uses glob v10 so that is probably difference. I traced back and seems this change happened in 2.1.0. I tested 2.0.1 and that seems to work fine.

Is this enough to get you on track how this should be fixed?

@domoritz
Copy link
Member

domoritz commented May 1, 2024

Oh, I see you are on windows. I saw that windows changed but I don't have windows so I can't fix it.

It would be awesome if you could find a fix and send it since you can actually test it. Looks like you already identified the root cause.

I can reenable the ci for windows when the fix is in.

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

Successfully merging a pull request may close this issue.

2 participants