From 2f298228172b343cc4fd0bd08507d7a1261a5807 Mon Sep 17 00:00:00 2001 From: Robert Coltheart Date: Tue, 12 Sep 2023 19:46:47 +1000 Subject: [PATCH 1/3] Use updated JsonDiffPatch library --- src/KubeOps/KubeOps.csproj | 2 +- .../Operator/Webhooks/KubernetesJsonDiffer.cs | 16 ++++++++-------- .../Webhook/KubernetesJsonDiffer.Test.cs | 3 +-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/KubeOps/KubeOps.csproj b/src/KubeOps/KubeOps.csproj index a607f9b1..ed9bb45f 100644 --- a/src/KubeOps/KubeOps.csproj +++ b/src/KubeOps/KubeOps.csproj @@ -20,7 +20,6 @@ - @@ -29,6 +28,7 @@ + diff --git a/src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs b/src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs index cba8e825..716435ed 100644 --- a/src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs +++ b/src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs @@ -1,27 +1,27 @@ -using JsonDiffPatch; +using System.Text.Json.JsonDiffPatch; +using System.Text.Json.JsonDiffPatch.Diffs.Formatters; +using System.Text.Json.Nodes; using k8s; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace KubeOps.Operator.Webhooks; internal static class KubernetesJsonDiffer { - private static readonly JsonDiffer JsonDiffer = new(); + private static readonly JsonPatchDeltaFormatter Formatter = new(); - public static PatchDocument DiffObjects(object? from, object? to) + public static JsonNode? DiffObjects(object? from, object? to) { var fromToken = GetJToken(from); var toToken = GetJToken(to); - return JsonDiffer.Diff(fromToken, toToken, false); + return fromToken.Diff(toToken, Formatter); } - private static JToken GetJToken(object? o) + private static JsonNode? GetJToken(object? o) { // Use the K8s Serializer to ensure we match their naming conventions // (and handle object conversions correctly). var json = KubernetesJson.Serialize(o); - return JToken.ReadFrom(new JsonTextReader(new StringReader(json))); + return JsonNode.Parse(json); } } diff --git a/tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs b/tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs index d546619a..ac346a73 100644 --- a/tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs +++ b/tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs @@ -1,7 +1,6 @@ using FluentAssertions; using k8s.Models; using KubeOps.Operator.Webhooks; -using Newtonsoft.Json; using Xunit; namespace KubeOps.Test.Operator.Webhook; @@ -17,7 +16,7 @@ public void When_diffing_objects_then_kubernetes_naming_conventions_should_be_us var result = KubernetesJsonDiffer.DiffObjects(left, right); // Should be all lowercase. - result.ToString(Formatting.None) + result.ToJsonString() .Should() .Be("[{\"op\":\"replace\",\"path\":\"/status/reason\",\"value\":\"bar\"}]"); } From 667cc970ca8ce1b6b1db79d7d8446329b8986e74 Mon Sep 17 00:00:00 2001 From: Robert Coltheart <13191652+robertcoltheart@users.noreply.github.com> Date: Thu, 14 Sep 2023 10:21:52 +1000 Subject: [PATCH 2/3] Update KubernetesJsonDiffer.cs --- src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs b/src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs index 716435ed..8b7d603c 100644 --- a/src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs +++ b/src/KubeOps/Operator/Webhooks/KubernetesJsonDiffer.cs @@ -9,12 +9,12 @@ internal static class KubernetesJsonDiffer { private static readonly JsonPatchDeltaFormatter Formatter = new(); - public static JsonNode? DiffObjects(object? from, object? to) + public static JsonNode DiffObjects(object? from, object? to) { var fromToken = GetJToken(from); var toToken = GetJToken(to); - return fromToken.Diff(toToken, Formatter); + return fromToken.Diff(toToken, Formatter)!; } private static JsonNode? GetJToken(object? o) From bccc731b46ca8018103dd8602d057f4f5556542c Mon Sep 17 00:00:00 2001 From: Robert Coltheart <13191652+robertcoltheart@users.noreply.github.com> Date: Thu, 14 Sep 2023 10:22:19 +1000 Subject: [PATCH 3/3] Update KubernetesJsonDiffer.Test.cs --- .../Operator/Webhook/KubernetesJsonDiffer.Test.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs b/tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs index ac346a73..c2b6ce44 100644 --- a/tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs +++ b/tests/KubeOps.Test/Operator/Webhook/KubernetesJsonDiffer.Test.cs @@ -20,4 +20,12 @@ public void When_diffing_objects_then_kubernetes_naming_conventions_should_be_us .Should() .Be("[{\"op\":\"replace\",\"path\":\"/status/reason\",\"value\":\"bar\"}]"); } + + [Fact] + public void When_diffing_null_objects_then_no_errors_should_be_thrown() + { + var result = KubernetesJsonDiffer.DiffObjects(null, null); + + Assert.NotNull(result); + } }