From 04dc130b39ae53601ef1c170eecc58e026e6c4d0 Mon Sep 17 00:00:00 2001 From: Akshay Kumar Date: Thu, 20 Nov 2025 21:22:44 +0530 Subject: [PATCH 1/2] Ignore From when serializing a JsonPatchDocument for add, remove, replace and test operations --- .../src/Operations/OperationBase.cs | 12 ++++------ .../test/JsonPatchDocumentTest.cs | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Features/JsonPatch.SystemTextJson/src/Operations/OperationBase.cs b/src/Features/JsonPatch.SystemTextJson/src/Operations/OperationBase.cs index adfa72767aa0..622d94a63e16 100644 --- a/src/Features/JsonPatch.SystemTextJson/src/Operations/OperationBase.cs +++ b/src/Features/JsonPatch.SystemTextJson/src/Operations/OperationBase.cs @@ -43,8 +43,10 @@ public string op } } - [JsonPropertyName(nameof(from))] - public string from { get; set; } +#nullable enable + [JsonPropertyName(nameof(from)), JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? from { get; set; } +#nullable restore public OperationBase() { @@ -59,10 +61,4 @@ public OperationBase(string op, string path, string from) this.path = path; this.from = from; } - - public bool ShouldSerializeFrom() - { - return (OperationType == OperationType.Move - || OperationType == OperationType.Copy); - } } diff --git a/src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs b/src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs index 9e7c6073e036..9888cf17ae16 100644 --- a/src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs +++ b/src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs @@ -255,4 +255,26 @@ private string GeneratePatchDocumentJson(SimpleObject toAdd, JsonSerializerOptio return JsonSerializer.Serialize>(document, jsonSerializerOptions); } + + [Fact] + public void Serialization_ShouldExcludeFrom_WhenNullAndNotMoveOrCopy() + { + // Arrange + JsonPatchDocument patchDocument = new(); + patchDocument.Add("/a/b/c", "foo"); + patchDocument.Remove("/x/y/z"); + patchDocument.Replace("/d/e", "bar"); + patchDocument.Test("/f/e", "t1"); + + var json = JsonSerializer.Serialize(patchDocument); + + // Assert + var expectedJson = """ + [{"value":"foo","path":"/a/b/c","op":"add"},{"value":null,"path":"/x/y/z","op":"remove"}, + { "value":"bar","path":"/d/e","op":"replace"},{ "value":"t1","path":"/f/e","op":"test"}] + """; + + // Act + Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expectedJson), JsonNode.Parse(json))); + } } From 0cf2b7d927587fbb1d3879389af3283c6a9dc034 Mon Sep 17 00:00:00 2001 From: Akshay Kumar Date: Thu, 20 Nov 2025 22:13:04 +0530 Subject: [PATCH 2/2] updated public api file --- .../JsonPatch.SystemTextJson/src/PublicAPI.Shipped.txt | 2 -- .../JsonPatch.SystemTextJson/src/PublicAPI.Unshipped.txt | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Shipped.txt b/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Shipped.txt index 003515cec17e..b09124403908 100644 --- a/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Shipped.txt +++ b/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Shipped.txt @@ -74,7 +74,6 @@ ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation.Operation(string op, string path, string from, object value) -> void ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation.Operation(string op, string path, string from) -> void ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.from.get -> string -~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.from.set -> void ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.op.get -> string ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.op.set -> void ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.OperationBase(string op, string path, string from) -> void @@ -95,7 +94,6 @@ Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation.Opera Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.OperationBase() -> void Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.OperationType.get -> Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationType -Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.ShouldSerializeFrom() -> bool Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationType Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationType.Add = 0 -> Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationType Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationType.Copy = 4 -> Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationType diff --git a/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Unshipped.txt b/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..88c61416b7cf 100644 --- a/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Unshipped.txt +++ b/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Unshipped.txt @@ -1 +1,4 @@ #nullable enable + +Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.from.get -> string? +Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationBase.from.set -> void