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..8b7d603c 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..c2b6ce44 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,8 +16,16 @@ 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\"}]"); } + + [Fact] + public void When_diffing_null_objects_then_no_errors_should_be_thrown() + { + var result = KubernetesJsonDiffer.DiffObjects(null, null); + + Assert.NotNull(result); + } }