vega / ts-json-schema-generator

Generate JSON schema from your Typescript sources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

baloghbence0915 opened this issue · comments

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!

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 :)

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

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.

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