Skip to content
Draft
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 @@ -258,6 +258,10 @@ in TModel model
}

var originalError = applyError!;
{
// if the operation looks like a list operation with [<index>] instead of /<index>
// then log a warning
}
var extensionDataKey = GetPathRelativeToExtensionData(originalOperation.path);
if (extensionDataAlreadyExists)
{
Expand Down
11 changes: 11 additions & 0 deletions JsonPatchDocumentExtensionDataAdapter/UnitTest/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ internal class MyClassWithNesting
public MyClass? MyModel { get; set; }
}

internal class MyClassWithAList
{
public int? Foo { get; set; }
public string? Bar { get; set; }

public List<int>? SomeList { get; set; }

[System.Text.Json.Serialization.JsonExtensionData]
public IDictionary<string, object>? MyExtensionData { get; set; }
}

/// <summary>
/// similar to <see cref="MyClass"/> but with JsonPropertyName attributes
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions JsonPatchDocumentExtensionDataAdapter/UnitTest/SimpleTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentAssertions;
using JsonExtensionDataPatchDocumentAdapter;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Microsoft.AspNetCore.JsonPatch.Operations;

namespace UnitTest;
Expand Down Expand Up @@ -126,4 +127,29 @@ bool invokeAsExtensionMethod
.Be("def");
}
}

[TestMethod]
public void TestPatchingExtensionData_Recognizes_Invalid_Path()
{
var myEntity = new MyClassWithAList();
var myPatch = new JsonPatchDocument<MyClassWithAList>();
myPatch.Add(x => x.Foo, 42);
myPatch.Add(x => x.Bar, "fgh");
myPatch.Operations.Add(
new Operation<MyClassWithAList>
{
path = "/SomeList[0]",
op = "add",
value = 17,
}
);
var patchingOriginal = () => myPatch.ApplyTo(myEntity);
patchingOriginal.Should().Throw<JsonPatchException>();

var adaptedPatch = new JsonPatchDocumentExtensionDataAdapter<MyClassWithAList>(x =>
x.MyExtensionData
).TransformDocument(myPatch, in myEntity);
adaptedPatch.ApplyTo(myEntity);
myEntity.MyExtensionData.Should().NotContainKey("SomeList[0]");
}
}