Skip to content

Commit

Permalink
Merge pull request #394 from gregsdennis/patch/copying-nodes
Browse files Browse the repository at this point in the history
Patch/copying nodes
  • Loading branch information
gregsdennis committed Mar 7, 2023
2 parents aa91dd2 + beeafe3 commit 555e4bc
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 15 deletions.
157 changes: 157 additions & 0 deletions JsonPatch.Tests/GithubTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using Json.More;
using Json.Pointer;
using NUnit.Framework;

namespace Json.Patch.Tests;

public class GithubTests
{
[Test]
public void Issue393_PatchDoesNothing()
{
const string mask = "*****";
var maskJson = JsonValue.Create(mask);

var pathsToPatch = new[] { "/first_name", "/last_name" };

var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson));
var patchConfig = new JsonPatch(patchOperations);

Console.WriteLine(JsonSerializer.Serialize(patchConfig));

const string singleObjectJson = "{" +
"\"_id\":\"640729d45434f90313d25c78\"," +
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
"\"first_name\":\"Kathrine\"," +
"\"last_name\":\"Pate\"" +
"}";

var singleObject = JsonDocument.Parse(singleObjectJson).RootElement;
var patchedSingleObject = patchConfig.Apply(singleObject.AsNode()).Result;
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject));

const string arrayObjectJson = "[" +
"{" +
"\"_id\":\"640729d45434f90313d25c78\"," +
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
"\"first_name\":\"Kathrine\"," +
"\"last_name\":\"Pate\"" +
"}," +
"{\"_id\":\"640729d45b5824ffcabc30a5\"," +
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," +
"\"first_name\":\"Rivers\"," +
"\"last_name\":\"Smith\"" +
"}" +
"]";

var arrayObject = JsonDocument.Parse(arrayObjectJson).RootElement;

// Way 1: patch whole array
var patchedArray = patchConfig.Apply(arrayObject.AsNode()).Result; // <- does nothing

Console.WriteLine(JsonSerializer.Serialize(patchedArray));
}
[Test]
public void Issue393_NodeAlreadyHasParent_2()
{
const string mask = "*****";
var maskJson = JsonValue.Create(mask);

var pathsToPatch = new[] { "/first_name", "/last_name" };

var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson));
var patchConfig = new JsonPatch(patchOperations);

const string singleObjectJson = "{" +
"\"_id\":\"640729d45434f90313d25c78\"," +
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
"\"first_name\":\"Kathrine\"," +
"\"last_name\":\"Pate\"" +
"}";

var singleObject = JsonDocument.Parse(singleObjectJson).RootElement;
var patchedSingleObject = patchConfig.Apply(singleObject.AsNode()).Result;
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject));

const string arrayObjectJson = "[" +
"{" +
"\"_id\":\"640729d45434f90313d25c78\"," +
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
"\"first_name\":\"Kathrine\"," +
"\"last_name\":\"Pate\"" +
"}," +
"{\"_id\":\"640729d45b5824ffcabc30a5\"," +
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," +
"\"first_name\":\"Rivers\"," +
"\"last_name\":\"Smith\"" +
"}" +
"]";

var arrayObject = JsonDocument.Parse(arrayObjectJson).RootElement;

var jsonArray = arrayObject.AsNode().AsArray();

// Way 2: just patch every element
foreach (var element in jsonArray)
{
var patchedNode = patchConfig.Apply(element).Result; // <- throws an error
Console.WriteLine(JsonSerializer.Serialize(patchedNode));
}
}
[Test]
public void Issue393_NodeAlreadyHasParent_3()
{
const string mask = "*****";
var maskJson = JsonValue.Create(mask);

var pathsToPatch = new[] { "/first_name", "/last_name" };

var patchOperations = pathsToPatch.Select(path => PatchOperation.Replace(JsonPointer.Parse(path), maskJson));
var patchConfig = new JsonPatch(patchOperations);

const string singleObjectJson = "{" +
"\"_id\":\"640729d45434f90313d25c78\"," +
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
"\"first_name\":\"Kathrine\"," +
"\"last_name\":\"Pate\"" +
"}";

var singleObject = JsonNode.Parse(singleObjectJson);
var patchedSingleObject = patchConfig.Apply(singleObject).Result;
Console.WriteLine(JsonSerializer.Serialize(patchedSingleObject));

const string arrayObjectJson = "[" +
"{" +
"\"_id\":\"640729d45434f90313d25c78\"," +
"\"guid\":\"f2e2767c-03e0-4862-addc-7d46c55efb33\"," +
"\"first_name\":\"Kathrine\"," +
"\"last_name\":\"Pate\"" +
"}," +
"{\"_id\":\"640729d45b5824ffcabc30a5\"," +
"\"guid\":\"73193eda-074b-4f31-9f09-507a008ccb75\"," +
"\"first_name\":\"Rivers\"," +
"\"last_name\":\"Smith\"" +
"}" +
"]";

var arrayObject = JsonNode.Parse(arrayObjectJson);

var jsonArray = arrayObject.AsArray();

// Way 3: remove from initial array and then patch
for (int currentIndex = jsonArray.Count - 1; currentIndex >= 0; currentIndex--)
{
var nodeToPatch = jsonArray[currentIndex];
jsonArray.RemoveAt(currentIndex);

var patchedNode = patchConfig.Apply(nodeToPatch).Result; // <- throws an error
Console.WriteLine(JsonSerializer.Serialize(patchedNode));
}
}
}
7 changes: 4 additions & 3 deletions JsonPatch/AddOperationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Text.Json.Nodes;
using Json.More;

