Skip to content

Commit ad96279

Browse files
committed
Improved json diff generation, we do not move into arrays to sort object properties nested in there making diffs hard to read when objects in arrays are involved
(cherry picked from commit 9353c1a)
1 parent c115278 commit ad96279

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/Tests/Tests.Core/Extensions/DiffExtensions.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,28 @@ namespace Tests.Core.Extensions
1010
{
1111
public static class DiffExtensions
1212
{
13-
public static void DeepSort(this JObject jObj)
13+
public static void DeepSort(this JToken token)
1414
{
15-
if (jObj == null) return;
15+
if (token == null) return;
1616

17-
var props = jObj.Properties().ToList();
18-
foreach (var prop in props) prop.Remove();
17+
if (token is JObject jObj)
18+
{
19+
var props = jObj.Properties().ToList();
20+
foreach (var prop in props) prop.Remove();
1921

20-
foreach (var prop in props.OrderBy(p => p.Name))
22+
foreach (var prop in props.OrderBy(p => p.Name))
23+
{
24+
jObj.Add(prop);
25+
prop.Value?.DeepSort();
26+
}
27+
}
28+
if (token is JArray jArray)
2129
{
22-
jObj.Add(prop);
23-
var o = prop.Value as JObject;
24-
o?.DeepSort();
30+
foreach (var v in jArray.Values())
31+
v?.DeepSort();
2532
}
33+
if (token is JProperty jProp)
34+
jProp.Value?.DeepSort();
2635
}
2736

2837
public static string CreateCharacterDifference(this string expected, string actual, string message = null)
@@ -59,7 +68,6 @@ private static string CreateDiff(string expected, string actual, string message,
5968
var hasChanges = result.Lines.Any(l => l.Type != ChangeType.Unchanged);
6069
if (!hasChanges) return string.Empty;
6170

62-
6371
return result.Lines.Aggregate(new StringBuilder().AppendLine(message), (sb, line) =>
6472
{
6573
if (line.Type == ChangeType.Inserted)

0 commit comments

Comments
 (0)