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

Regarding "A note on base_uri" #62

Open
mattany opened this issue Oct 11, 2023 · 0 comments
Open

Regarding "A note on base_uri" #62

mattany opened this issue Oct 11, 2023 · 0 comments

Comments

@mattany
Copy link

mattany commented Oct 11, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant