Skip to content

Commit

Permalink
DiffBuilder not handling added key in Dictionary - fix #35 (#36)
Browse files Browse the repository at this point in the history
* fix DiffBuilder not handling added key in Dictionary #35
  • Loading branch information
gcurb authored Aug 6, 2020
1 parent 16fcde1 commit 92591e1
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 6 deletions.
140 changes: 140 additions & 0 deletions src/2.1-JsonMergePatch.Tests/Builder/Diff/Dictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System.Collections.Generic;
using Morcatko.AspNetCore.JsonMergePatch.Builders;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Morcatko.AspNetCore.JsonMergePatch.Tests.Builders.Diff
{
public class Dictionary
{
class SimpleModelWithDictionary
{
public Dictionary<string, string> Dic { get; set; } = new Dictionary<string, string>();
}

[Fact]
public void NoChange()
{
var source = new SimpleModelWithDictionary();
var patched = new SimpleModelWithDictionary();

var diff = DiffBuilder.Build(source, patched);

Assert.Null(diff);
}

[Fact]
public void EmptyToNull()
{
var source = new SimpleModelWithDictionary();
var patched = new SimpleModelWithDictionary() { Dic = null };

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:null}"),
diff));
}

[Fact]
public void NullToEmpty()
{
var source = new SimpleModelWithDictionary() { Dic = null };
var patched = new SimpleModelWithDictionary();

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{}}"),
diff));
}

[Fact]
public void KeyAdded()
{
var source = new SimpleModelWithDictionary();
var patched = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val1" }
}
};

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{\"key1\": \"val1\"}}"),
diff));
}

[Fact]
public void KeyMissing()
{
var source = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val1" }
}
};
var patched = new SimpleModelWithDictionary();

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{\"key1\": null}}"),
diff));
}

[Fact]
public void ValueSetToNull()
{
var source = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val1" }
}
};
var patched = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", null }
}
};

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{\"key1\": null}}"),
diff));
}

[Fact]
public void ValueChanged()
{
var source = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val1" }
}
};
var patched = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val_changed" }
}
};

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{\"key1\": \"val_changed\"}}"),
diff));
}
}
}
7 changes: 4 additions & 3 deletions src/2.1-JsonMergePatch/Builders/DiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ private static JToken BuildDiff(JToken original, JToken patched)
private static JToken BuildObjectDiff(JObject original, JObject patched)
{
JObject result = new JObject();
var properties = original?.Properties() ?? patched.Properties();
foreach (var property in properties)
var propertyNames = original.Properties()
.Union(patched.Properties())
.Select(p => p.Name).Distinct();
foreach (var propertyName in propertyNames)
{
var propertyName = property.Name;
var originalJToken = original?.GetValue(propertyName);
var patchedJToken = patched?.GetValue(propertyName);

Expand Down
7 changes: 4 additions & 3 deletions src/3.0-JsonMergePatch.NewtonsoftJson/Builders/DiffBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ private static JToken BuildDiff(JToken original, JToken patched)
private static JToken BuildObjectDiff(JObject original, JObject patched)
{
JObject result = new JObject();
var properties = original?.Properties() ?? patched.Properties();
foreach (var property in properties)
var propertyNames = original.Properties()
.Union(patched.Properties())
.Select(p => p.Name).Distinct();
foreach (var propertyName in propertyNames)
{
var propertyName = property.Name;
var originalJToken = original?.GetValue(propertyName);
var patchedJToken = patched?.GetValue(propertyName);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System.Collections.Generic;
using Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson.Builders;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Morcatko.AspNetCore.JsonMergePatch.Tests.NewtonsoftJson.Builders.Diff
{
public class Dictionary
{
class SimpleModelWithDictionary
{
public Dictionary<string, string> Dic { get; set; } = new Dictionary<string, string>();
}

[Fact]
public void NoChange()
{
var source = new SimpleModelWithDictionary();
var patched = new SimpleModelWithDictionary();

var diff = DiffBuilder.Build(source, patched);

Assert.Null(diff);
}

[Fact]
public void EmptyToNull()
{
var source = new SimpleModelWithDictionary();
var patched = new SimpleModelWithDictionary() { Dic = null };

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:null}"),
diff));
}

[Fact]
public void NullToEmpty()
{
var source = new SimpleModelWithDictionary() { Dic = null };
var patched = new SimpleModelWithDictionary();

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{}}"),
diff));
}

[Fact]
public void KeyAdded()
{
var source = new SimpleModelWithDictionary();
var patched = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val1" }
}
};

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{\"key1\": \"val1\"}}"),
diff));
}

[Fact]
public void KeyMissing()
{
var source = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val1" }
}
};
var patched = new SimpleModelWithDictionary();

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{\"key1\": null}}"),
diff));
}

[Fact]
public void ValueSetToNull()
{
var source = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val1" }
}
};
var patched = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", null }
}
};

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{\"key1\": null}}"),
diff));
}

[Fact]
public void ValueChanged()
{
var source = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val1" }
}
};
var patched = new SimpleModelWithDictionary()
{
Dic = new Dictionary<string, string>()
{
{ "key1", "val_changed" }
}
};

var diff = DiffBuilder.Build(source, patched);

Assert.True(JObject.DeepEquals(
JObject.Parse("{Dic:{\"key1\": \"val_changed\"}}"),
diff));
}
}
}

0 comments on commit 92591e1

Please sign in to comment.