bcherny/json-schema-to-typescript

[bug] $ref resolve uses wrong root dir

Open

#324 opened on Jul 18, 2020

View on GitHub
 (13 comments) (14 reactions) (0 assignees)TypeScript (449 forks)github user discovery
buggood first issuehelp wanted

Repository metrics

Stars
 (3,302 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

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

Contributor guide