Skip to content
Advertisement

Add new operations to existing jsonpatch file

I’m working on a test suite for some Java code that uses jsonpatch to modify db entries. What I am trying to do is have a template jsonpatch request saved down as a file that individual unit tests could read from, modify some operations, and then call the patch directly.

The rough structure is as follows:

jsonpatch template:

JavaScript

java code:

JavaScript

When I comment out the modify operation section everything works so I’m not having issues with importing or sending the patch. My struggle is with updating the template’s operations to fit. The paths are the same across tests but I need to try with different values each time since we’re using a persistent database for testing.

Is there a way to modify the value of an existing jsonpatch operation like I’m trying above? Failing that, can I add new operations to the existing jsonpatch?

Advertisement

Answer

After a lot of trial and error I got it to work by modifying the inputstream itself instead of the jsonpatch object.

I added some targets to the json template and did stream replacement on a copy of the inputstream to force in my desired values before converting it all into the final jsonpatch.

new template:

JavaScript

new code:

JavaScript

Getting the number value to work was a bit tricky since everything is strings, but I was able to crack it by having the replace operation get rid of its quotes which causes it to get picked up as a number by the jsonpatch.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement