Skip to content

Commit 00d67cd

Browse files
authoredSep 19, 2023
feat: use updated/faster JsonDiffPatch library (#601)
The main motivation behind this change is to remove the deprecated transitive dependency `NetCoreApp 1.1` from `JsonDiffPatch`. This is causing validation issues in built images that are run through Aqua Trivy vulnerability scanner. A nice side-effect is that the diff should be much faster as it uses `System.Text.Json` instead of `Newtonsoft`. Functionally, there should be no impact as both libraries adhere to `RFC 6902`.
1 parent 7812f42 commit 00d67cd

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed
 

‎src/KubeOps/KubeOps.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
<ItemGroup>
2222
<PackageReference Include="CompareNETObjects" Version="4.79.0" />
23-
<PackageReference Include="JsonDiffPatch" Version="2.0.61" />
2423
<PackageReference Include="Localtunnel" Version="1.0.5" />
2524
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.2" />
2625
<PackageReference Include="McMaster.Extensions.Hosting.CommandLine" Version="4.0.2" />
@@ -29,6 +28,7 @@
2928
<PackageReference Include="prometheus-net.AspNetCore.HealthChecks" Version="7.0.0" />
3029
<PackageReference Include="SimpleBase" Version="4.0.0" />
3130
<PackageReference Include="System.Reactive" Version="5.0.0" />
31+
<PackageReference Include="SystemTextJson.JsonDiffPatch" Version="1.3.1" />
3232
</ItemGroup>
3333

3434
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
using JsonDiffPatch;
1+
using System.Text.Json.JsonDiffPatch;
2+
using System.Text.Json.JsonDiffPatch.Diffs.Formatters;
3+
using System.Text.Json.Nodes;
24
using k8s;
3-
using Newtonsoft.Json;
4-
using Newtonsoft.Json.Linq;
55

66
namespace KubeOps.Operator.Webhooks;
77

88
internal static class KubernetesJsonDiffer
99
{
10-
private static readonly JsonDiffer JsonDiffer = new();
10+
private static readonly JsonPatchDeltaFormatter Formatter = new();
1111

12-
public static PatchDocument DiffObjects(object? from, object? to)
12+
public static JsonNode DiffObjects(object? from, object? to)
1313
{
1414
var fromToken = GetJToken(from);
1515
var toToken = GetJToken(to);
1616

17-
return JsonDiffer.Diff(fromToken, toToken, false);
17+
return fromToken.Diff(toToken, Formatter)!;
1818
}
1919

20-
private static JToken GetJToken(object? o)
20+
private static JsonNode? GetJToken(object? o)
2121
{
2222
// Use the K8s Serializer to ensure we match their naming conventions
2323
// (and handle object conversions correctly).
2424
var json = KubernetesJson.Serialize(o);
25-
return JToken.ReadFrom(new JsonTextReader(new StringReader(json)));
25+
return JsonNode.Parse(json);
2626
}
2727
}

‎tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using FluentAssertions;
22
using k8s.Models;
33
using KubeOps.Operator.Webhooks;
4-
using Newtonsoft.Json;
54
using Xunit;
65

76
namespace KubeOps.Test.Operator.Webhook;
@@ -17,8 +16,16 @@ public void When_diffing_objects_then_kubernetes_naming_conventions_should_be_us
1716
var result = KubernetesJsonDiffer.DiffObjects(left, right);
1817

1918
// Should be all lowercase.
20-
result.ToString(Formatting.None)
19+
result.ToJsonString()
2120
.Should()
2221
.Be("[{\"op\":\"replace\",\"path\":\"/status/reason\",\"value\":\"bar\"}]");
2322
}
23+
24+
[Fact]
25+
public void When_diffing_null_objects_then_no_errors_should_be_thrown()
26+
{
27+
var result = KubernetesJsonDiffer.DiffObjects(null, null);
28+
29+
Assert.NotNull(result);
30+
}
2431
}

0 commit comments

Comments
 (0)
Please sign in to comment.