From bc1c0f4968939625ccd54e91687d8c9d00877607 Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Fri, 15 Sep 2023 08:54:06 -0400 Subject: [PATCH 1/2] enclose match regex in parens to match full string --- serde_json_path/src/parser/selector/function/registry.rs | 2 +- serde_json_path/tests/regressions.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/serde_json_path/src/parser/selector/function/registry.rs b/serde_json_path/src/parser/selector/function/registry.rs index aa6daf6..bc88636 100644 --- a/serde_json_path/src/parser/selector/function/registry.rs +++ b/serde_json_path/src/parser/selector/function/registry.rs @@ -46,7 +46,7 @@ fn count(nodes: NodesType) -> ValueType { #[serde_json_path_macros::register(name = "match", target = MATCH_FUNC)] fn match_func(value: ValueType, rgx: ValueType) -> LogicalType { match (value.as_value(), rgx.as_value()) { - (Some(Value::String(s)), Some(Value::String(r))) => Regex::new(format!("^{r}$").as_str()) + (Some(Value::String(s)), Some(Value::String(r))) => Regex::new(format!("^({r})$").as_str()) .map(|r| r.is_match(s)) .map(Into::into) .unwrap_or_default(), diff --git a/serde_json_path/tests/regressions.rs b/serde_json_path/tests/regressions.rs index c53b3b5..7f1f940 100644 --- a/serde_json_path/tests/regressions.rs +++ b/serde_json_path/tests/regressions.rs @@ -11,3 +11,12 @@ fn issue_49() { let path = JsonPath::parse("$[?(@.a == 2)]").expect("parses JSONPath"); assert!(path.query(&value).is_empty()); } + +// This test is meant for issue #60, which can be found here: +// https://github.com/hiltontj/serde_json_path/issues/60 +#[test] +fn issue_60() { + let value = json!([{"foo": "bar"}, {"foo": "biz"}]); + let path = JsonPath::parse("$[? match(@.foo, '|')]").expect("parses JSONPath"); + assert!(path.query(&value).is_empty()); +} From 3bc98aba757bc18c59627198da164caf4d1259a1 Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Sat, 16 Sep 2023 22:39:19 -0400 Subject: [PATCH 2/2] update changelog --- serde_json_path/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/serde_json_path/CHANGELOG.md b/serde_json_path/CHANGELOG.md index 0e693d1..79d8c11 100644 --- a/serde_json_path/CHANGELOG.md +++ b/serde_json_path/CHANGELOG.md @@ -11,11 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **documentation**: Improve example in Filter Selector section of main docs ([#54]) - **documentation**: Improve examples in Slice Slector section of main docs ([#55]) - **documentation**: Other improvements to documentation ([#56]) +- **fixed**: Formulate the regex used by the `match` function to correctly handle regular expressions with leading or trailing `|` characters ([#61]) [#53]: https://github.com/hiltontj/serde_json_path/pull/53 [#54]: https://github.com/hiltontj/serde_json_path/pull/54 [#55]: https://github.com/hiltontj/serde_json_path/pull/55 [#56]: https://github.com/hiltontj/serde_json_path/pull/56 +[#61]: https://github.com/hiltontj/serde_json_path/pull/61 # 0.6.2 (13 July 2023)