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

Copy operation doesn't deep clone, only references #177

Closed
alshakero opened this issue May 21, 2017 · 3 comments
Closed

Copy operation doesn't deep clone, only references #177

alshakero opened this issue May 21, 2017 · 3 comments

Comments

@alshakero
Copy link
Collaborator

No description provided.

@felixfbecker
Copy link
Contributor

Why is that a problem?

@tomalec
Copy link
Collaborator

tomalec commented May 22, 2017

It may be for someone expecting resulting object to be a JSON tree. You cannot use JSON syntax to create reference, you can only create two identical nodes.

let obj = {
  foo: {
    bar: 'baz'
  }
};
jsonpatch.applyOperation(obj, {op: 'copy', path: 'copied', from: 'foo'});

at this point your object looks like:

obj:
  foo: {
    bar: 'baz'
  },
  foo: {
    bar: 'baz'
  }
};

with current implementation

jsonpatch.applyOperation(obj, {op: 'replace', path: 'copied/bar', from: 'buz'});

changes also foo/bar. Which is pretty confusing as jsonpatch.applyOperation(JSON.parse(JSON.stringify(obj)), {op: 'replace', path: 'copied/bar', from: 'buz'}); will not.

I think that's a difference between "copy" and "attach"

https://tools.ietf.org/html/rfc6902#section-4.5:

This operation is functionally identical to an "add" operation at the
target location using the value specified in the "from" member.

"Value" not "reference".

@alshakero
Copy link
Collaborator Author

Closing as fixed in #134

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

Successfully merging a pull request may close this issue.

3 participants