Skip to content

Commit ac4f06d

Browse files
reecebradleyReece Bradley
andauthored
fix for issue 88 (#89)
Co-authored-by: Reece Bradley <[email protected]>
1 parent b69d3b0 commit ac4f06d

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/Hyperbee.Json/Patch/JsonPatch.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,13 @@ private static void ThrowLocationDoesNotExist( string path, JsonNode node )
416416

417417
private static JsonNode PatchValue( PatchOperation patch )
418418
{
419-
if ( patch.Value is null )
420-
throw new JsonPatchException( "The 'value' property was missing." );
421-
422-
return (patch.Value is JsonNode node)
423-
? (node.Parent != null ? node.DeepClone() : node)
424-
: JsonValue.Create( patch.Value );
419+
return patch.Value switch
420+
{
421+
null => null,
422+
JsonNode node when node.Parent != null => node.DeepClone(),
423+
JsonNode node => node,
424+
_ => JsonValue.Create( patch.Value )
425+
};
425426
}
426427

427428
public IEnumerator<PatchOperation> GetEnumerator()

test/Hyperbee.Json.Tests/Patch/JsonPatchTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Text.Json.Nodes;
1+
using System.Text.Json.Nodes;
32
using Hyperbee.Json.Patch;
43
using Microsoft.VisualStudio.TestTools.UnitTesting;
54

@@ -885,6 +884,25 @@ public void Replace_WhenValueProperty()
885884
Assert.AreEqual( "Mark", source!["first"]!.GetValue<string>() );
886885
}
887886

887+
[TestMethod]
888+
public void Replace_WhenValueProperty_WithNull()
889+
{
890+
var source = JsonNode.Parse(
891+
"""
892+
{
893+
"first": "John"
894+
}
895+
""" );
896+
897+
var patch = new JsonPatch(
898+
new PatchOperation( PatchOperationType.Replace, "/first", null, null )
899+
);
900+
901+
patch.Apply( source );
902+
903+
Assert.AreEqual( null, source!["first"] );
904+
}
905+
888906
[TestMethod]
889907
public void Replace_WhenValueObject()
890908
{

0 commit comments

Comments
 (0)