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

Generated patch contains array length #14

Closed
warpech opened this issue Aug 12, 2013 · 6 comments
Closed

Generated patch contains array length #14

warpech opened this issue Aug 12, 2013 · 6 comments

Comments

@warpech
Copy link
Collaborator

warpech commented Aug 12, 2013

In native Object.observe version (Chrome 28 & 30), array length is reported as part of the change records.

I don't think this is an error in Chrome, but I think JSON-Patch should filter out length from it's own generated patch

See http://jsfiddle.net/U4mZJ/3/

For a sample array, generated patch is:

[
 {"op":"replace","path":"/0","value":99},
 {"op":"add","path":"/2","value":3},
 {"op":"replace","path":"/length","value":3}
]

I will fix this soon

@psmolenski
Copy link
Contributor

I have prepared a sample test case:

it('should generate the same patch using Object.observe and shim', function() {
      var arr1 = [
        ["Albert", "Einstein"],
        ["Erwin", "Shrodinger"]
      ];
      var arr2 = arr1.slice();
      var newRecord = ['Niels', 'Bohr'];
      var observer1 = jsonpatch.observe(arr1);
      arr1.push(newRecord);
      var objectObservePatches = jsonpatch.generate(observer1);
      var _observe = Object.observe;
      Object.observe = undefined;
      var observer2 = jsonpatch.observe(arr2);
      arr2.push(newRecord);
      var shimPatches = jsonpatch.generate(observer2);
//fails because objectObservePathes has an extra patch with "length" property
      expect(objectObservePatches).toEqual(shimPatches); 
      Object.observe = _observe;
    });

@warpech
Copy link
Collaborator Author

warpech commented Oct 10, 2013

After studying http://tools.ietf.org/html/rfc6902 and some sample test cases here https://github.com/mikemccabe/json-patch-tests I think the only good solution is to NOT include length in the produced patch. I will submit a fix

@warpech
Copy link
Collaborator Author

warpech commented Oct 11, 2013

Thanks @psmolenski, I added your test to the test suite

warpech added a commit that referenced this issue Oct 11, 2013
@warpech
Copy link
Collaborator Author

warpech commented Oct 11, 2013

This issue is fixed in v0.3.3

@tomalec
Copy link
Collaborator

tomalec commented Apr 4, 2015

It seems that such behavior is not a bug in Chrome, but desired behavior for Object.observe it's even mentioned in spec.

That's the main difference with Array.observe, which I believe we should use for array(-like) nodes.

@tomalec
Copy link
Collaborator

tomalec commented Apr 4, 2015

I'll clean our .length logic at 0552ac6. So users may use 'length' property name for non-array objects. For arrays, we will handle splice change type.

tomalec added a commit that referenced this issue Apr 4, 2015
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

No branches or pull requests

3 participants