digitalbazaar / pyld

JSON-LD processor written in Python
https://json-ld.org/
Other
601 stars 128 forks source link

AttributeError on @id with a colon but less than two slashes #91

Open progval opened 5 years ago

progval commented 5 years ago

Hi,

I noticed the following error in PyLD 1.0.3:

$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyld import jsonld
>>> jsonld.compact({'http://schema.org/codeRepository': {'@id': 'http:'}}, {})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 145, in compact
    return JsonLdProcessor().compact(input_, ctx, options)
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 693, in compact
    compacted = self._compact(active_ctx, None, expanded, options)
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 1694, in _compact
    e = self._compact(active_ctx, active_property, e, options)
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 1895, in _compact
    inner_ if (is_list or is_graph) else expanded_item, options)
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 1726, in _compact
    active_ctx, active_property, element)
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 4271, in _compact_value
    active_ctx, value['@id'], vocab=(type_ == '@vocab'))
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 4190, in _compact_iri
    return remove_base(active_ctx['@base'], iri)
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 506, in remove_base
    rel = parse_url(iri)
  File "/home/dev/.local/lib/python3.5/site-packages/pyld/jsonld.py", line 594, in parse_url
    (g[0] == 'http' and g[1].endswith(':80'))):
AttributeError: 'NoneType' object has no attribute 'endswith'

Is it expected for PyLD to raise an exception on this kind of (possibly invalid) input?

davidlehn commented 5 years ago

No, that input shouldn't explode like that. I think the output should just be the same as the input in that case. That's what the js and ruby code do. Hopefully those are both correct.

Tight on time now, so if someone wants to make a patch, that'd be great. Should also add a test for this to https://github.com/w3c/json-ld-api. (Although pyld hasn't yet been updated to use the new WG test suites.)