bcherny/json-schema-to-typescript

[bug] $ref resolve uses wrong root dir

Open

#324 aperta il 18 lug 2020

Vedi su GitHub
 (13 commenti) (14 reazioni) (0 assegnatari)TypeScript (449 fork)github user discovery
buggood first issuehelp wanted

Metriche repository

Star
 (3302 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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 json2ts -i ./apidocs/response/**/*.json -o ts-apidocs we have a problem ResolverError: Error opening file ".../project/entity/Something-2.json" (notice how apidocs folder is missing)

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

During resolvement cli tool uses process.cwd for resolving https://github.com/bcherny/json-schema-to-typescript/blob/master/src/resolver.ts#L12

but according to docs mentioned at https://github.com/bcherny/json-schema-to-typescript/blob/master/src/index.ts#L24

https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html#rfc.section.4

If the URI contained in the JSON Reference value is a relative URI, then the base URI resolution MUST be calculated according to [RFC3986], section 5.2. Resolution is performed relative to the referring document.

so it should be relative to referring document (Something.json in my example) and not process.cwd

So our current workaround: we use docs dir as cwd, but it will break as soon as we'll have nested folder structure

Guida contributor