Skip to content

Commit

Permalink
fix issue Starcounter-Jack#76 by deep cloning patch values
Browse files Browse the repository at this point in the history
  • Loading branch information
baranga committed Dec 18, 2016
1 parent 0d7a628 commit bfb7aea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
20 changes: 15 additions & 5 deletions src/json-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ var jsonpatch;
return false;
}
}
function _deepClone(value) {
if (typeof value === 'object') {
return JSON.parse(JSON.stringify(value));
}
else {
return value;
}
}
/* We use a Javascript hash to store each
function. Each hash entry (property) uses
the operation identifiers specified in rfc6902.
Expand All @@ -64,7 +72,7 @@ var jsonpatch;
/* The operations applicable to an object */
var objOps = {
add: function (obj, key) {
obj[key] = this.value;
obj[key] = _deepClone(this.value);
},
remove: function (obj, key) {
var removed = obj[key];
Expand All @@ -73,7 +81,7 @@ var jsonpatch;
},
replace: function (obj, key) {
var removed = obj[key];
obj[key] = this.value;
obj[key] = _deepClone(this.value);
return removed;
},
move: function (obj, key, tree) {
Expand Down Expand Up @@ -109,7 +117,7 @@ var jsonpatch;
/* The operations applicable to an array. Many are the same as for the object */
var arrOps = {
add: function (arr, i) {
arr.splice(i, 0, this.value);
arr.splice(i, 0, _deepClone(this.value));
// this may be needed when using '-' in an array
return i;
},
Expand All @@ -119,7 +127,7 @@ var jsonpatch;
},
replace: function (arr, i) {
var removed = arr[i];
arr[i] = this.value;
arr[i] = _deepClone(this.value);
return removed;
},
move: objOps.move,
Expand All @@ -133,7 +141,7 @@ var jsonpatch;
rootOps.remove.call(this, obj);
for (var key in this.value) {
if (this.value.hasOwnProperty(key)) {
obj[key] = this.value[key];
obj[key] = _deepClone(this.value[key]);
}
}
},
Expand Down Expand Up @@ -198,6 +206,8 @@ var jsonpatch;
*/
function apply(tree, patches, validate) {
var results = new Array(patches.length), p = 0, plen = patches.length, patch, key;
// deep clone patch set to avoid modifications of it
//patches = JSON.parse(JSON.stringify(patches));
while (p < plen) {
patch = patches[p];
p++;
Expand Down
18 changes: 13 additions & 5 deletions src/json-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ module jsonpatch {
}
}

function _deepClone(value) {
if (typeof value === 'object') {
return JSON.parse(JSON.stringify(value));
} else {
return value;
}
}

/* We use a Javascript hash to store each
function. Each hash entry (property) uses
the operation identifiers specified in rfc6902.
Expand All @@ -77,7 +85,7 @@ module jsonpatch {
/* The operations applicable to an object */
var objOps = {
add: function (obj, key) {
obj[key] = this.value;
obj[key] = _deepClone(this.value);
},
remove: function (obj, key) {
var removed = obj[key];
Expand All @@ -86,7 +94,7 @@ module jsonpatch {
},
replace: function (obj, key) {
var removed = obj[key];
obj[key] = this.value;
obj[key] = _deepClone(this.value);
return removed;
},
move: function (obj, key, tree) {
Expand Down Expand Up @@ -125,7 +133,7 @@ module jsonpatch {
/* The operations applicable to an array. Many are the same as for the object */
var arrOps = {
add: function (arr, i) {
arr.splice(i, 0, this.value);
arr.splice(i, 0, _deepClone(this.value));
// this may be needed when using '-' in an array
return i;
},
Expand All @@ -135,7 +143,7 @@ module jsonpatch {
},
replace: function (arr, i) {
var removed = arr[i];
arr[i] = this.value;
arr[i] = _deepClone(this.value);
return removed;
},
move: objOps.move,
Expand All @@ -150,7 +158,7 @@ module jsonpatch {
rootOps.remove.call(this, obj);
for (var key in this.value) {
if (this.value.hasOwnProperty(key)) {
obj[key] = this.value[key];
obj[key] = _deepClone(this.value[key]);
}
}
},
Expand Down

0 comments on commit bfb7aea

Please sign in to comment.