Skip to content

Commit

Permalink
Merge pull request #400 from gregsdennis/patch/replace-missing-item
Browse files Browse the repository at this point in the history
replicate issue
  • Loading branch information
gregsdennis committed Mar 9, 2023
2 parents 813cd14 + 7a4cbfc commit 46491ec
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
32 changes: 29 additions & 3 deletions JsonPatch.Tests/GithubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using Json.More;
Expand Down Expand Up @@ -53,10 +54,11 @@ public void Issue393_PatchDoesNothing()
var arrayObject = JsonDocument.Parse(arrayObjectJson).RootElement;

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

Console.WriteLine(JsonSerializer.Serialize(patchedArray));
}

[Test]
public void Issue393_NodeAlreadyHasParent_2()
{
Expand Down Expand Up @@ -100,10 +102,11 @@ public void Issue393_NodeAlreadyHasParent_2()
// Way 2: just patch every element
foreach (var element in jsonArray)
{
var patchedNode = patchConfig.Apply(element).Result; // <- throws an error
var patchedNode = patchConfig.Apply(element).Result; // <- throws an error
Console.WriteLine(JsonSerializer.Serialize(patchedNode));
}
}

[Test]
public void Issue393_NodeAlreadyHasParent_3()
{
Expand Down Expand Up @@ -150,8 +153,31 @@ public void Issue393_NodeAlreadyHasParent_3()
var nodeToPatch = jsonArray[currentIndex];
jsonArray.RemoveAt(currentIndex);

var patchedNode = patchConfig.Apply(nodeToPatch).Result; // <- throws an error
var patchedNode = patchConfig.Apply(nodeToPatch).Result; // <- throws an error
Console.WriteLine(JsonSerializer.Serialize(patchedNode));
}
}

[Test]
public void Issue397_ReplaceShouldThrowForMissingValue()
{
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"",
""city"":""Boston""
}";

var singleObject = JsonNode.Parse(singleObjectJson);
var result = patchConfig.Apply(singleObject);
Console.WriteLine(JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }));
Assert.IsNotNull(patchConfig.Apply(singleObject).Error);
}
}
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.5</Version>
<Version>2.0.6</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.5.0</FileVersion>
<FileVersion>2.0.6.0</FileVersion>
<PackageId>JsonPatch.Net</PackageId>
<Authors>Greg Dennis</Authors>
<Company>Greg Dennis</Company>
Expand Down
5 changes: 3 additions & 2 deletions JsonPatch/ReplaceOperationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ public void Process(PatchContext context, PatchOperation operation)
return;
}

if (!operation.Path.EvaluateAndGetParent(context.Source, out var target))
if (!operation.Path.EvaluateAndGetParent(context.Source, out var target) ||
!operation.Path.TryEvaluate(context.Source, out _))
{
context.Message = $"Target path `{operation.From}` could not be reached.";
context.Message = $"Target path `{operation.Path}` could not be reached.";
return;
}

Expand Down
6 changes: 5 additions & 1 deletion json-everything.net/wwwroot/md/release-notes/json-patch.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# [2.0.6](https://github.com/gregsdennis/json-everything/pull/400) {#release-patch-2.0.6}

[#397](https://github.com/gregsdennis/json-everything/issues/397) - Fixed an issue where `replace` needs to check that the target location exists before proceeding with the `add` portion of its operation.

# [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.
[#393](https://github.com/gregsdennis/json-everything/issues/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}

Expand Down

0 comments on commit 46491ec

Please sign in to comment.