Skip to content

Commit

Permalink
fix for issue 88 (#89)
Browse files Browse the repository at this point in the history
Co-authored-by: Reece Bradley <[email protected]>
  • Loading branch information
reecebradley and Reece Bradley authored Dec 3, 2024
1 parent b69d3b0 commit ac4f06d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/Hyperbee.Json/Patch/JsonPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PatchOperation> GetEnumerator()
Expand Down
22 changes: 20 additions & 2 deletions test/Hyperbee.Json.Tests/Patch/JsonPatchTests.cs
Original file line number Diff line number Diff line change
@@ -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;

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

[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()
{
Expand Down

0 comments on commit ac4f06d

Please sign in to comment.