Skip to content

Commit a60e96d

Browse files
authored
fix issue #786 and add test case (#801)
1 parent 25b89a9 commit a60e96d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jayway.jsonpath.internal.path;
22

3+
import com.jayway.jsonpath.internal.Path;
34
import com.jayway.jsonpath.internal.PathRef;
45
import com.jayway.jsonpath.internal.function.Parameter;
56
import com.jayway.jsonpath.internal.function.PathFunction;
@@ -39,11 +40,29 @@ public void evaluate(String currentPath, PathRef parent, Object model, Evaluatio
3940
evaluateParameters(currentPath, parent, model, ctx);
4041
Object result = pathFunction.invoke(currentPath, parent, model, ctx, functionParams);
4142
ctx.addResult(currentPath + "." + functionName, parent, result);
43+
cleanWildcardPathToken();
4244
if (!isLeaf()) {
4345
next().evaluate(currentPath, parent, result, ctx);
4446
}
4547
}
4648

49+
private void cleanWildcardPathToken() {
50+
if (null != functionParams && functionParams.size() > 0) {
51+
Path path = functionParams.get(0).getPath();
52+
if (null != path && !path.isFunctionPath() && path instanceof CompiledPath) {
53+
RootPathToken root = ((CompiledPath) path).getRoot();
54+
PathToken tail = root.getNext();
55+
while (null != tail && null != tail.getNext() ) {
56+
if(tail.getNext() instanceof WildcardPathToken){
57+
tail.setNext(tail.getNext().getNext());
58+
break;
59+
}
60+
tail = tail.getNext();
61+
}
62+
}
63+
}
64+
}
65+
4766
private void evaluateParameters(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
4867

4968
if (null != functionParams) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.jayway.jsonpath;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
/**
8+
* test for issue 786
9+
*/
10+
public class Issue_786 extends BaseTest{
11+
12+
@Test
13+
public void test(){
14+
assertThat(bookLength()).describedAs("First run").isEqualTo(4);
15+
assertThat(bookLength()).describedAs("Second run").isEqualTo(4);
16+
assertThat(bookLength()).describedAs("Third run").isEqualTo(4);
17+
}
18+
19+
private int bookLength() {
20+
return JsonPath.read(JSON_DOCUMENT, "$..book.length()");
21+
}
22+
23+
}

0 commit comments

Comments
 (0)