Browse Source

Fix #857 DCollecting all values for given key in object array returns empty Collection

pull/859/head
skwqy 2 years ago
parent
commit
5174797534
  1. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathToken.java
  2. 28
      json-path/src/test/java/com/jayway/jsonpath/Issue_857.java

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

@ -82,11 +82,11 @@ public abstract class PathToken {
PathRef pathRef = ctx.forUpdate() ? PathRef.create(model, property) : PathRef.NO_OP;
if (isLeaf()) {
String idx = "[" + String.valueOf(upstreamArrayIndex) + "]";
if(idx.equals("[-1]") || ctx.getRoot().getTail().prev().getPathFragment().equals(idx)){
if (idx.equals("[-1]") || ctx.getRoot().getTail().prev().getPathFragment().equals(idx) ||
ctx.getRoot().getTail().prev().getPathFragment().equals("[*]")) {
ctx.addResult(evalPath, pathRef, propertyVal);
}
}
else {
} else {
next().evaluate(evalPath, pathRef, propertyVal, ctx);
}
} else {
@ -214,7 +214,8 @@ public abstract class PathToken {
return super.equals(obj);
}
public void invoke(PathFunction pathFunction, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
public void invoke(PathFunction pathFunction, String currentPath, PathRef parent, Object model,
EvaluationContextImpl ctx) {
ctx.addResult(currentPath, parent, pathFunction.invoke(currentPath, parent, model, ctx, null));
}

28
json-path/src/test/java/com/jayway/jsonpath/Issue_857.java

@ -0,0 +1,28 @@
package com.jayway.jsonpath;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
/**
* @author skwqyg
* @Created on 2022 09 2022/9/10 16:36
*/
public class Issue_857 extends BaseTest{
@Test
public void test(){
Collection values = JsonPath.parse("[{\"key\":\"first value\"},{\"key\":\"second value\"}]")
.read("$..[*].key", java.util.Collection.class);
Assert.assertEquals(2,values.size());
Set<String> expect = new HashSet<>();
expect.add("first value");
expect.add("second value");
Assert.assertTrue(expect.containsAll(values));
System.out.println(values);
}
}
Loading…
Cancel
Save