Skip to content
Merged
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
2 changes: 2 additions & 0 deletions SystemTextJsonPatch.Tests/Internal/ParsedPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class ParsedPathTests
[InlineData("foo/bar~1baz", new string[] { "foo", "bar/baz" })]
[InlineData("foo/bar~0/~0/~1~1/~0~0/baz", new string[] { "foo", "bar~", "~", "//", "~~", "baz" })]
[InlineData("~0~1foo", new string[] { "~/foo" })]
[InlineData("/foo/", new string[] { "foo", "" })]
[InlineData("/foo//bar", new string[] { "foo", "", "bar" })]
public void ParsingValidPathShouldSucceed(string path, string[] expected)
{
// Arrange & Act
Expand Down
119 changes: 119 additions & 0 deletions SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,125 @@ public void ApplyToArrayAdd()
Assert.Equal("foo@baz.com", model.CustomData["Emails"][1].GetValue<string>());
}

[Fact]
public void AddAtExistingArrayIndexInsertsBeforeElement()
{
var node = JsonSerializer.Deserialize<JsonArray>("[\"foo\",\"sil\"]");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/1"", ""value"": ""bar"" } ]");

patch.ApplyTo(node);

Assert.Equal("[\"foo\",\"bar\",\"sil\"]", node.ToJsonString());
}

[Fact]
public void AddAtArrayIndexZeroInsertsBeforeElement()
{
var node = JsonSerializer.Deserialize<JsonArray>("[\"foo\",\"sil\"]");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/0"", ""value"": ""bar"" } ]");

patch.ApplyTo(node);

Assert.Equal("[\"bar\",\"foo\",\"sil\"]", node.ToJsonString());
}

[Fact]
public void AddArrayValueAtExistingIndexKeepsItAsOneElement()
{
var node = JsonSerializer.Deserialize<JsonArray>("[\"foo\",\"sil\"]");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/1"", ""value"": [""bar"", ""baz""] } ]");

patch.ApplyTo(node);

Assert.Equal("[\"foo\",[\"bar\",\"baz\"],\"sil\"]", node.ToJsonString());
}

[Fact]
public void AddAtArrayIndexFromRfcExampleInsertsBeforeElement()
{
var node = JsonSerializer.Deserialize<JsonObject>("{\"foo\":[\"bar\",\"baz\"]}");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/foo/1"", ""value"": ""qux"" } ]");

patch.ApplyTo(node);

Assert.Equal("{\"foo\":[\"bar\",\"qux\",\"baz\"]}", node.ToJsonString());
}

[Fact]
public void AddAtArrayLengthAppends()
{
var node = JsonSerializer.Deserialize<JsonArray>("[\"foo\",\"sil\"]");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/2"", ""value"": ""bar"" } ]");

patch.ApplyTo(node);

Assert.Equal("[\"foo\",\"sil\",\"bar\"]", node.ToJsonString());
}

[Fact]
public void AddBeyondArrayLengthFails()
{
var node = JsonSerializer.Deserialize<JsonArray>("[\"foo\",\"sil\"]");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/3"", ""value"": ""bar"" } ]");

Assert.Throws<ArgumentOutOfRangeException>(() => patch.ApplyTo(node));
}

[Fact]
public void AddTrailingEmptyPointerTokenTargetsEmptyMember()
{
var node = JsonSerializer.Deserialize<JsonObject>("{\"foo\":{}}");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/foo/"", ""value"": 1 } ]");

patch.ApplyTo(node);

Assert.Equal("{\"foo\":{\"\":1}}", node.ToJsonString());
}

[Fact]
public void AddIgnoresUnrecognizedOperationMember()
{
var node = JsonNode.Parse("{\"foo\":\"bar\"}");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/baz"", ""value"": ""qux"", ""xyz"": 123 } ]", new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

patch.ApplyTo(node);

Assert.Equal("{\"foo\":\"bar\",\"baz\":\"qux\"}", node.ToJsonString());
}

[Fact]
public void TestIgnoresUnrecognizedOperationMember()
{
var node = JsonNode.Parse("{\"foo\":1}");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""test"", ""path"": ""/foo"", ""value"": 1, ""spurious"": 1 } ]");

