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/src/PublicAPI.Unshipped.txt b/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Unshipped.txt index e2421fc8af3c..73f3c1904e71 100644 --- a/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Unshipped.txt +++ b/src/Features/JsonPatch.SystemTextJson/src/PublicAPI.Unshipped.txt @@ -14,7 +14,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 @@ -97,8 +96,8 @@ Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationType.Test = 5 ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation.Apply(TModel objectToApplyTo, Microsoft.AspNetCore.JsonPatch.SystemTextJson.Adapters.IObjectAdapter adapter) -> void ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation.Operation(string op, string path, string from) -> void ~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation.Operation(string op, string path, string from, object value) -> 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.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 diff --git a/src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs b/src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs index 9e7c6073e036..2f91bed85eaf 100644 --- a/src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs +++ b/src/Features/JsonPatch.SystemTextJson/test/JsonPatchDocumentTest.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Text.Json; +using System.Text.Json.Nodes; using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Converters; using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Exceptions; using Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations; @@ -255,4 +256,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))); + } +} \ No newline at end of file