Skip to content

Commit

Permalink
Merge pull request #828 from vimateck/patch/copy-to-end-of-array
Browse files Browse the repository at this point in the history
fix: make copy with '-' as target working
  • Loading branch information
gregsdennis authored Dec 3, 2024
2 parents 31afd0e + c6e0e76 commit f00c935
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
31 changes: 28 additions & 3 deletions src/JsonPatch.Tests/GithubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public void Issue543_CreatePatchToAddItem()
}

[Test]
public void MoveShouldMoveItemAround()
public void Issue825_MoveShouldMoveItemToTheEndOfArray()
{
var jsonModel = new JsonObject
{
Expand All @@ -226,11 +226,36 @@ public void MoveShouldMoveItemAround()
var result = doc.Apply(jsonModel);

Assert.That(result.Error, Is.Null);
Assert.That(result.Result!["Items"]!.AsArray()[^1].IsEquivalentTo(3));

var jsonArray = result.Result!["Items"]!.AsArray();
Assert.That(jsonArray, Has.Count.EqualTo(5));
Assert.That(jsonArray[^1].IsEquivalentTo(3));
}

[Test]
public void Issue825_CopyShouldCopyItemToEndOfArray()
{
var jsonModel = new JsonObject
{
["Items"] = new JsonArray(1, 2, 3, 4, 5)
};

var doc = new JsonPatch(
PatchOperation.Copy(
JsonPointer.Parse("/Items/2"),
JsonPointer.Parse("/Items/-")));

var result = doc.Apply(jsonModel);

Assert.That(result.Error, Is.Null);

var jsonArray = result.Result!["Items"]!.AsArray();
Assert.That(jsonArray, Has.Count.EqualTo(6));
Assert.That(jsonArray[^1].IsEquivalentTo(3));
}

[Test]
public void CopyShouldCopyValuesToTarget()
public void Issue826_CopyShouldCopyValuesToTarget()
{
var jsonModel = new JsonObject
{
Expand Down
2 changes: 1 addition & 1 deletion src/JsonPatch/CopyOperationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Process(PatchContext context, PatchOperation operation)

if (target is JsonArray arrTarget)
{
var index = lastPathSegment.Length == 0 && lastPathSegment[0] == '-'
var index = lastPathSegment.Length != 0 && lastPathSegment[0] == '-'
? arrTarget.Count
: int.TryParse(lastPathSegment, out var i)
? i
Expand Down
4 changes: 2 additions & 2 deletions src/JsonPatch/JsonPatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageId>JsonPatch.Net</PackageId>
<Version>3.2.1</Version>
<FileVersion>3.2.1</FileVersion>
<Version>3.2.2</Version>
<FileVersion>3.2.2</FileVersion>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<Authors>Greg Dennis</Authors>
<Description>JSON Patch built on the System.Text.Json namespace</Description>
Expand Down
4 changes: 4 additions & 0 deletions tools/ApiDocsGenerator/release-notes/rn-json-patch.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ title: JsonPatch.Net
icon: fas fa-tag
order: "09.09"
---
# [3.2.2](https://github.com/gregsdennis/json-everything/pull/827) {#release-patch-3.2.2}

- [#825](https://github.com/gregsdennis/json-everything/issues/825) - Copy not working when `to` pointer ends with `-`. (Cannot copy to end of array.)

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

- [#825](https://github.com/gregsdennis/json-everything/issues/825) - Move not working when `to` pointer ends with `-`. (Cannot move to end of array.)
Expand Down

0 comments on commit f00c935

Please sign in to comment.