From 5c0dc5672203643318172ab4045a481dc284b8f8 Mon Sep 17 00:00:00 2001 From: Alexey Makeyev Date: Fri, 16 Oct 2015 14:51:53 +0300 Subject: [PATCH] number of issues discovered in predicate parsing And I dont see a quickfix. I suggest reimplementing entire path compilation using context-free grammar approach. --- .../com/jayway/jsonpath/PredicateTest.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/json-path/src/test/java/com/jayway/jsonpath/PredicateTest.java b/json-path/src/test/java/com/jayway/jsonpath/PredicateTest.java index 62939070..472cb641 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/PredicateTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/PredicateTest.java @@ -1,5 +1,6 @@ package com.jayway.jsonpath; +import org.junit.Ignore; import org.junit.Test; import java.util.List; @@ -23,4 +24,107 @@ public class PredicateTest extends BaseTest { assertThat(reader.read("$.store.book[?].isbn", List.class, booksWithISBN)).containsOnly("0-395-19395-8", "0-553-21311-3"); } + + @Ignore("not ready yet (requires compiler reimplementation)") + @Test + public void issue_predicate_can_have_escaped_backslash_in_prop() { + String json = "{\n" + + " \"logs\": [\n" + + " {\n" + + " \"message\": \"it\\\",\n" + + " \"id\": 2\n" + + " }\n" + + " ]\n" + + "}"; + + List result = JsonPath.read(json, "$.logs[?(@.message == 'it\\\\')].message"); + + assertThat(result).containsExactly("it\\"); + } + + @Ignore("not ready yet (requires compiler reimplementation)") + @Test + public void issue_predicate_can_have_bracket_in_regex() { + String json = "{\n" + + " \"logs\": [\n" + + " {\n" + + " \"message\": \"(it\",\n" + + " \"id\": 2\n" + + " }\n" + + " ]\n" + + "}"; + + List result = JsonPath.read(json, "$.logs[?(@.message =~ /\\(it/)].message"); + + assertThat(result).containsExactly("(it"); + } + + @Ignore("not ready yet (requires compiler reimplementation)") + @Test + public void issue_predicate_can_have_and_in_regex() { + String json = "{\n" + + " \"logs\": [\n" + + " {\n" + + " \"message\": \"it\",\n" + + " \"id\": 2\n" + + " }\n" + + " ]\n" + + "}"; + + List result = JsonPath.read(json, "$.logs[?(@.message =~ /&&|it/)].message"); + + assertThat(result).containsExactly("it"); + } + + @Ignore("not ready yet (requires compiler reimplementation)") + @Test + public void issue_predicate_can_have_and_in_prop() { + String json = "{\n" + + " \"logs\": [\n" + + " {\n" + + " \"message\": \"&& it\",\n" + + " \"id\": 2\n" + + " }\n" + + " ]\n" + + "}"; + + List result = JsonPath.read(json, "$.logs[?(@.message == '&& it')].message"); + + assertThat(result).containsExactly("&& it"); + } + + @Ignore("not ready yet (requires compiler reimplementation)") + @Test + public void issue_predicate_brackets_must_change_priorities() { + String json = "{\n" + + " \"logs\": [\n" + + " {\n" + + " \"id\": 2\n" + + " }\n" + + " ]\n" + + "}"; + + List result = JsonPath.read(json, "$.logs[?(@.message && (@.id == 1 || @.id == 2))].id"); + assertThat(result).isEmpty(); + + result = JsonPath.read(json, "$.logs[?((@.id == 2 || @.id == 1) && @.message)].id"); + assertThat(result).isEmpty(); + } + + @Ignore("not ready yet (requires compiler reimplementation)") + @Test + public void issue_predicate_can_have_square_bracket_in_prop() { + String json = "{\n" + + " \"logs\": [\n" + + " {\n" + + " \"message\": \"] it\",\n" + + " \"id\": 2\n" + + " }\n" + + " ]\n" + + "}"; + + List result = JsonPath.read(json, "$.logs[?(@.message == '] it')].message"); + + assertThat(result).containsExactly("] it"); + } }