namespace Json.Patch;

Expand Down Expand Up @@ -28,7 +29,7 @@ public void Process(PatchContext context, PatchOperation operation)
var lastPathSegment = operation.Path.Segments.Last().Value;
if (target is JsonObject objTarget)
{
objTarget[lastPathSegment] = operation.Value;
objTarget[lastPathSegment] = operation.Value.Copy();
return;
}

Expand All @@ -43,9 +44,9 @@ public void Process(PatchContext context, PatchOperation operation)
return;
}
if (0 <= index && index < arrTarget.Count)
arrTarget.Insert(index, operation.Value);
arrTarget.Insert(index, operation.Value.Copy());
else if (index == arrTarget.Count)
arrTarget.Add(operation.Value);
arrTarget.Add(operation.Value.Copy());
else
context.Message = "Path indicates an index greater than the bounds of the array";
}
Expand Down
6 changes: 3 additions & 3 deletions JsonPatch/CopyOperationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Process(PatchContext context, PatchOperation operation)
{
if (Equals(operation.Path, operation.From)) return;

if (!operation.From.EvaluateAndGetParent(context.Source, out var source) ||
if (!operation.From.EvaluateAndGetParent(context.Source, out _) ||
!operation.From.TryEvaluate(context.Source, out var data))
{
context.Message = $"Source path `{operation.Path}` could not be reached.";
Expand Down Expand Up @@ -44,9 +44,9 @@ public void Process(PatchContext context, PatchOperation operation)
{
var index = lastPathSegment == "-" ? arrTarget.Count : int.Parse(lastPathSegment);
if (0 < index || index < arrTarget.Count)
arrTarget[index] = data;
arrTarget[index] = data.Copy();
else if (index == arrTarget.Count)
arrTarget.Add(data);
arrTarget.Add(data.Copy());
}
}
}
4 changes: 2 additions & 2 deletions JsonPatch/JsonPatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<RootNamespace>Json.Patch</RootNamespace>
<Version>2.0.4</Version>
<Version>2.0.5</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.4.0</FileVersion>
<FileVersion>2.0.5.0</FileVersion>
<PackageId>JsonPatch.Net</PackageId>
<Authors>Greg Dennis</Authors>
<Company>Greg Dennis</Company>
Expand Down
7 changes: 4 additions & 3 deletions JsonPatch/MoveOperationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Text.Json.Nodes;
using Json.More;
using Json.Pointer;

namespace Json.Patch;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void Process(PatchContext context, PatchOperation operation)
var lastPathSegment = operation.Path.Segments.Last().Value;
if (target is JsonObject objTarget)
{
objTarget[lastPathSegment] = data;
objTarget[lastPathSegment] = data.Copy();
return;
}

Expand All @@ -66,9 +67,9 @@ public void Process(PatchContext context, PatchOperation operation)
return;
}
if (0 <= index && index < arrTarget.Count)
arrTarget.Insert(index, data);
arrTarget.Insert(index, data.Copy());
else if (index == arrTarget.Count)
arrTarget.Add(data);
arrTarget.Add(data.Copy());
else
context.Message = "Path indicates an index greater than the bounds of the array";
}
Expand Down
7 changes: 4 additions & 3 deletions JsonPatch/ReplaceOperationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Text.Json.Nodes;
using Json.More;

namespace Json.Patch;

Expand All @@ -26,7 +27,7 @@ public void Process(PatchContext context, PatchOperation operation)
var lastPathSegment = operation.Path.Segments.Last().Value;
if (target is JsonObject objTarget)
{
objTarget[lastPathSegment] = operation.Value;
objTarget[lastPathSegment] = operation.Value.Copy();
return;
}

Expand All @@ -41,9 +42,9 @@ public void Process(PatchContext context, PatchOperation operation)
return;
}
if (0 <= index && index < arrTarget.Count)
arrTarget[index] = operation.Value;
arrTarget[index] = operation.Value.Copy();
else if (index == arrTarget.Count)
arrTarget.Add(operation.Value);
arrTarget.Add(operation.Value.Copy());
else
context.Message = "Path indicates an index greater than the bounds of the array";
}
Expand Down
4 changes: 4 additions & 0 deletions json-everything.net/wwwroot/md/release-notes/json-patch.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# [2.0.5](https://github.com/gregsdennis/json-everything/pull/394) {#release-patch-2.0.5}

[#393](https://github.com/gregsdennis/json-everything/pull/393) - Fixed an `InvalidOperationException` from some of the operations.

# [2.0.4](https://github.com/gregsdennis/json-everything/pull/323) {#release-patch-2.0.4}

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

0 comments on commit 555e4bc

Please sign in to comment.