Skip to content

Commit 8b490ab

Browse files
committed
fixed invalidop exception
1 parent aa91dd2 commit 8b490ab

File tree

7 files changed

+178
-14
lines changed

7 files changed

+178
-14
lines changed

JsonPatch.Tests/GithubTests.cs

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.Json;
6+
using System.Text.Json.Nodes;
7+
using Json.More;
8+
using Json.Pointer;
9+
using NUnit.Framework;
10+
11+
namespace Json.Patch.Tests;
12+
13+
public class GithubTests
14+
{
15+
[Test]
16+
public void Issue393_PatchDoesNothing()
17+
{
18+
const string mask = "*****";
19+
var maskJson = JsonValue.Create(mask);
20+
21+
var pathsToPatch = new[] { "/first_name", "/last_name" };
22+
23+
var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson));
24+
var patchConfig = new JsonPatch(patchOperations);
25+
26+
Console.WriteLine(JsonSerializer.Serialize(patchConfig));
27+
28+
const string singleObjectJson = "{" +
29+
"\"_id\":\"640729d45434f90313d25c78\"," +
30+
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
31+
"\"first_name\":\"Kathrine\"," +
32+
"\"last_name\":\"Pate\"" +
33+
"}";
34+
35+
var singleObject = JsonDocument.Parse(singleObjectJson).RootElement;
36+
var patchedSingleObject = patchConfig.Apply(singleObject.AsNode()).Result;
37+
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject));
38+
39+
const string arrayObjectJson = "[" +
40+
"{" +
41+
"\"_id\":\"640729d45434f90313d25c78\"," +
42+
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
43+
"\"first_name\":\"Kathrine\"," +
44+
"\"last_name\":\"Pate\"" +
45+
"}," +
46+
"{\"_id\":\"640729d45b5824ffcabc30a5\"," +
47+
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," +
48+
"\"first_name\":\"Rivers\"," +
49+
"\"last_name\":\"Smith\"" +
50+
"}" +
51+
"]";
52+
53+
var arrayObject = JsonDocument.Parse(arrayObjectJson).RootElement;
54+
55+
// Way 1: patch whole array
56+
var patchedArray = patchConfig.Apply(arrayObject.AsNode()).Result; // <- does nothing
57+
58+
Console.WriteLine(JsonSerializer.Serialize(patchedArray));
59+
}
60+
[Test]
61+
public void Issue393_NodeAlreadyHasParent_2()
62+
{
63+
const string mask = "*****";
64+
var maskJson = JsonValue.Create(mask);
65+
66+
var pathsToPatch = new[] { "/first_name", "/last_name" };
67+
68+
var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson));
69+
var patchConfig = new JsonPatch(patchOperations);
70+
71+
const string singleObjectJson = "{" +
72+
"\"_id\":\"640729d45434f90313d25c78\"," +
73+
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
74+
"\"first_name\":\"Kathrine\"," +
75+
"\"last_name\":\"Pate\"" +
76+
"}";
77+
78+
var singleObject = JsonDocument.Parse(singleObjectJson).RootElement;
79+
var patchedSingleObject = patchConfig.Apply(singleObject.AsNode()).Result;
80+
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject));
81+
82+
const string arrayObjectJson = "[" +
83+
"{" +
84+
"\"_id\":\"640729d45434f90313d25c78\"," +
85+
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
86+
"\"first_name\":\"Kathrine\"," +
87+
"\"last_name\":\"Pate\"" +
88+
"}," +
89+
"{\"_id\":\"640729d45b5824ffcabc30a5\"," +
90+
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," +
91+
"\"first_name\":\"Rivers\"," +
92+
"\"last_name\":\"Smith\"" +
93+
"}" +
94+
"]";
95+
96+
var arrayObject = JsonDocument.Parse(arrayObjectJson).RootElement;
97+
98+
var jsonArray = arrayObject.AsNode().AsArray();
99+
100+
// Way 2: just patch every element
101+
foreach (var element in jsonArray)
102+
{
103+
var patchedNode = patchConfig.Apply(element).Result; // <- throws an error
104+
Console.WriteLine(JsonSerializer.Serialize(patchedNode));
105+
}
106+
}
107+
[Test]
108+
public void Issue393_NodeAlreadyHasParent_3()
109+
{
110+
const string mask = "*****";
111+
var maskJson = JsonValue.Create(mask);
112+
113+
var pathsToPatch = new[] { "/first_name", "/last_name" };
114+
115+
var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson));
116+
var patchConfig = new JsonPatch(patchOperations);
117+
118+
const string singleObjectJson = "{" +
119+
"\"_id\":\"640729d45434f90313d25c78\"," +
120+
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
121+
"\"first_name\":\"Kathrine\"," +
122+
"\"last_name\":\"Pate\"" +
123+
"}";
124+
125+
var singleObject = JsonNode.Parse(singleObjectJson);
126+
var patchedSingleObject = patchConfig.Apply(singleObject).Result;
127+
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject));
128+
129+
const string arrayObjectJson = "[" +
130+
"{" +
131+
"\"_id\":\"640729d45434f90313d25c78\"," +
132+
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
133+
"\"first_name\":\"Kathrine\"," +
134+
"\"last_name\":\"Pate\"" +
135+
"}," +
136+
"{\"_id\":\"640729d45b5824ffcabc30a5\"," +
137+
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," +
138+
"\"first_name\":\"Rivers\"," +
139+
"\"last_name\":\"Smith\"" +
140+
"}" +
141+
"]";
142+
143+
var arrayObject = JsonNode.Parse(arrayObjectJson);
144+
145+
var jsonArray = arrayObject.AsArray();
146+
147+
// Way 3: remove from initial array and then patch
148+
for (int currentIndex = jsonArray.Count - 1; currentIndex >= 0; currentIndex--)
149+
{
150+
var nodeToPatch = jsonArray[currentIndex];
151+
jsonArray.RemoveAt(currentIndex);
152+
153+
var patchedNode = patchConfig.Apply(nodeToPatch).Result; // <- throws an error
154+
Console.WriteLine(JsonSerializer.Serialize(patchedNode));
155+
}
156+
}
157+
}

