diff --git a/src/Hyperbee.Json/Patch/JsonPatch.cs b/src/Hyperbee.Json/Patch/JsonPatch.cs index eb77522..b62dbd3 100644 --- a/src/Hyperbee.Json/Patch/JsonPatch.cs +++ b/src/Hyperbee.Json/Patch/JsonPatch.cs @@ -416,12 +416,13 @@ private static void ThrowLocationDoesNotExist( string path, JsonNode node ) private static JsonNode PatchValue( PatchOperation patch ) { - if ( patch.Value is null ) - throw new JsonPatchException( "The 'value' property was missing." ); - - return (patch.Value is JsonNode node) - ? (node.Parent != null ? node.DeepClone() : node) - : JsonValue.Create( patch.Value ); + return patch.Value switch + { + null => null, + JsonNode node when node.Parent != null => node.DeepClone(), + JsonNode node => node, + _ => JsonValue.Create( patch.Value ) + }; } public IEnumerator GetEnumerator() diff --git a/test/Hyperbee.Json.Tests/Patch/JsonPatchTests.cs b/test/Hyperbee.Json.Tests/Patch/JsonPatchTests.cs index f1e9ff5..2e496a7 100644 --- a/test/Hyperbee.Json.Tests/Patch/JsonPatchTests.cs +++ b/test/Hyperbee.Json.Tests/Patch/JsonPatchTests.cs @@ -1,5 +1,4 @@ -using System; -using System.Text.Json.Nodes; +using System.Text.Json.Nodes; using Hyperbee.Json.Patch; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -885,6 +884,25 @@ public void Replace_WhenValueProperty() Assert.AreEqual( "Mark", source!["first"]!.GetValue() ); } + [TestMethod] + public void Replace_WhenValueProperty_WithNull() + { + var source = JsonNode.Parse( + """ + { + "first": "John" + } + """ ); + + var patch = new JsonPatch( + new PatchOperation( PatchOperationType.Replace, "/first", null, null ) + ); + + patch.Apply( source ); + + Assert.AreEqual( null, source!["first"] ); + } + [TestMethod] public void Replace_WhenValueObject() {