Skip to content

Commit 3104b58

Browse files
Copilotstefankoegl
andcommitted
Fix bug with numeric string dict keys in _item_added method
Co-authored-by: stefankoegl <184196+stefankoegl@users.noreply.github.com>
1 parent ae41173 commit 3104b58

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

jsonpatch.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,9 +797,18 @@ def _item_added(self, path, key, item):
797797
index = self.take_index(item, _ST_REMOVE)
798798
if index is not None:
799799
op = index[2]
800-
if type(op.key) == int and type(key) == int:
801-
for v in self.iter_from(index):
802-
op.key = v._on_undo_remove(op.path, op.key)
800+
# We can't rely on the op.key type since PatchOperation casts
801+
# the .key property to int and this path wrongly ends up being taken
802+
# for numeric string dict keys while the intention is to only handle lists.
803+
# So we do an explicit check on the data structure type instead.
804+
try:
805+
src_item = op.pointer.to_last(self.src_doc)[0]
806+
if type(src_item) == list:
807+
for v in self.iter_from(index):
808+
op.key = v._on_undo_remove(op.path, op.key)
809+
except (KeyError, IndexError):
810+
# If the path doesn't exist, assume it's not a list operation
811+
pass
803812

804813
self.remove(index)
805814
if op.location != _path_join(path, key):

0 commit comments

Comments
 (0)