Skip to content

Regarding "A note on base_uri" #62

@mattany

Description

@mattany

I think the docs and error handling would benefit from explicitly writing that the base_uri parameter must be set to the directory of the file that has references in it. I tried to set base_uri to the directory of the file from which to inject references, and that caused the program to crash. Took me a couple of hours to isolate the issue. I'm not sure if this is expected behavior or not, but in any case it is not apparent from the docs or the error. You can see the error thrown in example 2 below.

Assuming we have 2 files:

Example 1

/project/folder/file-a.json:

{
  "bat": {
      "$ref": "file:../file-b.json#/cat"
  }
}

/project/file-b.json:

{
  "cat": "hat"
}

/project/main.py

from pathlib import Path
import jsonref

with open("./folder/file-a.json", "r") as f:
    config: dict = jsonref.load(f, base_uri=Path("./folder/file-a.json").absolute().as_uri())
    print(config)

output:

{'bat': 'hat'}

Example 2

/project/folder/file-a.json:

{
  "bat": {
      "$ref": "file:./file-b.json#/cat"
  }
}

/project/file-b.json:

unchanged

/project/main.py

from pathlib import Path
import jsonref

with open("./folder/file-a.json", "r") as f:
    config: dict = jsonref.load(f, base_uri=Path("./file-b.json").absolute().as_uri())
    print(config)

expected output:

{'bat': 'hat'}

actual:

Traceback (most recent call last):
  File "/Library/Python/3.9/site-packages/jsonref.py", line 179, in resolve_pointer
    document = document[part]
KeyError: 'cat'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/<username>/pythonProject2/main.py", line 6, in <module>
    print(config)
  File "/Library/Python/3.9/site-packages/proxytypes.py", line 121, in wrapper
    return method(self, *args, **kwargs)
  File "/Library/Python/3.9/site-packages/jsonref.py", line 199, in __repr__
    return repr(self.__subject__)
  File "/Library/Python/3.9/site-packages/proxytypes.py", line 163, in __getattribute__
    return _oga(self, attr)
  File "/Library/Python/3.9/site-packages/proxytypes.py", line 121, in wrapper
    return method(self, *args, **kwargs)
  File "/Library/Python/3.9/site-packages/proxytypes.py", line 243, in __subject__
    self.cache = super(LazyProxy, self).__subject__
  File "/Library/Python/3.9/site-packages/proxytypes.py", line 121, in wrapper
    return method(self, *args, **kwargs)
  File "/Library/Python/3.9/site-packages/proxytypes.py", line 227, in __subject__
    return self.callback()
  File "/Library/Python/3.9/site-packages/proxytypes.py", line 121, in wrapper
    return method(self, *args, **kwargs)
  File "/Library/Python/3.9/site-packages/jsonref.py", line 140, in callback
    result = self.resolve_pointer(base_doc, fragment)
  File "/Library/Python/3.9/site-packages/proxytypes.py", line 121, in wrapper
    return method(self, *args, **kwargs)
  File "/Library/Python/3.9/site-packages/jsonref.py", line 181, in resolve_pointer
    raise self._error(
jsonref.JsonRefError: Error while resolving `file:///Users/<username>/file-b.json#/cat`: Unresolvable JSON pointer: '/cat'

Process finished with exit code 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions