python-json-patch icon indicating copy to clipboard operation
python-json-patch copied to clipboard

How to get test patch?

Open michaelrice opened this issue 5 years ago • 3 comments

Given:

d1 = {1: "a", 2: "b"}
d2 = {1: "b", 2: "v"}
patch = jsonpatch.make_patch(d1, d2)
patch.to_string()
'[{"value": "b", "op": "replace", "path": "/1"}, {"value": "v", "op": "replace", "path": "/2"}]'

How can I get the patch that would have test instead of replace?

michaelrice avatar Jun 21 '19 04:06 michaelrice

https://python-json-patch.readthedocs.io/en/latest/mod-jsonpatch.html#jsonpatch.make_patch does apply() not work?

Xronophobe avatar Jul 11 '19 22:07 Xronophobe

@Xronophobe Im not sure I understand what you mean? My use case is to have jsonpatch generate a patch that I send to a remote API in a PATCH operation. Right now Im only able to get a patch object that has replace but Id like to get the patch to have test in it as well so I can submit it first to make sure the API would be happy, then if I get a success response from my test patch I can follow up with another call that has the real patch in it with replace

michaelrice avatar Jul 12 '19 01:07 michaelrice

The API that I'm working against also requires a test operation on the existing value before any other operation can take place. As a work-around for this, I switched around the old and new document and changed the "op" to "test" on the resulting patch document. Maybe there's a smarter way?

jschristensen avatar Aug 31 '21 11:08 jschristensen