stefankoegl / python-json-patch

Applying JSON Patches in Python
https://python-json-patch.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
440 stars 96 forks source link

Add operation doesnt support nested paths, if they dont exist already #153

Closed Rexxz closed 9 months ago

Rexxz commented 9 months ago
import jsonpatch

patch = jsonpatch.JsonPatch(
    [{'op': 'add', 'path': '/contact/email', 'value': 'test@test.com'}]
)

dictionary = {
    'system': {
        'name': 'test-system'
    }
}
print(patch.apply(dictionary))

returns Traceback (most recent call last): File "C:\Users\Mike\IdeaProjects\test\test.py", line 13, in print(patch.apply(dictionary)) File "C:\Users\Mike\IdeaProjects\test\venv\lib\site-packages\jsonpatch.py", line 692, in apply obj = operation.apply(obj) File "C:\Users\Mike\IdeaProjects\test\venv\lib\site-packages\jsonpatch.py", line 282, in apply subobj, part = self.pointer.to_last(obj) File "C:\Users\Mike\IdeaProjects\test\venv\lib\site-packages\jsonpointer.py", line 196, in to_last doc = self.walk(doc, part) File "C:\Users\Mike\IdeaProjects\test\venv\lib\site-packages\jsonpointer.py", line 291, in walk raise JsonPointerException("member '%s' not found in %s" % (part, doc)) jsonpointer.JsonPointerException: member 'contact' not found in {'system': {'name': 'test-system'}}

Rexxz commented 9 months ago

Dont worry, having looked at the source I can understand why you dont support this and will implement a utiliy to ensure a path exists before using the patch