diff --git a/src/jsonpatcherproxy.js b/src/jsonpatcherproxy.js index 8cd7227..a1a8539 100644 --- a/src/jsonpatcherproxy.js +++ b/src/jsonpatcherproxy.js @@ -85,6 +85,10 @@ const JSONPatcherProxy = (function() { if (isObject(newValue)) { const subtreeMetadata = newValue[instance._metadataSymbol]; if (subtreeMetadata) { + if(subtreeMetadata.parent === tree && subtreeMetadata.keyInParent === key) { + //this is the same object that we already proxified, proxified now by someone else. In this case, remain silent + return Reflect.set(tree, key, newValue); + } subtreeMetadata.parent = tree; subtreeMetadata.keyInParent = key; /* diff --git a/test/spec/proxySpec.js b/test/spec/proxySpec.js index e199d44..5263ccb 100644 --- a/test/spec/proxySpec.js +++ b/test/spec/proxySpec.js @@ -271,6 +271,30 @@ describe('proxy', function() { obj2.phoneNumbers[1].number = '456'; expect(observedObj).toReallyEqual(obj2); //objects should be still the same }); + + it('should generate replace (deep object, proxified)', function() { + const obj = generateDeepObjectFixture(); + const jsonPatcherProxy = new JSONPatcherProxy(obj); + let observedObj = jsonPatcherProxy.observe(true); + + //begin external proxification + observedObj = new Proxy(observedObj, {}); + observedObj.phoneNumbers = new Proxy(observedObj.phoneNumbers, {}); + observedObj.phoneNumbers[0] = new Proxy(observedObj.phoneNumbers[0], {}); + observedObj.phoneNumbers[1] = new Proxy(observedObj.phoneNumbers[1], {}); + //end external proxification + + observedObj.phoneNumbers[0].number = '123'; + + const patches = jsonPatcherProxy.generate(); + expect(patches).toReallyEqual([ + { + op: 'replace', + path: '/phoneNumbers/0/number', + value: '123' + } + ]); + }); it('should generate replace (changes in new array cell, primitive values)', function() { var arr = [1];