From 66d22b189a26c2bc1466e552d05207bb163deda4 Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Wed, 12 Jul 2023 21:05:29 -0400 Subject: [PATCH 1/5] fix a bug in singular query evaluation that was producing false positive results from filters --- serde_json_path/tests/regressions.rs | 13 +++++++++++++ serde_json_path_core/src/spec/selector/filter.rs | 6 ++++++ 2 files changed, 19 insertions(+) create mode 100644 serde_json_path/tests/regressions.rs diff --git a/serde_json_path/tests/regressions.rs b/serde_json_path/tests/regressions.rs new file mode 100644 index 0000000..c53b3b5 --- /dev/null +++ b/serde_json_path/tests/regressions.rs @@ -0,0 +1,13 @@ +use serde_json::json; +use serde_json_path::JsonPath; +#[cfg(feature = "trace")] +use test_log::test; + +// This test is meant for issue #49, which can be found here: +// https://github.com/hiltontj/serde_json_path/issues/49 +#[test] +fn issue_49() { + let value = json!({"a": 1, "b": 2}); + let path = JsonPath::parse("$[?(@.a == 2)]").expect("parses JSONPath"); + assert!(path.query(&value).is_empty()); +} diff --git a/serde_json_path_core/src/spec/selector/filter.rs b/serde_json_path_core/src/spec/selector/filter.rs index 7a5b405..07a0d9d 100644 --- a/serde_json_path_core/src/spec/selector/filter.rs +++ b/serde_json_path_core/src/spec/selector/filter.rs @@ -366,6 +366,7 @@ impl std::fmt::Display for Comparable { impl Comparable { #[doc(hidden)] + #[cfg_attr(feature = "trace", tracing::instrument(name = "Comparable::as_value", level = "trace", parent = None, ret))] pub fn as_value<'a, 'b: 'a>( &'a self, current: &'b Value, @@ -493,6 +494,7 @@ pub struct SingularQuery { impl SingularQuery { /// Evaluate the singular query + #[cfg_attr(feature = "trace", tracing::instrument(name = "SingularQuery::eval_query", level = "trace", parent = None, ret))] pub fn eval_query<'b>(&self, current: &'b Value, root: &'b Value) -> Option<&'b Value> { let mut target = match self.kind { SingularQueryKind::Absolute => root, @@ -507,6 +509,8 @@ impl SingularQuery { } else { return None; } + } else { + return None; } } SingularQuerySegment::Index(index) => { @@ -516,6 +520,8 @@ impl SingularQuery { } else { return None; } + } else { + return None; } } } From 451e18e6f7c23168a4dfaf25cde73ce7a9bb72f0 Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Wed, 12 Jul 2023 22:46:29 -0400 Subject: [PATCH 2/5] Update Changelogs for `serde_json_path` and `serde_json_path_core` --- serde_json_path/CHANGELOG.md | 4 ++++ serde_json_path_core/CHANGELOG.md | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/serde_json_path/CHANGELOG.md b/serde_json_path/CHANGELOG.md index 07b36ac..cfbdb10 100644 --- a/serde_json_path/CHANGELOG.md +++ b/serde_json_path/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results [#50] + +[#50]: https://github.com/hiltontj/serde_json_path/pull/50 + # 0.6.1 (5 July 2023) * **documentation**: Updated links to JSONPath specification to latest version (base 14) [#43] diff --git a/serde_json_path_core/CHANGELOG.md b/serde_json_path_core/CHANGELOG.md index f0b37a7..5bfda73 100644 --- a/serde_json_path_core/CHANGELOG.md +++ b/serde_json_path_core/CHANGELOG.md @@ -7,5 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results [#50] + +[#50]: https://github.com/hiltontj/serde_json_path/pull/50 + +# 0.1.0 (2 April 2023) + Initial Release From fe410302affba88cec89305816706063fe107555 Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Wed, 12 Jul 2023 23:08:20 -0400 Subject: [PATCH 3/5] Use a more functional syntax for less branching --- .../src/spec/selector/filter.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/serde_json_path_core/src/spec/selector/filter.rs b/serde_json_path_core/src/spec/selector/filter.rs index 07a0d9d..e3e2242 100644 --- a/serde_json_path_core/src/spec/selector/filter.rs +++ b/serde_json_path_core/src/spec/selector/filter.rs @@ -503,23 +503,18 @@ impl SingularQuery { for segment in &self.segments { match segment { SingularQuerySegment::Name(name) => { - if let Some(v) = target.as_object() { - if let Some(t) = v.get(name.as_str()) { - target = t; - } else { - return None; - } + if let Some(t) = target.as_object().and_then(|o| o.get(name.as_str())) { + target = t; } else { return None; } } SingularQuerySegment::Index(index) => { - if let Some(l) = target.as_array() { - if let Some(t) = usize::try_from(index.0).ok().and_then(|i| l.get(i)) { - target = t; - } else { - return None; - } + if let Some(t) = target + .as_array() + .and_then(|l| usize::try_from(index.0).ok().and_then(|i| l.get(i))) + { + target = t; } else { return None; } From 21cb8644754918948a1afc99c46cd7f0d78faf5e Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Thu, 13 Jul 2023 06:48:33 -0400 Subject: [PATCH 4/5] Make the note in changelog clearer as it pertains to issue fixed --- serde_json_path/CHANGELOG.md | 2 +- serde_json_path_core/CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/serde_json_path/CHANGELOG.md b/serde_json_path/CHANGELOG.md index cfbdb10..2563173 100644 --- a/serde_json_path/CHANGELOG.md +++ b/serde_json_path/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results [#50] +* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results when relative singular queries, e.g., `@.foo.bar`, were being used as comparables in a filter, e.g., `$.foo[?(@bar == 'baz')]` [#50] [#50]: https://github.com/hiltontj/serde_json_path/pull/50 diff --git a/serde_json_path_core/CHANGELOG.md b/serde_json_path_core/CHANGELOG.md index 5bfda73..340efa4 100644 --- a/serde_json_path_core/CHANGELOG.md +++ b/serde_json_path_core/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results [#50] +* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results when relative singular queries, e.g., `@.foo.bar`, were being used as comparables in a filter, e.g., `$.foo[?(@bar == 'baz')]` [#50] [#50]: https://github.com/hiltontj/serde_json_path/pull/50 From 9af60b904890169904ea5ac3c7b614778ddaae57 Mon Sep 17 00:00:00 2001 From: Trevor Hilton Date: Thu, 13 Jul 2023 06:52:07 -0400 Subject: [PATCH 5/5] fix a typo in changelog --- serde_json_path/CHANGELOG.md | 2 +- serde_json_path_core/CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/serde_json_path/CHANGELOG.md b/serde_json_path/CHANGELOG.md index 2563173..31953d2 100644 --- a/serde_json_path/CHANGELOG.md +++ b/serde_json_path/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results when relative singular queries, e.g., `@.foo.bar`, were being used as comparables in a filter, e.g., `$.foo[?(@bar == 'baz')]` [#50] +* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results when relative singular queries, e.g., `@.bar`, were being used as comparables in a filter, e.g., `$.foo[?(@.bar == 'baz')]` [#50] [#50]: https://github.com/hiltontj/serde_json_path/pull/50 diff --git a/serde_json_path_core/CHANGELOG.md b/serde_json_path_core/CHANGELOG.md index 340efa4..7a44a24 100644 --- a/serde_json_path_core/CHANGELOG.md +++ b/serde_json_path_core/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results when relative singular queries, e.g., `@.foo.bar`, were being used as comparables in a filter, e.g., `$.foo[?(@bar == 'baz')]` [#50] +* **fixed**: Fixed an issue in the evaluation of `SingularQuery`s that was producing false positive query results when relative singular queries, e.g., `@.bar`, were being used as comparables in a filter, e.g., `$.foo[?(@.bar == 'baz')]` [#50] [#50]: https://github.com/hiltontj/serde_json_path/pull/50