From ccbe244b835f692e32f95817875739a3ed1273d0 Mon Sep 17 00:00:00 2001 From: Frans van Ek Date: Thu, 25 May 2023 12:20:20 +0200 Subject: [PATCH] Feature/bug multi layer array 61 (#62) * bug fix * removed comment --- JLio.Commands/Logic/CopyMove.cs | 2 +- JLio.UnitTests/CommandsTests/CopyMoveTests.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/JLio.Commands/Logic/CopyMove.cs b/JLio.Commands/Logic/CopyMove.cs index 632cfaf..b42c100 100644 --- a/JLio.Commands/Logic/CopyMove.cs +++ b/JLio.Commands/Logic/CopyMove.cs @@ -51,7 +51,7 @@ private int GetInnerArrayIndex() var toPath = JsonPathMethods.SplitPath(ToPath); if (!fromPath.HasArrayIndication || !toPath.HasArrayIndication) return -1; var index = fromPath.GetSameElementsIndex(toPath); - if (!HasArrayNotationAfterIndex(fromPath, index)) + if (!HasArrayNotationAfterIndex(fromPath, index) || index > 1) return index; return -1; diff --git a/JLio.UnitTests/CommandsTests/CopyMoveTests.cs b/JLio.UnitTests/CommandsTests/CopyMoveTests.cs index 6ebbe47..44baec7 100644 --- a/JLio.UnitTests/CommandsTests/CopyMoveTests.cs +++ b/JLio.UnitTests/CommandsTests/CopyMoveTests.cs @@ -146,6 +146,23 @@ public void CanCopyMovePropertiesInAnLayeredArray(string startobject, string mov expectedValue))); } + [TestCase( + "{\"firstArray\":[{\"target\":[],\"secondArray\":[{\"id\":\"item1\",\"sub\":{\"name\":\"item 1\"}},{\"id\":\"item2\",\"sub\":{\"name\":\"item 2\"}}]},{\"target\":[],\"secondArray\":[{\"id\":\"item3\",\"sub\":{\"name\":\"item 3\"}},{\"id\":\"item4\",\"sub\":{\"name\":\"item 4\"}}]}]}", + "$.firstArray[*].secondArray[*].sub", + "$.firstArray[*].target", + "{\"firstArray\":[{\"target\":[{\"name\":\"item 1\"},{\"name\":\"item 2\"}],\"secondArray\":[{\"id\":\"item1\",\"sub\":{\"name\":\"item 1\"}},{\"id\":\"item2\",\"sub\":{\"name\":\"item 2\"}}]},{\"target\":[{\"name\":\"item 3\"},{\"name\":\"item 4\"}],\"secondArray\":[{\"id\":\"item3\",\"sub\":{\"name\":\"item 3\"}},{\"id\":\"item4\",\"sub\":{\"name\":\"item 4\"}}]}]}" + )] + public void CanCopyPropertiesInAnLayeredArray(string startobject, string copyFrom, string copyTo, string expectedValue) + { + var startObject = startobject; + var result = + new Copy(copyFrom, copyTo).Execute( + JToken.Parse(startObject), executeOptions); + Assert.IsTrue(JToken.DeepEquals(result.Data, + JToken.Parse( + expectedValue))); + } + [Test] public void CanCopyMovePropertiesInAnArray() {