gazpachoking/jsonref

Relative references pointers in multiple directories and files

Closed this issue · 1 comments

I have 3 json schemas in 2 directories, for example:

/working/A.json
/ressource/B.json
/ressource/C.json

As shown, B and C schemas are in the same directory.

A uses relative references from B (from another directory):

"$ref": "../ressource/B.json#/definitions/myItemFromB"

and B uses relative references from C (from the same directory):

"$ref": "C.json#/definitions/myItemFromC"

If I load A with a base_uri = /working/ and print it, I get a path error for B referencing C:

jsonref.JsonRefError: URLError: <urlopen error [Errno 2] No such file or directory: /working/C.json

meaning that in jsonref, does not "update" the base_uri when jumping to a different schema file when following references chain.
Indeed, I would expect jsonref to search in /ressource/C.json.

How can I get the behavior I expect there?

This seems to be working for me. Make sure your base_uri is an absolute path to whatever the base document you are loading is.

pathlib makes that easy:

file_a_path = Path("file-a.json").absolute()

with file_a_path.open() as file_a:
    result = jsonref.load(file_a, base_uri=file_a_path.as_uri())