Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate corrupts patches when adding array then appending #76

Closed
albanm opened this issue Aug 11, 2015 · 10 comments
Closed

validate corrupts patches when adding array then appending #76

albanm opened this issue Aug 11, 2015 · 10 comments
Labels

Comments

@albanm
Copy link

albanm commented Aug 11, 2015

Hello,

You can try the following code:

var jsonpatch = require('./src/json-patch');
var patches = [{op: 'add', value: [], path: '/foo'}, {op: 'add', value: 2, path: '/foo/-'}];
jsonpatch.validate(patches, {});
console.log(patches);

It outputs:

[ { op: 'add', value: [ 2 ], path: '/foo' },
  { op: 'add', value: 2, path: '/foo/-' } ]

As you can see the value object of the first patch has been modified. Instead of an empty array it is now an array containing the value of the second patch.

@baranga
Copy link
Contributor

baranga commented May 3, 2016

This also works with objects:

var jsonpatch = require('./src/json-patch');
var patches = [{op: 'add', value: {}, path: '/foo'}, {op: 'add', value: 'a', path: '/foo/a'}];
jsonpatch.validate(patches, {});
console.log(patches);

Output:

[ { op: 'add', value: { a: 'a' }, path: '/foo' },
  { op: 'add', value: 'a', path: '/foo/a' } ]

Looks like patch value gets copied by reference instead of deep copy.

baranga added a commit to baranga/JSON-Patch that referenced this issue May 3, 2016
@MarkHerhold
Copy link
Contributor

How does this work with undefined values? IIRC, this repo tries to respect
undefined fields despite them not being serializable to/from JSON (which is
a bad idea anyway IMO) .

On Tue, May 3, 2016, 9:53 AM baranga notifications@github.com wrote:

This also works with objects:

var jsonpatch = require('./src/json-patch');
var patches = [{op: 'add', value: {}, path: '/foo'}, {op: 'add', value: 'a', path: '/foo/a'}];
jsonpatch.validate(patches, {});
console.log(patches);

Looks like patch value gets copied by reference instead of deep copy.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#76 (comment)

@baranga
Copy link
Contributor

baranga commented May 3, 2016

@MarkHerhold I don't understand your question. How to deep copy something with a undefined in it? That should not happen because you can not express a patch operation with value of undefined.

@MarkHerhold
Copy link
Contributor

MarkHerhold commented May 4, 2016

@baranga You are right, it shouldn't happen in the traditional JSON sense, but JSON-Patch treats undefined as a first-class citizen. ;)

Here's an example:

> var jsonpatch = require('fast-json-patch')
undefined
> var obj = {};
undefined
> jsonpatch.apply(obj, [{op: 'add', path:"/thing", value: undefined}]);
true
> Object.keys(obj)
[ 'thing' ]
> obj.thing
undefined

See also #90 #60 #32 and probably others...

@baranga
Copy link
Contributor

baranga commented May 4, 2016

@MarkHerhold That still works. deepClone only does a JSON encode/decode roundtrip if value is of type object. See json-patch-duplex.ts.

Is that behaviour covered by the test suites? I run them with my modifications and got same errors as on master.

@baranga
Copy link
Contributor

baranga commented May 4, 2016

Ok, now I see the issue:

> var jsonpatch = require('./src/json-patch-duplex')
undefined
> var obj = {}
undefined
> jsonpatch.apply(obj, [{op: 'add', path: '/deepThing', value: {undef: undefined}}])
true
> Object.keys(obj)
[ 'deepThing' ]
> Object.keys(obj.deepThing)
[]

But the last should be:

> Object.keys(obj.deepThing)
[ 'undef' ]

@baranga
Copy link
Contributor

baranga commented May 4, 2016

@MarkHerhold So right now something broken can't be fixed because the lib supports a non standard feature?

@MarkHerhold
Copy link
Contributor

No, there are other more expensive clone strategies and implementations.
e.g. lodash's clone.

I personally think we should cut a v1 release, dropping support for
undefined and apply your PR.

On Wed, May 4, 2016, 3:19 AM baranga notifications@github.com wrote:

@MarkHerhold https://github.com/MarkHerhold So right now something
broken can't be fixed because the lib supports a non standard feature?


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#76 (comment)

warpech added a commit that referenced this issue Jun 3, 2016
this is a fix for issues #105, #90, #60, #32 and a part of #76 that describes a problem with undefined
all tests pass after this commit
@warpech
Copy link
Collaborator

warpech commented Jun 21, 2016

The part regarding undefined is fixed in 1.0.0

baranga added a commit to baranga/JSON-Patch that referenced this issue Dec 16, 2016
@baranga
Copy link
Contributor

baranga commented Dec 16, 2016

This issue is still present. I updated my possible fix #100 to newest version: #134. This comes with a speed penalty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants