Skip to content

Commit fc239b9

Browse files
authored
Merge pull request #22 from jg-rp/root-pointer
fix: root pointer from parts
2 parents 3608ee4 + 670ab2b commit fc239b9

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Python JSONPath Change Log
22

3+
## Version 0.8.1
4+
5+
**Fixes**
6+
7+
- Fixed the string representation of a `JSONPointer` when built using `JSONPointer.from_parts()` and pointing to the document root. See [#21](https://github.com/jg-rp/python-jsonpath/issues/21).
8+
39
## Version 0.8.0
410

511
**Breaking changes**

jsonpath/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: 2023-present James Prior <jamesgr.prior@gmail.com>
22
#
33
# SPDX-License-Identifier: MIT
4-
__version__ = "0.8.0"
4+
__version__ = "0.8.1"

jsonpath/pointer.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,16 @@ def from_match(
232232
) -> JSONPointer:
233233
"""Return a JSON Pointer for the path from a JSONPathMatch instance."""
234234
# A rfc6901 string representation of match.parts.
235-
return cls(
236-
"/"
237-
+ "/".join(
235+
if not match.parts:
236+
# This should not happen, unless the JSONPathMatch has been tampered with.
237+
pointer = ""
238+
else:
239+
pointer = "/" + "/".join(
238240
str(p).replace("~", "~0").replace("/", "~1") for p in match.parts
239-
),
241+
)
242+
243+
return cls(
244+
pointer,
240245
parts=match.parts,
241246
unicode_escape=False,
242247
uri_decode=False,
@@ -269,9 +274,18 @@ def from_parts(
269274
.decode("utf-16")
270275
for p in _parts
271276
)
277+
272278
__parts = tuple(_parts)
279+
280+
if __parts:
281+
pointer = "/" + "/".join(
282+
p.replace("~", "~0").replace("/", "~1") for p in __parts
283+
)
284+
else:
285+
pointer = ""
286+
273287
return cls(
274-
"/" + "/".join(p.replace("~", "~0").replace("/", "~1") for p in __parts),
288+
pointer,
275289
parts=__parts,
276290
unicode_escape=False,
277291
uri_decode=False,

tests/test_json_pointer.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ def test_pointer_from_parts() -> None:
170170
assert str(pointer) == "/some/thing/0"
171171

172172

173+
def test_pointer_from_empty_parts() -> None:
174+
parts: List[Union[str, int]] = []
175+
pointer = JSONPointer.from_parts(parts)
176+
assert str(pointer) == ""
177+
178+
179+
def test_pointer_from_only_empty_string_parts() -> None:
180+
parts: List[Union[str, int]] = [""]
181+
pointer = JSONPointer.from_parts(parts)
182+
assert str(pointer) == "/"
183+
184+
173185
def test_pointer_from_uri_encoded_parts() -> None:
174186
parts: List[Union[str, int]] = ["some%20thing", "else", 0]
175187
pointer = JSONPointer.from_parts(parts, uri_decode=True)

0 commit comments

Comments
 (0)