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

Set Default Working Directory to Schema Root #417

Closed
jhunterkohler opened this issue Oct 31, 2021 · 5 comments
Closed

Set Default Working Directory to Schema Root #417

jhunterkohler opened this issue Oct 31, 2021 · 5 comments

Comments

@jhunterkohler
Copy link

jhunterkohler commented Oct 31, 2021

Set Default Working Directory to Schema Root

References should be resolved by the directory of the schema itself, not the script/program caller. Currently, the cwd is resetting on every de-reference, thus making it virtually impossible to compile multi-file schemas without first de-referencing them with the $RefParser library. This ruins the ability to reuse definition interfaces, and bloats code. Either way, defaulting to schema root would conform with the RFC, trivially.

I don't think this should be too difficult to change because the $RefParser library does this by default.

Example of Issue (exposition):

Assume a working tree like the following.

schemas
├── bits
│   ├── review_partial.json
│   └── user_partial.json
├── helpers
│   ├── object_id.json
│   └── tokens_wrapper.json
├── review.json
└── user.json

And the file schemas/bits/review_partial.json being:

{
    "$schema": "http:https://json-schema.org/draft-07/schema",
    "type": "object"
    "properties": {
        "_id": { 
            "$ref": "../helpers/object_id.json"
        }
    }
}

And the file schemas/helpers/object_id.json being:

{
    "$schema": "http:https://json-schema.org/draft-07/schema",
    "type": "object",
    "properties": {
        "wrapper": {
            "$ref": "./tokens_wrapper.json",
        }
    }
}
  1. Invoking the compiler on schemas/bits/review_partial.json outside the directory (say, at the root), will not result in a schema-relative reference.
  2. If you change the cwd setting to schema/bits, this initial reference will resolve, but the reference in schemas/helpers/object_id.json will ALSO resolve with cwd of schemas/bits, which, it isn't in, and thus the reference fails to resolve to schemas/helpers/token_wrapper.json

$RefParser Library

Note that the parser will auto resolve based on the schema root if invoked on the file. Take the last example: by default, the following will bundle the schema. This is required before passing it to this library, which, as I said ruins the possibility of interface definition re-use, and makes it much more difficult to invoke, especially without experience with JSON references.

console.log(await $RefParser.bundle("./schemas/bits/review_partial.json"));
// logs bundled schema without error
@relief-melone
Copy link

I think this is the same issue as #324 no? If yes I created a pull Request #421 unfortunately there has no review been made so far but you could try if this solves your problems. It does solve ours when it comes to references ;)

@relief-melone
Copy link

since at least at the moment this repo seems to be abandonden and my pr has been lying around for 2 months i decided to publish my fork (will deprecate it as soon as the $ref issues get resolved in this repo). Let me know if this fixes your issue as well.
https://www.npmjs.com/package/rm-json-schema-to-typescript

@beuleal
Copy link

beuleal commented Jan 19, 2022

Hey @relief-melone - Do you know if the same works for CLI? I'm trying to json2ts -i myschema.json for my current schema that references an external schema:

"items": {
            "$ref": "file:https://another_schema.json#/definitions/myItem"
          }

This is not working. I'm receiving the following error:

code: 'ERESOLVER',
    message: 'Error opening file "/another_schema.json/" \n' +
      "ENOENT: no such file or directory, open '/another_schema.json/'",

@relief-melone
Copy link

relief-melone commented Feb 2, 2022

sorry for the late reply. i am using it in the cli and i think it sould work. Have you installed the original or my fork? If it's my fork feel free to create an issue. would be great if you provided a repo to reproduce the behaviour too

@bcherny
Copy link
Owner

bcherny commented May 22, 2022

Dup of #324.

Sorry for the delayed response. Feel free to update the PR per requested changes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants