json-schema-ref-parser icon indicating copy to clipboard operation
json-schema-ref-parser copied to clipboard

Relative references not working correctly.

Open OsaPL opened this issue 4 years ago • 4 comments

It'll be easiest to explain with example: I have a folder structure:

│   index.js
│
├───test1
│       test1.json
│       test11.json
│
└───test2
        test2.json

test1.json contains:

{
  "MyProp": "Test1",
  "$ref": "../test2/test2.json#/Prop",
  "Test11": [
    {
      "$ref": "../test1/test11.json#/MyProp"
    }
  ]
}

trying to deference test1.json fails, with an error that is not really true:

'Error: Error resolving $ref pointer "c:/refparserTest/test1/test11.json#/MyProp". \n' +
    '"c:/refparserTest/test1/test11.json" not found.\n' 

while you can see that /test1/test11.json actually exists, the path is correct.

I've tried few other ways: "$ref": "test11.json#/MyProp" will use a wrong directory. "$ref": "./../test1/test11.json#/MyProp" also wont work. "$ref": "./test1/test11.json#/MyProp" will report wrong path also: test2/test1/test11.json

OsaPL avatar Dec 15 '21 09:12 OsaPL

I am having a similar problem: When I try to reach a file in another folder using a relative path it just ends with a weird error. Assuming the root dir is called test for example referencing test2.json from your index.js results into: Error resolving $ref pointer "/test2/test2.json" while I clearly typed in "$ref": "./test2/test2.json" and then it should reach /test/test2/test2.json. When I try to reach it through absolute paths other weird things happen.

I use node in docker, maybe there is the issue?

RottenSchnitzel avatar Dec 20 '21 02:12 RottenSchnitzel

After some investigation with my coworker, it looks like "$ref": "../test2/test2.json#/Prop", is being dereferenced successfully, but after that the root path is not being changed back to test1.json and it tries to dereference "$ref": "../test1/test11.json#/MyProp" in test2.json scope, and fails cause "Test11" variable path doesnt exists there.

OsaPL avatar Apr 11 '22 11:04 OsaPL

@OsaPL I'm also facing this issue... Just wondering if there's a reason why you didn't create a PR for the fix in your fork? Thanks

mikeb1rd avatar Mar 19 '23 22:03 mikeb1rd

There was a PR but I had trouble adding tests (mainly to my lack of knowledge and time). https://github.com/APIDevTools/json-schema-ref-parser/pull/261

OsaPL avatar Mar 21 '23 09:03 OsaPL