bcherny / json-schema-to-typescript

Compile JSON Schema to TypeScript type declarations
https://bcherny.github.io/json-schema-to-typescript-browser/
MIT License
2.92k stars 390 forks source link

[bug] pass json-schema-to-typescript parser to refResolver as default .json resolver #325

Open Hulkmaster opened 4 years ago

Hulkmaster commented 4 years ago

So we have project with structure like

project/apidocs/entitity project/apidocs/response project/ts-apidocs/entitity project/ts-apidocs/response

if we launch cli tool like cd apidocs && json2ts -i ./response/**/*.json -o ../ts-apidocs

we have a file ./apidocs/response/Something.json which has reference "$ref": "../entity/Something-2.json"

and our something-2.json

has required properties, for example, id

if we would generate Something-2.json directly, then .ts files will have property as required but if type would be generates via $ref, then property will be marked as optional

so i guess direct converting and converting via $ref uses different rules, and $ref converting has a bug

Hulkmaster commented 4 years ago

i guess the reason is here https://github.com/bcherny/json-schema-to-typescript/blob/1ec105d1f00f23969bd057fe151ff952a7280e3d/src/index.ts#L64 https://github.com/APIDevTools/json-schema-ref-parser/blob/a4eec2ba56d793c410fb884a06f5eb9dfba4e412/lib/options.js#L32

so the solution will be to pass json parser to ref-parser that lib by default

also i'm not sure if its possible to pass it from cli currently

memark commented 4 years ago

We've run into this bug as well. It causes us some problems.

What are your thoughts on this @bcherny ?

bcherny commented 3 years ago

Hey, sorry for the delayed response. Not sure I totally understand the issue -- mind spelling it out a bit more, and providing a complete, whittled down repro case?

Hulkmaster commented 3 years ago

When lib is trying to resolve references, it uses different parser

I would suggest to use recursive (using same lib) approach for resolving references

Hulkmaster commented 3 years ago

accidentally closed