Raise JsonPatchConflict instead of TypeError on invalid move/copy/remove targets - #188
Conversation
…ove targets move and copy read subobj[part] from the 'from' pointer, and remove does del subobj[part]. When the pointer ends in '-' (the array-append token) the part is a string used to index a list, and when it points into a string value the container is immutable; both raised a bare TypeError. Catch TypeError alongside KeyError/IndexError so these surface as JsonPatchConflict.
There was a problem hiding this comment.
Pull request overview
This PR improves error consistency in apply_patch by ensuring invalid move/copy/remove targets raise JsonPatchConflict instead of leaking bare TypeError, aligning these operations with how other invalid-pointer scenarios are reported in the library.
Changes:
- Catch
TypeErrorinRemoveOperation,MoveOperation, andCopyOperationwhen indexing/deleting with invalid targets and re-raise asJsonPatchConflict. - Add regression tests covering
from: "/-"forcopy/moveand removing through a pointer into a string value.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| jsonpatch.py | Expands exception handling for remove/move/copy to convert certain TypeErrors into JsonPatchConflict. |
| tests.py | Adds regression tests ensuring these invalid targets raise JsonPatchConflict rather than TypeError. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| except (KeyError, IndexError, TypeError) as ex: | ||
| # TypeError happens when subobj is not a mutable container, e.g. a | ||
| # pointer into a string value ("str object doesn't support item | ||
| # deletion"). | ||
| msg = "can't remove a non-existent object '{0}'".format(part) | ||
| raise JsonPatchConflict(msg) |
|
Hi! Gentle nudge on this one whenever you have some bandwidth. It's a small, self-contained fix ( |
Some patches make
apply_patchraise a bareTypeErrorinstead of aJsonPatchConflict:move/copyreadsubobj[part]from thefrompointer andremovedoesdel subobj[part]. When the pointer ends in-(the array-append token),partis the string'-'used to index a list; when it points into a string value the container is immutable. Both raiseTypeError, which isn't in theexcept (KeyError, IndexError)clauses. I addedTypeErrorto those three clauses so the operations reportJsonPatchConflictlike other invalid targets. Valid moves/copies/removes are unaffected.Added three tests to
ConflictTests; they raise TypeError onmasterand pass with the change, and the full suite still passes. Found it by fuzzingapply_patchwith random patches.