diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/path/CompiledPath.java b/json-path/src/main/java/com/jayway/jsonpath/internal/path/CompiledPath.java index 80ac2bd4..8e50dbaf 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/path/CompiledPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/path/CompiledPath.java @@ -60,14 +60,14 @@ public class CompiledPath implements Path { * A function with the scanner as input, or if this situation doesn't exist just the input path */ private RootPathToken invertScannerFunctionRelationship(final RootPathToken path) { - if (path.isFunctionPath() && path.next() instanceof ScanPathToken) { + if (path.isFunctionPath()) { PathToken token = path; PathToken prior = null; while (null != (token = token.next()) && !(token instanceof FunctionPathToken)) { prior = token; } // Invert the relationship $..path.function() to $.function($..path) - if (token instanceof FunctionPathToken) { + if (path.next() instanceof ScanPathToken || (prior instanceof PredicatePathToken && prior.toString().equals("[?].length()") && token != null)) { prior.setNext(null); path.setTail(prior); diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java index 61c40f26..fb0e30e8 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonOrgJsonProviderTest.java @@ -151,4 +151,47 @@ public class JsonOrgJsonProviderTest extends BaseTest { Object result = JsonPath.read(json,"$..[?(@.price == 22.99)].length()"); assertThat(result).isEqualTo(1); } + + @Test + public void test_issue_562() { + String json = "{\n" + + " \"store\": {\n" + + " \"book\": [\n" + + " {\n" + + " \"category\": \"reference\",\n" + + " \"author\": \"Nigel Rees\",\n" + + " \"title\": \"Sayings of the Century\",\n" + + " \"price\": 8.95\n" + + " },\n" + + " {\n" + + " \"category\": \"fiction\",\n" + + " \"author\": \"Evelyn Waugh\",\n" + + " \"title\": \"Sword of Honour\",\n" + + " \"price\": 12.99\n" + + " },\n" + + " {\n" + + " \"category\": \"fiction\",\n" + + " \"author\": \"Herman Melville\",\n" + + " \"title\": \"Moby Dick\",\n" + + " \"isbn\": \"0-553-21311-3\",\n" + + " \"price\": 8.99\n" + + " },\n" + + " {\n" + + " \"category\": \"fiction\",\n" + + " \"author\": \"J. R. R. Tolkien\",\n" + + " \"title\": \"The Lord of the Rings\",\n" + + " \"isbn\": \"0-395-19395-8\",\n" + + " \"price\": 22.99\n" + + " }\n" + + " ],\n" + + " \"bicycle\": {\n" + + " \"color\": \"red\",\n" + + " \"price\": 19.95\n" + + " }\n" + + " },\n" + + " \"expensive\": 10\n" + + "}\n"; + Object result = JsonPath.read(json,"$.store.book[?(@.price>10)].length()"); + assertThat(result).isEqualTo(2); + } }