Skip to content

Raise JsonPatchConflict instead of TypeError on invalid move/copy/remove targets - #188

Open
eeshsaxena wants to merge 1 commit into
stefankoegl:masterfrom
eeshsaxena:fix/typeerror-on-dash-and-string-index
Open

Raise JsonPatchConflict instead of TypeError on invalid move/copy/remove targets#188
eeshsaxena wants to merge 1 commit into
stefankoegl:masterfrom
eeshsaxena:fix/typeerror-on-dash-and-string-index

Conversation

@eeshsaxena

Copy link
Copy Markdown

Some patches make apply_patch raise a bare TypeError instead of a JsonPatchConflict:

import jsonpatch
jsonpatch.apply_patch([1, 2, 3], [{'op': 'copy', 'path': '/0', 'from': '/-'}])
# TypeError: list indices must be integers or slices, not str
jsonpatch.apply_patch({'foo': 'bar'}, [{'op': 'remove', 'path': '/foo/0'}])
# TypeError: 'str' object doesn't support item deletion

move/copy read subobj[part] from the from pointer and remove does del subobj[part]. When the pointer ends in - (the array-append token), part is the string '-' used to index a list; when it points into a string value the container is immutable. Both raise TypeError, which isn't in the except (KeyError, IndexError) clauses. I added TypeError to those three clauses so the operations report JsonPatchConflict like other invalid targets. Valid moves/copies/removes are unaffected.

Added three tests to ConflictTests; they raise TypeError on master and pass with the change, and the full suite still passes. Found it by fuzzing apply_patch with random patches.

…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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TypeError in RemoveOperation, MoveOperation, and CopyOperation when indexing/deleting with invalid targets and re-raise as JsonPatchConflict.
  • Add regression tests covering from: "/-" for copy/move and 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.

Comment thread jsonpatch.py
Comment on lines +249 to 254
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)
@eeshsaxena

Copy link
Copy Markdown
Author

Hi! Gentle nudge on this one whenever you have some bandwidth. It's a small, self-contained fix (Raise JsonPatchConflict instead of TypeError on invalid move/copy/remove targets), and it's currently mergeable with no conflicts. No urgency at all, and I'm happy to make any changes you'd like. Thanks for maintaining python-json-patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants