From 7c3bb0d8cb0d18b98fc8c5138f5067cc41c54a8f Mon Sep 17 00:00:00 2001 From: LQBing Date: Sat, 21 Jun 2025 20:49:42 +0800 Subject: [PATCH 1/2] fix addap op as addne issue --- jsonpath/patch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonpath/patch.py b/jsonpath/patch.py index ce5c054..054d6e7 100644 --- a/jsonpath/patch.py +++ b/jsonpath/patch.py @@ -419,7 +419,7 @@ def _build(self, patch: Iterable[Mapping[str, object]]) -> None: value=self._op_value(operation, "value", "addne", i), ) elif op == "addap": - self.addne( + self.addap( path=self._op_pointer(operation, "path", "addap", i), value=self._op_value(operation, "value", "addap", i), ) From 9b83acb53b079434a4e0ffc621bc7dd17d3b2f2b Mon Sep 17 00:00:00 2001 From: James Prior Date: Sun, 22 Jun 2025 08:13:06 +0100 Subject: [PATCH 2/2] Add a test for JSON Patch op `addap` --- jsonpath/patch.py | 2 +- tests/test_json_patch.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/jsonpath/patch.py b/jsonpath/patch.py index 054d6e7..913987c 100644 --- a/jsonpath/patch.py +++ b/jsonpath/patch.py @@ -124,7 +124,7 @@ class OpAddAp(OpAdd): """A non-standard add operation that appends to arrays/lists . This is like _OpAdd_, but assumes an index of "-" if the path can not - be resolved. + be resolved rather than raising a JSONPatchError. **New in version 1.2.0** """ diff --git a/tests/test_json_patch.py b/tests/test_json_patch.py index eec2db7..64fafaf 100644 --- a/tests/test_json_patch.py +++ b/tests/test_json_patch.py @@ -262,3 +262,9 @@ def test_asdict() -> None: patch = JSONPatch(patch_doc) assert patch.asdicts() == patch_doc + + +def test_non_standard_addap_op() -> None: + # Index 7 is out of range and would raises a JSONPatchError with the `add` op. + patch = JSONPatch().addap(path="/foo/7", value=99) + assert patch.apply({"foo": [1, 2, 3]}) == {"foo": [1, 2, 3, 99]}