json-patch / json-patch-tests

Tests for implementations of json-patch
68 stars 21 forks source link

Multiple `remove` operations to the same Array #14

Closed rkusa closed 10 years ago

rkusa commented 10 years ago

I was just wondering what to expect from a patch containing multiple remove operations to the same array, e.g.:

Object: { "arr": [1, 2, 3, 4] }
Patch:

[
  { "op": "remove", "path": "/arr/1" },
  { "op": "remove", "path": "/arr/2" }
]

Should the result be { "arr": [1, 4] } or { "arr": [1, 3] }. Or in words: should every remove operation be applied before shifting existing elements to the left, or should existing elements be shifted to the left after each remove operation?

This case is maybe worth adding a test for.

almost commented 10 years ago

Definitely the latter. Each operation should be fully applied before the next starts. I'm pretty sure that's how all existing implementations will do it. Definitely agree that a test should be put in to make sure though.

rkusa commented 10 years ago

Thanks for the answer @almost! I also found the relevant part in the spec.

Operations are applied sequentially in the order they appear in the array. Each operation in the sequence is applied to the target document; the resulting document becomes the target of the next operation. Evaluation continues until all operations are successfully applied or until an error condition is encountered.

So as you said, the latter is correct. Thanks

mikemccabe commented 10 years ago

Can't hurt! One could imagine someone trying to collapse multiple patches and going astray. Also, this tripped up my implementation's diff(), so... good test! (My implementation mines these testcases to make diffs and reverse diffs between 'doc' and 'expected.).