Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using SystemTextJsonPatch.Exceptions;
using SystemTextJsonPatch.Operations;
using Xunit;
Expand Down Expand Up @@ -328,6 +330,46 @@ public void ApplyToModelReplaceNull()
Assert.Null(model.CustomData["Email"]);
}

[Fact]
public void ApplyToModelReplaceNonReferenceWithNull()
{
// Arrange
var model = new SimpleObject { IntegerValue = 123 };
var patch = new JsonPatchDocument<SimpleObject>()
{
Options = new() { Converters = { new IntJsonConverter() } },
};

patch.Operations.Add(new Operation<SimpleObject>("replace", "/IntegerValue", null, null));

// Act
patch.ApplyTo(model);

// Assert
Assert.Equal(999, model.IntegerValue);
}

class IntJsonConverter : JsonConverter<int>
{
public override bool HandleNull { get; } = true;

public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options
)
{
if (reader.TokenType == JsonTokenType.Null)
{
return 999;
}

return reader.GetInt32();
}

public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value);
}
}

[Fact]
public void ApplyToModelReplaceWithIgnoringCasing()
{
Expand Down
7 changes: 5 additions & 2 deletions SystemTextJsonPatch/Internal/ConversionResultProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ internal static bool TryConvertTo(object? value, Type typeToConvertTo, JsonSeria

if (value == null)
{
convertedValue = null;
return IsNullableType(typeToConvertTo);
if (IsNullableType(typeToConvertTo))
{
convertedValue = null;
return true;
}
}

if (typeToConvertTo.IsInstanceOfType(value))
Expand Down
Loading