From bc1c0f4968939625ccd54e91687d8c9d00877607 Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Fri, 15 Sep 2023 08:54:06 -0400 Subject: [PATCH] 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()); +}