Skip to content

apply_patch function failed for array in nested structure #41

Closed
@kevinxin90

Description

@kevinxin90

This issue is similar to issue #40 , but it happens when using the apply_patch function.

A 'JsonPatchConflict: can't insert outside of list' error would raise every time we try to use apply_patch function between two docs having array inside a nested structure.

Take the following example:

doc5 = {'school':{'names':['Kevin','Carl']}}

doc6 = {'school':{'names':['Carl','Kate','Kevin','Jake']}}

patch = jsonpatch.JsonPatch.from_diff(doc5, doc6)

jsonpatch.apply_patch(doc5,patch)

When it run it produce the following error:

----> 1 jsonpatch.apply_patch(doc5,patch)

/home/kevinxin/ENV/src/jsonpatch/jsonpatch.py in apply_patch(doc, patch, in_place)
    149     else:
    150         patch = JsonPatch(patch)
--> 151     return patch.apply(doc, in_place)
    152
    153

/home/kevinxin/ENV/src/jsonpatch/jsonpatch.py in apply(self, obj, in_place)
    347
    348         for operation in self._ops:
--> 349             obj = operation.apply(obj)
    350
    351         return obj

/home/kevinxin/ENV/src/jsonpatch/jsonpatch.py in apply(self, obj)
    498             'path': self.location,
    499             'value': value
--> 500         }).apply(obj)
    501
    502         return obj

/home/kevinxin/ENV/src/jsonpatch/jsonpatch.py in apply(self, obj)
    422
    423             elif part > len(subobj) or part < 0:
--> 424                 raise JsonPatchConflict("can't insert outside of list")
    425
    426             else:

JsonPatchConflict: can't insert outside of list

However, it the array is not in nested structure, the problem is gone.

Take the following example:

doc3 = {'names':['kevin','Carl']}

doc4 = {'names':['Carl','Kate','Kevin','Jake']}

patch = jsonpatch.JsonPatch.from_diff(doc3, doc4)

jsonpatch.apply_patch(doc3,patch)
Out: {'names': ['Carl', 'Kate', 'Kevin', 'Jake']}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions