Skip to content

Commit

Permalink
WIP: Reject pathes that use the wrong list indexing syntax [i] inst…
Browse files Browse the repository at this point in the history
…ead of `/i`
  • Loading branch information
Konstantin committed Oct 21, 2024
1 parent be8d3fd commit b5529bf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
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]");
}
}

0 comments on commit b5529bf

Please sign in to comment.