Generated patch contains array `length` #14

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

Comments

Projects
None yet
3 participants
@warpech
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

This comment has been minimized.

Show comment Hide comment
@psmolenski

psmolenski Aug 13, 2013

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;
    });
Contributor

psmolenski commented Aug 13, 2013

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

This comment has been minimized.

Show comment Hide comment
@warpech

warpech Oct 10, 2013

Collaborator

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

Collaborator

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

This comment has been minimized.

Show comment Hide comment
@warpech

warpech Oct 11, 2013

Collaborator

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

Collaborator

warpech commented Oct 11, 2013

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

@warpech warpech closed this in c3702e9 Oct 11, 2013

warpech added a commit that referenced this issue Oct 11, 2013

@warpech

This comment has been minimized.

Show comment Hide comment
@warpech

warpech Oct 11, 2013

Collaborator

This issue is fixed in v0.3.3

Collaborator

warpech commented Oct 11, 2013

This issue is fixed in v0.3.3

@tomalec

This comment has been minimized.

Show comment Hide comment
@tomalec

tomalec Apr 4, 2015

Collaborator

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.

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

This comment has been minimized.

Show comment Hide comment
@tomalec

tomalec Apr 4, 2015

Collaborator

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.

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