From b74ad331b4ab8b073275e4e8cefeff958fe0cd99 Mon Sep 17 00:00:00 2001 From: James Lucas Date: Mon, 24 Mar 2025 11:05:10 +1100 Subject: [PATCH] Fixed: ignore whitespace after comparator in filter expression --- CHANGELOG.md | 3 +++ src/Filters/QueryMatchFilter.php | 2 +- tests/JSONPathTest.php | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad5ecdd..2218e0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +### 0.10.1 +- Fixed ignore whitespace after comparison value in filter expression + ### 0.10.0 - Fixed query/selector Filter Expression With Current Object - Fixed query/selector Filter Expression With Different Grouped Operators diff --git a/src/Filters/QueryMatchFilter.php b/src/Filters/QueryMatchFilter.php index 51d0894..6906d81 100644 --- a/src/Filters/QueryMatchFilter.php +++ b/src/Filters/QueryMatchFilter.php @@ -28,7 +28,7 @@ class QueryMatchFilter extends AbstractFilter protected const MATCH_QUERY_OPERATORS = ' (@\.(?[^\s<>!=]+)|@\[["\']?(?.*?)["\']?\]|(?@)|(%group(?\d+)%)) - (\s*(?==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?.+?(?=(&&|$|\|\||%))))? + (\s*(?==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?.+?(?=\s*(?:&&|$|\|\||%))))? (\s*(?&&|\|\|)\s*)? '; diff --git a/tests/JSONPathTest.php b/tests/JSONPathTest.php index 800c696..049bf3b 100644 --- a/tests/JSONPathTest.php +++ b/tests/JSONPathTest.php @@ -324,6 +324,21 @@ public function testQueryMatchEquals(): void self::assertEquals('The Lord of the Rings', $result[0]); } + /** + * $..books[?(@.category=="fiction" && @.author == \'Evelyn Waugh\')].title' + * Filter books that are in the "..." category and have an author equal to "..." + * + * @throws JSONPathException|JsonException + */ + public function testQueryMatchWhitespaceIgnored(): void + { + // Additional spaces in filter string are intended to be there + $result = (new JSONPath($this->getData('example'))) + ->find('$..books[?( @.category=="fiction" && @.author == \'Evelyn Waugh\' )].title'); + + self::assertEquals('Sword of Honour', $result[0]); + } + /** * $..books[?(@.author = 1)] * Filter books that have a title equal to "..."