gazpachoking/jsonref

Cannot dereference top level '$ref'

Closed this issue · 2 comments

I'm trying to use jsonref to expand a JSON Schema which has a top level $ref but I'm getting a ValueError.

This is enough to to reproduce the issue:

schema = '{"definition": "helloworld", "$ref": "#/definition"}'
print(jsonref.loads(schema))

And this is the error:

Traceback (most recent call last):
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 252, in __subject__
    return self.cache
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 176, in __getattribute__
    return _oga(self, attr)
AttributeError: 'JsonRef' object has no attribute 'cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/jsonref.py", line 178, in callback
    base_doc = self.loader(uri)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/jsonref.py", line 299, in __call__
    result = self.get_remote_json(uri, **kwargs)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/jsonref.py", line 316, in get_remote_json
    result = json.loads(urlopen(uri).read().decode("utf-8"), **kwargs)
  File "/usr/lib/python3.8/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.8/urllib/request.py", line 509, in open
    req = Request(fullurl, data)
  File "/usr/lib/python3.8/urllib/request.py", line 328, in __init__
    self.full_url = url
  File "/usr/lib/python3.8/urllib/request.py", line 354, in full_url
    self._parse()
  File "/usr/lib/python3.8/urllib/request.py", line 383, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: ''

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

Traceback (most recent call last):
  File "/home/shishax/.config/JetBrains/PyCharm2022.1/scratches/jsonref-test.py", line 5, in <module>
    print(jsonref.loads(schema))
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 202, in proxied
    args.insert(arg_pos, self.__subject__)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 176, in __getattribute__
    return _oga(self, attr)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 134, in wrapper
    return method(self, *args, **kwargs)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 254, in __subject__
    self.cache = super(LazyProxy, self).__subject__
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 134, in wrapper
    return method(self, *args, **kwargs)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 240, in __subject__
    return self.callback()
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 134, in wrapper
    return method(self, *args, **kwargs)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/jsonref.py", line 180, in callback
    self._error("%s: %s" % (e.__class__.__name__, unicode(e)), cause=e)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/proxytypes.py", line 134, in wrapper
    return method(self, *args, **kwargs)
  File "/home/shishax/.env/sam-test-jsonref/lib/python3.8/site-packages/jsonref.py", line 223, in _error
    raise JsonRefError(
jsonref.JsonRefError: ValueError: unknown url type: ''

Is it allowed to have a top level $ref?
I didn't found any mention in the docs.

The spec says that other keys within the reference object shall be ignored. Your reference object contains the definitions key that the reference is trying to point to, so no, I don't think this is supported. That being said, I'll try to take a peek to see if the error can be made any better.

Yeah, at best this is a reference that refers to itself. I didn't figure out an easy way to improve the error message, but this is not supported by the jsonref spec.