kennknowles / python-jsonpath-rw

A robust and significantly extended implementation of JSONPath for Python, with a clear AST for metaprogramming.
Apache License 2.0
599 stars 193 forks source link

Unhandled None in `jsonpath.Slice` #82

Open jacobbogdanov opened 4 years ago

jacobbogdanov commented 4 years ago

When specifying a json path that contains a [*], if the value is None then a TypeError is raises.

Expected behavior

No exception is raised, and an empty result set is returned.

Minimal reproduction of the issue

>>> import jsonpath_rw
>>> d = {"key": None}
>>> p = jsonpath_rw.parse('$.key.[*].value')
>>> p.find(d)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    p.find(d)
  File "lib/python3.6/site-packages/jsonpath_rw/jsonpath.py",
 line 226, in find
    for subdata in self.left.find(datum)
  File "lib/python3.6/site-packages/jsonpath_rw/jsonpath.py",
 line 226, in find
    for subdata in self.left.find(datum)
  File "lib/python3.6/site-packages/jsonpath_rw/jsonpath.py",
 line 228, in <listcomp>
    for submatch in self.right.find(subdata)]
  File "lib/python3.6/site-packages/jsonpath_rw/jsonpath.py",
 line 494, in find
    return [DatumInContext(datum.value[i], path=Index(i), context=datum) for i in xrange(0, len(datum.value)
)]
TypeError: object of type 'NoneType' has no len()

Version

tested against python3.6 and jsonpath_rw==1.4.0

edit: this seems to be fixed by #77, but I can't find the corresponding issue

paalbra commented 3 years ago

I'm experiencing the same issue. Pip installed from github/master. Python 3.9.1.

Possibly second problem (that might be related/close)?:

>>> jsonpath_rw.parse("list[*]").find({"list": None})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/root/.local/lib/python3.9/site-packages/jsonpath_rw/jsonpath.py", line 229, in find
    return [submatch
  File "/root/.local/lib/python3.9/site-packages/jsonpath_rw/jsonpath.py", line 232, in <listcomp>
    for submatch in self.right.find(subdata)]
  File "/root/.local/lib/python3.9/site-packages/jsonpath_rw/jsonpath.py", line 546, in find
    return [DatumInContext(datum.value[i], path=Index(i), context=datum) for i in xrange(0, len(datum.value))]
TypeError: object of type 'NoneType' has no len()
>>> jsonpath_rw.parse("list[*]").find({"list": 1})
[DatumInContext(value=1, path=<jsonpath_rw.jsonpath.Index object at 0x7f1cb1d6a3d0>, context=DatumInContext(value=[1], path=Fields('list'), context=DatumInContext(value={'list': 1}, path=This(), context=None)))]
>>> jsonpath_rw.parse("list[*]").find({"list": "foo"})
[DatumInContext(value='foo', path=<jsonpath_rw.jsonpath.Index object at 0x7f17f674d1f0>, context=DatumInContext(value=['foo'], path=Fields('list'), context=DatumInContext(value={'list': 'foo'}, path=This(), context=None)))]

The array-syntax shouldn't match an int or string (nor other types)?

This issue (and the referenced PR) is fairly old though. Is this library alive and well?

paalbra commented 3 years ago

This issue (and the referenced PR) is fairly old though. Is this library alive and well?

Discussion regarding the health of this library #73