patch.ApplyTo(node);

Assert.Equal("{\"foo\":1}", node.ToJsonString());
}

[Fact]
public void AddIgnoresNestedUnrecognizedOperationMember()
{
var node = JsonNode.Parse("{\"foo\":\"bar\"}");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""add"", ""path"": ""/baz"", ""value"": ""qux"", ""extra"": { ""nested"": [1, { ""deep"": true }] } } ]");

patch.ApplyTo(node);

Assert.Equal("{\"foo\":\"bar\",\"baz\":\"qux\"}", node.ToJsonString());
}

[Fact]
public void ReplaceExistingNullJsonProperty()
{
var node = JsonSerializer.Deserialize<JsonObject>("{\"foo\":null}");
var patch = JsonSerializer.Deserialize<JsonPatchDocument>(@"[ { ""op"": ""replace"", ""path"": ""/foo"", ""value"": ""truthy"" } ]");

patch.ApplyTo(node);

Assert.Equal("{\"foo\":\"truthy\"}", node.ToJsonString());
}

[Fact]
public void ReplaceJsonNodeWithNewJson()
{
Expand Down
14 changes: 12 additions & 2 deletions SystemTextJsonPatch/Converters/JsonPatchDocumentConverterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ private static List<TOperation> ParseOperationCaseSensitive(ref Utf8JsonReader r
}
else
{
ThrowJsonPatchException();
reader.Skip();
}
}

if (op == null || path == null)
{
ThrowJsonPatchException();
}

var operation = new TOperation
{
Op = op,
Expand Down Expand Up @@ -171,10 +176,15 @@ private static List<TOperation> ParseOperationCaseInSensitive(ref Utf8JsonReader
}
else
{
ExceptionHelper.ThrowJsonPatchException(Resources.InvalidJsonPatchDocument);
reader.Skip();
}
}

if (op == null || path == null)
{
ThrowJsonPatchException();
}

var operation = new TOperation
{
Op = op,
Expand Down
2 changes: 1 addition & 1 deletion SystemTextJsonPatch/Internal/JSonObjectAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public bool TryReplace(object target, string segment, JsonSerializerOptions opti

var propertyName = FindPropertyName(obj, segment, options);

if (obj[propertyName] == null)
if (!obj.TryGetPropertyValue(propertyName, out _))
{
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment);
return false;
Expand Down
6 changes: 5 additions & 1 deletion SystemTextJsonPatch/Internal/ParsedPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ private static string[] ParsePath(string path)
strings.Add(sb.ToString());
sb.Length = 0;
}
else if (i > 0 && path[i - 1] == '/')
{
strings.Add(string.Empty);
}
}
else if (c == '~')
{
Expand Down Expand Up @@ -81,7 +85,7 @@ private static string[] ParsePath(string path)
}
}

if (sb.Length > 0)
if (sb.Length > 0 || path.EndsWith("/", StringComparison.Ordinal))
{
strings.Add(sb.ToString());
}
Expand Down
9 changes: 8 additions & 1 deletion SystemTextJsonPatch/Internal/PocoAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ public bool TryAdd(object target, string segment, JsonSerializerOptions options,
return false;
}

jsonProperty.SetValue(target, convertedValue);
if (jsonProperty is JsonArrayProxy jsonArrayProxy)
{
jsonArrayProxy.AddValue(convertedValue);
}
else
{
jsonProperty.SetValue(target, convertedValue);
}

errorMessage = null;
return true;
Expand Down
14 changes: 14 additions & 0 deletions SystemTextJsonPatch/Internal/Proxies/JsonArrayProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public void SetValue(object target, object? convertedValue)
}
}

public void AddValue(object? convertedValue)
{
var value = convertedValue == null ? null : JsonSerializer.SerializeToNode(convertedValue);

if (_propertyName == Consts.LastElement)
{
_jsonArray.Add(value);
}
else
{
_jsonArray.Insert(this.PropIndex, value);
}
}

public void RemoveValue(object target)
{
if (_propertyName == Consts.LastElement)
Expand Down
Loading