Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation<TModel>.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
Expand Down Expand Up @@ -97,8 +96,8 @@ Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.OperationType.Test = 5
~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation<TModel>.Apply(TModel objectToApplyTo, Microsoft.AspNetCore.JsonPatch.SystemTextJson.Adapters.IObjectAdapter adapter) -> void
~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation<TModel>.Operation(string op, string path, string from) -> void
~Microsoft.AspNetCore.JsonPatch.SystemTextJson.Operations.Operation<TModel>.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -255,4 +256,26 @@ private string GeneratePatchDocumentJson(SimpleObject toAdd, JsonSerializerOptio

return JsonSerializer.Serialize<JsonPatchDocument<SimpleObject>>(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)));
}
}
Loading