Skip to content

[WIP] Coercing to Unicode #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions jsonpatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,18 @@ def _item_added(self, path, key, item):
index = self.take_index(item, _ST_REMOVE)
if index is not None:
op = index[2]
if type(op.key) == int and type(key) == int:
for v in self.iter_from(index):
op.key = v._on_undo_remove(op.path, op.key)
# We can't rely on the op.key type since PatchOperation casts
# the .key property to int and this path wrongly ends up being taken
# for numeric string dict keys while the intention is to only handle lists.
# So we do an explicit check on the data structure type instead.
try:
src_item = op.pointer.to_last(self.src_doc)[0]
if type(src_item) == list:
for v in self.iter_from(index):
op.key = v._on_undo_remove(op.path, op.key)
except (KeyError, IndexError):
# If the path doesn't exist, assume it's not a list operation
pass

self.remove(index)
if op.location != _path_join(path, key):
Expand Down
10 changes: 10 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,16 @@ def test_issue119(self):
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)

def test_numeric_string_dict_keys(self):
"""Test the reported issue with numeric string dict keys"""
src = {'1': 'def'}
dst = {'test': '1', 'b': 'def'}
# This should not raise an exception
patch = jsonpatch.make_patch(src, dst)
# Verify the patch works as expected
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)

def test_issue120(self):
"""Make sure it avoids casting numeric str dict key to int"""
src = [{'foobar': {'821b7213_b9e6_2b73_2e9c_cf1526314553': ['Open Work'],
Expand Down
16 changes: 16 additions & 0 deletions tests/test_issue_bug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import unittest
import jsonpatch

class IssueBugTestCase(unittest.TestCase):
def test_numeric_string_dict_keys(self):
"""Test the issue with numeric string dict keys"""
src = {'1': 'def'}
dst = {'test': '1', 'b': 'def'}
# This should not raise an exception
patch = jsonpatch.make_patch(src, dst)
# Verify the patch works as expected
applied = jsonpatch.apply_patch(src, patch)
self.assertEqual(applied, dst)

if __name__ == '__main__':
unittest.main()