JSON-Patch icon indicating copy to clipboard operation
JSON-Patch copied to clipboard

Undocumented behavior: jsonpatch.compare alters obj1

Open DavidMikeSimon opened this issue 9 years ago • 5 comments

The compare function calls _generate internally, which, in addition to generating a patches array, updates the mirror object to match the obj object! This caught me by surprise and created a bunch of hard-to-debug problems.

Please document this behavior in the API docs, or better yet, allow this behavior to be disabled (since now I have to make lots of deep clones every time I call jsonpatch.compare to keep it from messing with my program state...)

DavidMikeSimon avatar Jan 28 '16 23:01 DavidMikeSimon

cc @tomalec, @Starcounter-Jack

miyconst avatar Jan 29 '16 07:01 miyconst

Thanks for pointing that!

I would said that from API purity perspective we should not update mirror object on every (public) compare call, maybe unless explicitly specified.

I think we may consider, if possible, adding a parameter for that.

tomalec avatar Jan 29 '16 14:01 tomalec

Agreed.

Actually we addressed this problem before (there was a PR: https://github.com/Starcounter-Jack/JSON-Patch/pull/53), so that sounds like a regression.

There is even a test for that: https://github.com/Starcounter-Jack/JSON-Patch/commit/b2435fa725a2a4efc7eb7b7059ddf5fa4c344771#diff-2f9a66403e6f581b7b3abaff2c80ca45R694. Maybe the test is too naive.

warpech avatar Jan 29 '16 15:01 warpech

Isn't this fixed by https://github.com/Starcounter-Jack/JSON-Patch/pull/167?

alshakero avatar Jun 08 '17 15:06 alshakero

Looking at just code, I'm not so sure https://github.com/Starcounter-Jack/JSON-Patch/pull/167/files#diff-2b195b8e1323c973a44eefb88a63a5c7R778 does not clone.

tomalec avatar Jun 12 '17 12:06 tomalec