Browse Source

fix issue json-path#786 and add test case (#801)

pull/793/head
sdww0 3 years ago committed by GitHub
parent
commit
a60e96d6d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      json-path/src/main/java/com/jayway/jsonpath/internal/path/FunctionPathToken.java
  2. 23
      json-path/src/test/java/com/jayway/jsonpath/Issue_786.java

19
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) {

23
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()");
}
}
Loading…
Cancel
Save