From 8d15ed5740027d5c0f295f82b347d963c77b8c5e Mon Sep 17 00:00:00 2001 From: Ryan Marvin Date: Mon, 1 Feb 2021 16:58:05 +0300 Subject: Fix make_patch --- jsonpatch.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'jsonpatch.py') diff --git a/jsonpatch.py b/jsonpatch.py index 84f6fb3..14341d7 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -635,7 +635,7 @@ class JsonPatch(object): True """ json_dumper = dumps or cls.json_dumper - builder = DiffBuilder(json_dumper, pointer_cls=pointer_cls) + builder = DiffBuilder(src, dst, json_dumper, pointer_cls=pointer_cls) builder._compare_values('', None, src, dst) ops = list(builder.execute()) return cls(ops, pointer_cls=pointer_cls) @@ -688,12 +688,14 @@ class JsonPatch(object): class DiffBuilder(object): - def __init__(self, dumps=json.dumps, pointer_cls=JsonPointer): + def __init__(self, src_doc, dst_doc, dumps=json.dumps, pointer_cls=JsonPointer): self.dumps = dumps self.pointer_cls = pointer_cls self.index_storage = [{}, {}] self.index_storage2 = [[], []] self.__root = root = [] + self.src_doc = src_doc + self.dst_doc = dst_doc root[:] = [root, root, None] def store_index(self, value, index, st): @@ -800,7 +802,8 @@ class DiffBuilder(object): new_index = self.insert(new_op) if index is not None: op = index[2] - if type(op.key) == int: + added_item = op.pointer.to_last(self.dst_doc)[0] + if type(added_item) == list: for v in self.iter_from(index): op.key = v._on_undo_add(op.path, op.key) -- cgit v1.2.1 From 78abec1651c4c3166d0eda4f9c0e43e00df57494 Mon Sep 17 00:00:00 2001 From: Ryan Marvin Date: Thu, 18 Feb 2021 18:25:25 +0300 Subject: Add comment --- jsonpatch.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'jsonpatch.py') diff --git a/jsonpatch.py b/jsonpatch.py index 14341d7..7b895b7 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -802,6 +802,10 @@ class DiffBuilder(object): new_index = self.insert(new_op) if index is not None: op = index[2] + # We can't rely on the op.key property 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 item affected by the op instead. added_item = op.pointer.to_last(self.dst_doc)[0] if type(added_item) == list: for v in self.iter_from(index): -- cgit v1.2.1 From f6b26b25805f1c01c3fae1495176cefac7d4a158 Mon Sep 17 00:00:00 2001 From: Ryan Marvin Date: Thu, 18 Feb 2021 18:27:21 +0300 Subject: Update comment --- jsonpatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'jsonpatch.py') diff --git a/jsonpatch.py b/jsonpatch.py index 7b895b7..b4ff24b 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -802,7 +802,7 @@ class DiffBuilder(object): new_index = self.insert(new_op) if index is not None: op = index[2] - # We can't rely on the op.key property type since PatchOperation casts + # 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 item affected by the op instead. -- cgit v1.2.1