From e1480dbae582e81407b1bb84307dce6feac11133 Mon Sep 17 00:00:00 2001 From: abhishek kumar tiwari Date: Sun, 21 Jan 2024 22:02:06 +0530 Subject: [PATCH 1/2] Fix logic of parsing function parameter with no arguments. Currently a function parameter with no arguments is not parsed correctly. Given `$.objects.keys().size()`, after encountering keys (a function with no arguments) the position should move from $.objects.keys`(`).size() to $.objects.keys()`.`size(). But in current implementation read position moves from $.objects.keys`(`).size() to $.objects.keys(`)`.size() This pull request will fix this issue. --- .../java/com/jayway/jsonpath/internal/path/PathCompiler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java b/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java index 27b6e063..66851a58 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java @@ -235,7 +235,7 @@ else if (path.charAt(i) == OPEN_PARENTHESIS) String functionName = path.subSequence(startPosition, endPosition).toString(); functionParameters = parseFunctionParameters(functionName); } else { - path.setPosition(readPosition + 1); + path.setPosition(readPosition + 2); } } else { From 50d0ff29d996243cc2e929cbc3e06559a2bc3417 Mon Sep 17 00:00:00 2001 From: abhishek kumar tiwari Date: Mon, 29 Jan 2024 15:14:35 +0530 Subject: [PATCH 2/2] Add test case for fixing parsing logic for function with no arguments Add test case for fixing parsing logic for function with no arguments --- .../test/java/com/jayway/jsonpath/PathCompilerTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java b/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java index ef220619..82e96ed6 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/PathCompilerTest.java @@ -323,4 +323,11 @@ public void accept_only_a_single_comma_between_indexes() { public void property_must_be_separated_by_commas() { compile("$['aaa'}'bbb']"); } + + @Test + public void function_with_no_argument_can_be_parsed() { + String json = "{\"empty\": [], \"number-series\" : [[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12]]}"; + Integer result = JsonPath.read(json, "$.number-series.first().last()"); + assertThat(result).isEqualTo(10); + } }