diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java b/json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java index 2d24b70d..0f449895 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java @@ -1,5 +1,6 @@ package com.jayway.jsonpath.internal.path; +import com.jayway.jsonpath.internal.Path; import com.jayway.jsonpath.internal.PathRef; import com.jayway.jsonpath.internal.function.Parameter; import com.jayway.jsonpath.internal.function.PathFunction; @@ -39,11 +40,29 @@ public class FunctionPathToken extends PathToken { evaluateParameters(currentPath, parent, model, ctx); Object result = pathFunction.invoke(currentPath, parent, model, ctx, functionParams); ctx.addResult(currentPath + "." + functionName, parent, result); + cleanWildcardPathToken(); if (!isLeaf()) { next().evaluate(currentPath, parent, result, ctx); } } + private void cleanWildcardPathToken() { + if (null != functionParams && functionParams.size() > 0) { + Path path = functionParams.get(0).getPath(); + if (null != path && !path.isFunctionPath() && path instanceof CompiledPath) { + RootPathToken root = ((CompiledPath) path).getRoot(); + PathToken tail = root.getNext(); + while (null != tail && null != tail.getNext() ) { + if(tail.getNext() instanceof WildcardPathToken){ + tail.setNext(tail.getNext().getNext()); + break; + } + tail = tail.getNext(); + } + } + } + } + private void evaluateParameters(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { if (null != functionParams) { diff --git a/json-path/src/test/java/com/jayway/jsonpath/Issue_786.java b/json-path/src/test/java/com/jayway/jsonpath/Issue_786.java new file mode 100644 index 00000000..97329451 --- /dev/null +++ b/json-path/src/test/java/com/jayway/jsonpath/Issue_786.java @@ -0,0 +1,23 @@ +package com.jayway.jsonpath; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for issue 786 + */ +public class Issue_786 extends BaseTest{ + + @Test + public void test(){ + assertThat(bookLength()).describedAs("First run").isEqualTo(4); + assertThat(bookLength()).describedAs("Second run").isEqualTo(4); + assertThat(bookLength()).describedAs("Third run").isEqualTo(4); + } + + private int bookLength() { + return JsonPath.read(JSON_DOCUMENT, "$..book.length()"); + } + +}