JsonPatch/AddOperationHandler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using System.Text.Json.Nodes;
3+
using Json.More;
34

45
namespace Json.Patch;
56

@@ -28,7 +29,7 @@ public void Process(PatchContext context, PatchOperation operation)
2829
var lastPathSegment = operation.Path.Segments.Last().Value;
2930
if (target is JsonObject objTarget)
3031
{
31-
objTarget[lastPathSegment] = operation.Value;
32+
objTarget[lastPathSegment] = operation.Value.Copy();
3233
return;
3334
}
3435

@@ -43,9 +44,9 @@ public void Process(PatchContext context, PatchOperation operation)
4344
return;
4445
}
4546
if (0 <= index && index < arrTarget.Count)
46-
arrTarget.Insert(index, operation.Value);
47+
arrTarget.Insert(index, operation.Value.Copy());
4748
else if (index == arrTarget.Count)
48-
arrTarget.Add(operation.Value);
49+
arrTarget.Add(operation.Value.Copy());
4950
else
5051
context.Message = "Path indicates an index greater than the bounds of the array";
5152
}

JsonPatch/CopyOperationHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Process(PatchContext context, PatchOperation operation)
1414
{
1515
if (Equals(operation.Path, operation.From)) return;
1616

17-
if (!operation.From.EvaluateAndGetParent(context.Source, out var source) ||
17+
if (!operation.From.EvaluateAndGetParent(context.Source, out _) ||
1818
!operation.From.TryEvaluate(context.Source, out var data))
1919
{
2020
context.Message = $"Source path `{operation.Path}` could not be reached.";
@@ -44,9 +44,9 @@ public void Process(PatchContext context, PatchOperation operation)
4444
{
4545
var index = lastPathSegment == "-" ? arrTarget.Count : int.Parse(lastPathSegment);
4646
if (0 < index || index < arrTarget.Count)
47-
arrTarget[index] = data;
47+
arrTarget[index] = data.Copy();
4848
else if (index == arrTarget.Count)
49-
arrTarget.Add(data);
49+
arrTarget.Add(data.Copy());
5050
}
5151
}
5252
}

JsonPatch/JsonPatch.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<LangVersion>latest</LangVersion>
66
<RootNamespace>Json.Patch</RootNamespace>
7-
<Version>2.0.4</Version>
7+
<Version>2.0.5</Version>
88
<AssemblyVersion>2.0.0.0</AssemblyVersion>
9-
<FileVersion>2.0.4.0</FileVersion>
9+
<FileVersion>2.0.5.0</FileVersion>
1010
<PackageId>JsonPatch.Net</PackageId>
1111
<Authors>Greg Dennis</Authors>
1212
<Company>Greg Dennis</Company>

JsonPatch/MoveOperationHandler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using System.Text.Json.Nodes;
3+
using Json.More;
34
using Json.Pointer;
45

56
namespace Json.Patch;
@@ -51,7 +52,7 @@ public void Process(PatchContext context, PatchOperation operation)
5152
var lastPathSegment = operation.Path.Segments.Last().Value;
5253
if (target is JsonObject objTarget)
5354
{
54-
objTarget[lastPathSegment] = data;
55+
objTarget[lastPathSegment] = data.Copy();
5556
return;
5657
}
5758

@@ -66,9 +67,9 @@ public void Process(PatchContext context, PatchOperation operation)
6667
return;
6768
}
6869
if (0 <= index && index < arrTarget.Count)
69-
arrTarget.Insert(index, data);
70+
arrTarget.Insert(index, data.Copy());
7071
else if (index == arrTarget.Count)
71-
arrTarget.Add(data);
72+
arrTarget.Add(data.Copy());
7273
else
7374
context.Message = "Path indicates an index greater than the bounds of the array";
7475
}

