Repository navigation
Serialize and compare patches whose paths are pointer objects - #272
Merged
Merged
Conversation
The 'path' and 'from' of an operation can be given as pointer objects instead of strings. A JsonPatch created from such operations could be applied, but to_string() raised a TypeError, as its operations still contained the pointer objects, and it was not equal to the same patch given the paths as strings. JsonPatch now replaces pointer objects by their paths in its operations, in copies, so the operations it was created from are unchanged. Patch operations compare and hash by the paths as well. Their setters keep updating the operation they were created from. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PewzpjVeke4LTQ7Q88nyu6
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Converting constructed pointers to strings causes their paths to be parsed again, changing custom-pointer targets and breaking valid add operations.
1 open issue
What changed in this PR
This PR enables serialization and comparison of JSON patches whose path and from values are pointer objects, while preserving caller-owned operation dictionaries.
Changes:
- Normalize pointer objects to path strings in
JsonPatchoperations. - Compare and hash standalone operations using normalized paths.
- Add seven tests covering serialization, equality, custom pointers, read-only mappings, setters, and input preservation.
| File | Description |
|---|---|
| tests.py | Adds coverage for patches and operations containing pointer objects. |
| jsonpatch.py | Adds pointer normalization for patch storage, operation equality, and hashing. |
Review effort: Balanced
| # concrete, JSON-serializable operation dictionaries on success. | ||
| if not isinstance(op, dict): | ||
| self.patch[index] = dict(op) | ||
| self.patch[index] = _plain_operation(op, self.pointer_cls) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

The
pathandfromof an operation can be given as pointer objects instead of strings. Such a patch could be applied, but:Changes
JsonPatchreplaces pointer objects by their paths inpatch.patch, soto_string(),str(), iteration,==andhash()all see strings. This is done in copies: the operation dicts the patch was created from keep their pointer objects.PatchOperation.__eq__and__hash__compare by the paths too, so an operation created directly with a pointer object equals one created with the string.What stays the same
A
PatchOperationstill shares the dict it was created from, also when that dict holds pointer objects, because its setters (key,from_key, …) write back into it and an existing test relies on that. My first attempt copied the dict there and broke that test, so the normalisation happens only inJsonPatchand in the comparisons.Visible change
patch.patchnow contains path strings where it used to contain the pointer objects that were passed in.Tests
Seven new tests: serialisation, equality and hashing of patches and of single operations, read-only mapping inputs, a pointer class with a different path syntax, the setters, and that the caller's operations are not modified.
python tests.pyandproperty_tests.pypass with coverage at 100%; flake8 is clean (Python 3.13).Generated by Claude Code