Skip to content

Commit b7e4159

Browse files
committed
fix: pointers without slashes, closes jg-rp#27
1 parent be219c3 commit b7e4159

5 files changed

Lines changed: 36 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Version 0.9.0 (unreleased)
44

5+
**Fixes**
6+
7+
- Fixed a bug with the parsing of JSON Pointers. When given an arbitrary string without slashes, `JSONPointer` would resolve to the document root. The empty string is the only valid pointer that should resolve to the document root. See [#27](https://github.com/jg-rp/python-jsonpath/issues/27).
8+
59
**Features**
610

711
- Added a command line interface, exposing JSONPath, JSON Pointer and JSON Patch features.

‎jsonpath/cli.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from jsonpath.exceptions import JSONPathIndexError
1010
from jsonpath.exceptions import JSONPathSyntaxError
1111
from jsonpath.exceptions import JSONPathTypeError
12-
from jsonpath.exceptions import JSONPointerResolutionError
12+
from jsonpath.exceptions import JSONPointerError
1313

1414
INDENT = 2
1515

@@ -295,7 +295,7 @@ def handle_pointer_command(args: argparse.Namespace) -> None:
295295
raise
296296
sys.stderr.write(f"target document json decode error: {err}\n")
297297
sys.exit(1)
298-
except JSONPointerResolutionError as err:
298+
except JSONPointerError as err:
299299
if args.debug:
300300
raise
301301
sys.stderr.write(str(err) + "\n")

‎jsonpath/patch.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,12 @@ def _op_pointer(
382382
f"({op}:{i})"
383383
)
384384

385-
return JSONPointer(
386-
pointer, unicode_escape=self.unicode_escape, uri_decode=self.uri_decode
387-
)
385+
try:
386+
return JSONPointer(
387+
pointer, unicode_escape=self.unicode_escape, uri_decode=self.uri_decode
388+
)
389+
except JSONPointerError as err:
390+
raise JSONPatchError(f"{err} ({op}:{i})") from err
388391

389392
def _op_value(
390393
self, operation: Mapping[str, object], key: str, op: str, i: int

‎jsonpath/pointer.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Union
1616
from urllib.parse import unquote
1717

18+
from .exceptions import JSONPointerError
1819
from .exceptions import JSONPointerIndexError
1920
from .exceptions import JSONPointerKeyError
2021
from .exceptions import JSONPointerResolutionError
@@ -93,8 +94,12 @@ def _parse(
9394
.decode("utf-16")
9495
)
9596

96-
# TODO: lstrip pointer
97-
# TODO: handle pointer without leading slash and not empty string
97+
s = s.lstrip()
98+
if s and not s.startswith("/"):
99+
raise JSONPointerError(
100+
"pointer must start with a slash or be the empty string"
101+
)
102+
98103
return tuple(
99104
self._index(p.replace("~1", "/").replace("~0", "~")) for p in s.split("/")
100105
)[1:]

‎tests/test_json_pointer.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import jsonpath
99
from jsonpath import JSONPointer
10+
from jsonpath import JSONPointerError
1011
from jsonpath import JSONPointerIndexError
1112
from jsonpath import JSONPointerResolutionError
1213
from jsonpath import JSONPointerTypeError
@@ -117,9 +118,9 @@ def test_resolve_with_missing_parent() -> None:
117118

118119
def test_resolve_with_missing_target() -> None:
119120
data = {"some": {"thing": [1, 2, 3]}}
120-
pointer = JSONPointer("some/other")
121+
pointer = JSONPointer("/some/other")
121122
parent, rv = pointer.resolve_parent(data)
122-
assert parent == data
123+
assert parent == data["some"]
123124
assert rv == UNDEFINED
124125

125126

@@ -204,3 +205,17 @@ def test_index_with_leading_zero() -> None:
204205
pointer = JSONPointer("/some/thing/01")
205206
with pytest.raises(JSONPointerTypeError):
206207
pointer.resolve_parent(data)
208+
209+
210+
def test_pointer_without_leading_slash() -> None:
211+
with pytest.raises(JSONPointerError):
212+
JSONPointer("some/thing/01")
213+
214+
with pytest.raises(JSONPointerError):
215+
JSONPointer("nosuchthing")
216+
217+
218+
def test_pointer_with_leading_whitespace() -> None:
219+
data = {"some": {"thing": [1, 2, 3]}}
220+
pointer = JSONPointer(" /some/thing/0")
221+
assert pointer.resolve(data) == 1

0 commit comments

Comments
 (0)