JsonPatch/ReplaceOperationHandler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq;
22
using System.Text.Json.Nodes;
3+
using Json.More;
34

45
namespace Json.Patch;
56

@@ -26,7 +27,7 @@ public void Process(PatchContext context, PatchOperation operation)
2627
var lastPathSegment = operation.Path.Segments.Last().Value;
2728
if (target is JsonObject objTarget)
2829
{
29-
objTarget[lastPathSegment] = operation.Value;
30+
objTarget[lastPathSegment] = operation.Value.Copy();
3031
return;
3132
}
3233

@@ -41,9 +42,9 @@ public void Process(PatchContext context, PatchOperation operation)
4142
return;
4243
}
4344
if (0 <= index && index < arrTarget.Count)
44-
arrTarget[index] = operation.Value;
45+
arrTarget[index] = operation.Value.Copy();
4546
else if (index == arrTarget.Count)
46-
arrTarget.Add(operation.Value);
47+
arrTarget.Add(operation.Value.Copy());
4748
else
4849
context.Message = "Path indicates an index greater than the bounds of the array";
4950
}

json-everything.net/wwwroot/md/release-notes/json-patch.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# [2.0.5](https://github.com/gregsdennis/json-everything/pull/394) {#release-patch-2.0.5}
2+
3+
[#393](https://github.com/gregsdennis/json-everything/pull/393) - Fixed an `InvalidOperationException` from some of the operations.
4+
15
# [2.0.4](https://github.com/gregsdennis/json-everything/pull/323) {#release-patch-2.0.4}
26

37
[#322](https://github.com/gregsdennis/json-everything/pull/322) - [@z4kn4fein](https://github.com/z4kn4fein) discovered and fixed an issue in the `move` operation logic.

0 commit comments

Comments
 (0)