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

Schema generator fails when nested property name is StringLiteral (kind 11) #2017

Closed
baloghbence0915 opened this issue Jul 9, 2024 · 4 comments · Fixed by #2018
Closed
Assignees
Labels
bug good first issue Good for newcomers

Comments

@baloghbence0915
Copy link
Contributor

baloghbence0915 commented Jul 9, 2024

Hi folks,

I run into the error below, when I tried to generate schema for the given class.

Env:

node: v18.20.3
ts-json-schema-generator: 2.3.0

Error:

TypeError: Cannot read properties of undefined (reading 'length')
    at strip (C:\...\node_modules\ts-json-schema-generator\dist\src\Utils\String.js:6:26)
    at ObjectProperty.getName (C:\...\node_modules\ts-json-schema-generator\dist\src\Type\ObjectType.js:16:38)
    at C:\...\ts-json-schema-generator\dist\src\TypeFormatter\ObjectTypeFormatter.js:57:41
    at Array.map (<anonymous>)
    at ObjectTypeFormatter.getObjectDefinition (C:\...\node_modules\ts-json-schema-generator\dist\src\TypeFormatter\ObjectTypeFormatter.js:57:14)
    at ObjectTypeFormatter.getDefinition (C:\...\node_modules\ts-json-schema-generator\dist\src\TypeFormatter\ObjectTypeFormatter.js:27:25)
    at ChainTypeFormatter.getDefinition (C:\...\node_modules\ts-json-schema-generator\dist\src\ChainTypeFormatter.js:18:44)        
    at CircularReferenceTypeFormatter.getDefinition (C:\...\node_modules\ts-json-schema-generator\dist\src\CircularReferenceTypeFormatter.js:21:59)
    at C:\...\node_modules\ts-json-schema-generator\dist\src\TypeFormatter\ObjectTypeFormatter.js:59:66
    at Array.reduce (<anonymous>)

Reproduce:

// test-class.ts
export class TestClass {
    CHILD = {
        PROP_1: '',
        "PROP_2": '',    // This is OK
        "PROP.3": '',    // This causes ERROR
        "{PROP_4}": '',    // This causes ERROR
        "400": ''       // This causes ERROR
    };
    PROP_5 = '';
    "PROP_6" = '';    // This is OK
    "PROP.7" = '';    // This is OK
    "{PROP_8}" = '';    // This is OK
    "400" = '';    // This is OK
}

Run:

./node_modules/.bin/ts-json-schema-generator --path test-class.ts --type TestClass

Probably this method is missing the handling of such a usecase:
https://github.com/vega/ts-json-schema-generator/blob/v2.3.0/src/NodeParser/TypeLiteralNodeParser.ts#L82

NodeObject has no escapedText prop:
image

Thanks in advance for checking!

@baloghbence0915 baloghbence0915 changed the title Schema generator fails when nested property is StringLiteral (kind 11) Schema generator fails when nested property name is StringLiteral (kind 11) Jul 9, 2024
@arthurfiorette
Copy link
Collaborator

Hey @baloghbence0915 thanks in advance for this super detailed pull request! Would you want to start a PR? I could help you with it. Remember to add unit tests :)

@baloghbence0915
Copy link
Contributor Author

Hi @arthurfiorette,
Yeah sure, I can give it a try. I'd appreciate any kind of help/guidance, thanks!

@arthurfiorette arthurfiorette self-assigned this Jul 9, 2024
@arthurfiorette arthurfiorette added bug good first issue Good for newcomers labels Jul 9, 2024
@arthurfiorette
Copy link
Collaborator

You can create a small unit test like this one:

https://github.com/vega/ts-json-schema-generator/tree/next/test/valid-data/annotation-comment

and add it into

https://github.com/vega/ts-json-schema-generator/blob/next/test/valid-data-struct.test.ts

Then using it.only instead of it and test it with npx jest test/valid-data-struct.test.ts.

Plase open a draft PR so I can see how's it going.

@baloghbence0915
Copy link
Contributor Author

@arthurfiorette , thanks for the help!
Draft PR is opened: #2018
Please check!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants