Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Src/JsonDiffPatchDotNet.UnitTests/UnpatchUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

Expand Down Expand Up @@ -238,5 +238,33 @@ public void Unpatch_Bug17EfficienText_Success()

Assert.IsTrue(JToken.DeepEquals(left.ToString(), patched.ToString()));
}

[Test]
public void Unpatch_ObjectIfOldValueMatches_Success()
{
var jdpUnpatchIgnoreOriginalValue = new JsonDiffPatch(new Options { UnpatchIgnoreOriginalValue = false });
var jdp = new JsonDiffPatch();
var stage1 = JToken.Parse(@"{""stage"":1}");
var stage2 = JToken.Parse(@"{""stage"":2}");
var stage3 = JToken.Parse(@"{""stage"":3}");

var diff = jdp.Diff(stage1, stage2);

// Try to unpatch with stage2 not ignoring original value
var unpatched_stage2 = jdpUnpatchIgnoreOriginalValue.Unpatch(stage2, diff);
Assert.AreEqual(stage1.ToString(), unpatched_stage2.ToString());

// Try to unpatch with stage3 not ignoring original value
var unpatched_stage3 = jdpUnpatchIgnoreOriginalValue.Unpatch(stage3, diff);
Assert.AreNotEqual(stage1.ToString(), unpatched_stage3.ToString());

// Try to unpatch with stage2 ignoring original value
unpatched_stage2 = jdp.Unpatch(stage2, diff);
Assert.AreEqual(stage1.ToString(), unpatched_stage2.ToString());

// Try to unpatch with stage3 ignoring original value
unpatched_stage3 = jdp.Unpatch(stage3, diff);
Assert.AreEqual(stage1.ToString(), unpatched_stage3.ToString());
}
}
}
9 changes: 8 additions & 1 deletion Src/JsonDiffPatchDotNet/JsonDiffPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ public JToken Unpatch(JToken right, JToken patch)

if (patchArray.Count == 2) // Replace
{
return patchArray[0];
if (_options.UnpatchIgnoreOriginalValue || right != null && right.Type == patchArray[1].Type && right.Value<JValue>().Equals(patchArray[1].Value<JValue>()))
{
return patchArray[0];
}
else
{
return right;
}
}

if (patchArray.Count == 3) // Delete, Move or TextDiff
Expand Down
5 changes: 4 additions & 1 deletion Src/JsonDiffPatchDotNet/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public Options()
TextDiff = TextDiffMode.Efficient;
MinEfficientTextDiffLength = 50;
DiffArrayOptions = new ArrayOptions();
UnpatchIgnoreOriginalValue = true;
}

/// <summary>
Expand Down Expand Up @@ -57,7 +58,9 @@ public Options()
/// To improve the results leveraging the power of LCS(and position move detection) you need to provide a way to compare 2 objects.
/// </summary>
public Func<JToken, object> ObjectHash { get; set; }
}

public bool UnpatchIgnoreOriginalValue { get; set; }
}

public sealed class ArrayOptions
{
Expand Down