diff --git a/SystemTextJsonPatch.Tests/Internal/ParsedPathTests.cs b/SystemTextJsonPatch.Tests/Internal/ParsedPathTests.cs index 8e67713..0858ee5 100644 --- a/SystemTextJsonPatch.Tests/Internal/ParsedPathTests.cs +++ b/SystemTextJsonPatch.Tests/Internal/ParsedPathTests.cs @@ -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 diff --git a/SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs b/SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs index cc40fe6..6449a35 100644 --- a/SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs +++ b/SystemTextJsonPatch.Tests/JsonPatchDocumentJsonObjectTest.cs @@ -28,6 +28,125 @@ public void ApplyToArrayAdd() Assert.Equal("foo@baz.com", model.CustomData["Emails"][1].GetValue()); } + [Fact] + public void AddAtExistingArrayIndexInsertsBeforeElement() + { + var node = JsonSerializer.Deserialize("[\"foo\",\"sil\"]"); + var patch = JsonSerializer.Deserialize(@"[ { ""op"": ""add"", ""path"": ""/1"", ""value"": ""bar"" } ]"); + + patch.ApplyTo(node); + + Assert.Equal("[\"foo\",\"bar\",\"sil\"]", node.ToJsonString()); + } + + [Fact] + public void AddAtArrayIndexZeroInsertsBeforeElement() + { + var node = JsonSerializer.Deserialize("[\"foo\",\"sil\"]"); + var patch = JsonSerializer.Deserialize(@"[ { ""op"": ""add"", ""path"": ""/0"", ""value"": ""bar"" } ]"); + + patch.ApplyTo(node); + + Assert.Equal("[\"bar\",\"foo\",\"sil\"]", node.ToJsonString()); + } + + [Fact] + public void AddArrayValueAtExistingIndexKeepsItAsOneElement() + { + var node = JsonSerializer.Deserialize("[\"foo\",\"sil\"]"); + var patch = JsonSerializer.Deserialize(@"[ { ""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("{\"foo\":[\"bar\",\"baz\"]}"); + var patch = JsonSerializer.Deserialize(@"[ { ""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("[\"foo\",\"sil\"]"); + var patch = JsonSerializer.Deserialize(@"[ { ""op"": ""add"", ""path"": ""/2"", ""value"": ""bar"" } ]"); + + patch.ApplyTo(node); + + Assert.Equal("[\"foo\",\"sil\",\"bar\"]", node.ToJsonString()); + } + + [Fact] + public void AddBeyondArrayLengthFails() + { + var node = JsonSerializer.Deserialize("[\"foo\",\"sil\"]"); + var patch = JsonSerializer.Deserialize(@"[ { ""op"": ""add"", ""path"": ""/3"", ""value"": ""bar"" } ]"); + + Assert.Throws(() => patch.ApplyTo(node)); + } + + [Fact] + public void AddTrailingEmptyPointerTokenTargetsEmptyMember() + { + var node = JsonSerializer.Deserialize("{\"foo\":{}}"); + var patch = JsonSerializer.Deserialize(@"[ { ""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(@"[ { ""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(@"[ { ""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(@"[ { ""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("{\"foo\":null}"); + var patch = JsonSerializer.Deserialize(@"[ { ""op"": ""replace"", ""path"": ""/foo"", ""value"": ""truthy"" } ]"); + + patch.ApplyTo(node); + + Assert.Equal("{\"foo\":\"truthy\"}", node.ToJsonString()); + } + [Fact] public void ReplaceJsonNodeWithNewJson() { diff --git a/SystemTextJsonPatch/Converters/JsonPatchDocumentConverterBase.cs b/SystemTextJsonPatch/Converters/JsonPatchDocumentConverterBase.cs index ab5bf82..fb9cc52 100644 --- a/SystemTextJsonPatch/Converters/JsonPatchDocumentConverterBase.cs +++ b/SystemTextJsonPatch/Converters/JsonPatchDocumentConverterBase.cs @@ -85,10 +85,15 @@ private static List ParseOperationCaseSensitive(ref Utf8JsonReader r } else { - ThrowJsonPatchException(); + reader.Skip(); } } + if (op == null || path == null) + { + ThrowJsonPatchException(); + } + var operation = new TOperation { Op = op, @@ -171,10 +176,15 @@ private static List ParseOperationCaseInSensitive(ref Utf8JsonReader } else { - ExceptionHelper.ThrowJsonPatchException(Resources.InvalidJsonPatchDocument); + reader.Skip(); } } + if (op == null || path == null) + { + ThrowJsonPatchException(); + } + var operation = new TOperation { Op = op, diff --git a/SystemTextJsonPatch/Internal/JSonObjectAdapter.cs b/SystemTextJsonPatch/Internal/JSonObjectAdapter.cs index 92c8f99..ee8b568 100644 --- a/SystemTextJsonPatch/Internal/JSonObjectAdapter.cs +++ b/SystemTextJsonPatch/Internal/JSonObjectAdapter.cs @@ -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; diff --git a/SystemTextJsonPatch/Internal/ParsedPath.cs b/SystemTextJsonPatch/Internal/ParsedPath.cs index e379b0b..a1cd1bd 100644 --- a/SystemTextJsonPatch/Internal/ParsedPath.cs +++ b/SystemTextJsonPatch/Internal/ParsedPath.cs @@ -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 == '~') { @@ -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()); } diff --git a/SystemTextJsonPatch/Internal/PocoAdapter.cs b/SystemTextJsonPatch/Internal/PocoAdapter.cs index f17fd77..123c0c4 100644 --- a/SystemTextJsonPatch/Internal/PocoAdapter.cs +++ b/SystemTextJsonPatch/Internal/PocoAdapter.cs @@ -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; diff --git a/SystemTextJsonPatch/Internal/Proxies/JsonArrayProxy.cs b/SystemTextJsonPatch/Internal/Proxies/JsonArrayProxy.cs index f76068a..5d6544c 100644 --- a/SystemTextJsonPatch/Internal/Proxies/JsonArrayProxy.cs +++ b/SystemTextJsonPatch/Internal/Proxies/JsonArrayProxy.cs @@ -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)