diff --git a/SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs b/SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs index b01fd62..0dde7d2 100644 --- a/SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs +++ b/SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs @@ -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; @@ -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() + { + Options = new() { Converters = { new IntJsonConverter() } }, + }; + + patch.Operations.Add(new Operation("replace", "/IntegerValue", null, null)); + + // Act + patch.ApplyTo(model); + + // Assert + Assert.Equal(999, model.IntegerValue); + } + + class IntJsonConverter : JsonConverter + { + 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() { diff --git a/SystemTextJsonPatch/Internal/ConversionResultProvider.cs b/SystemTextJsonPatch/Internal/ConversionResultProvider.cs index 47c235b..4943743 100644 --- a/SystemTextJsonPatch/Internal/ConversionResultProvider.cs +++ b/SystemTextJsonPatch/Internal/ConversionResultProvider.cs @@ -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))