Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong generated JSON patches when using .compare()

JbRezidu opened this issue · comments

Hi,

I'm using the last version of json-fast-patch (3.0.0-1) and I'm having an issue in a special case when generating JSON patches.

The problem is simple, I have this code :

const rfc6902 = require('rfc6902');
const fastJsonPatch = require('fast-json-patch');

const oldFormat = {
  myProperty: {
    myOtherProperty: [
      1,
      2
    ]
  }
};
const newFormat = {
  myProperty: [
    {myOtherProperty: 1},
    {myOtherProperty: 2}
  ]
};

const patchesFromRFC = rfc6902.createPatch(oldFormat, newFormat);
console.log('Patches from RFC : ', JSON.stringify(patchesFromRFC, null, 2));

const patchesFromFastJsonPatch = fastJsonPatch.compare(oldFormat, newFormat);
console.log('Patches from fast-json-patch : ', JSON.stringify(patchesFromFastJsonPatch, null, 2));

It is a simple new format of one element and we want the JSON patch related to the transformation into the new format.
The result is this one :

Patches from RFC :  [
  {
    "op": "replace",
    "path": "/myProperty",
    "value": [
      {
        "myOtherProperty": 1
      },
      {
        "myOtherProperty": 2
      }
    ]
  }
]
Patches from fast-json-patch :  [
  {
    "op": "replace",
    "path": "/myProperty",
    "value": [
      {
        "myOtherProperty": 1
      },
      {
        "myOtherProperty": 2
      }
    ]
  },
  {
    "op": "add",
    "path": "/myProperty/0",
    "value": {
      "myOtherProperty": 1
    }
  },
  {
    "op": "add",
    "path": "/myProperty/1",
    "value": {
      "myOtherProperty": 2
    }
  }
]

I don't know why but json-fast-patch add two patches that are useless (I guess) and that breaks the thing when reapplying patches.
The module rfc6902 (version 4.0.1) do the right thing and without these two added patches at the end, json-fast-patch will also do the right thing.
Am I missing something, or is it a real bug from json-fast-patch?

PS : With an array of only one element, there is no problem json-fast-patch just return the 'replace' operation patch.

Hi !

I saw that the issue is fixed on the master branch. However there isn't a release for almost 2 years...

@Starcounter-Jack Do you think you could make a new one, in order to get the new version ?

Thanks in advance :)

@lucactusss
There is a new version going out today