Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions src/query/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,12 @@ impl Query for FilterAtom {
res
}
} else {
let struct_check = |s: &T| {
if let Some(arr) = s.as_array() {
!arr.is_empty()
} else if let Some(obj) = s.as_object() {
!obj.is_empty()
} else if let Some(str) = s.as_str() {
!str.is_empty()
} else {
true
}
};

// RFC 9535 2.3.5.2: a test expression is true when the
// nodelist it produces is non-empty. The value of the nodes
// does not take part in the decision.
let struct_presented = match res.data {
Data::Ref(v) => struct_check(v.inner),
Data::Refs(e) if e.is_empty() => false,
Data::Refs(elems) => elems.iter().map(|v| v.inner).all(struct_check),
Data::Ref(_) => true,
Data::Refs(elems) => !elems.is_empty(),
_ => false,
};

Expand Down
22 changes: 22 additions & 0 deletions src/query/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ mod tests {
);
}

#[test]
fn existence_of_empty_containers() {
let json = json!({
"a": {
"empty_array": {"b": []},
"empty_object": {"b": {}},
"empty_string": {"b": ""},
"no_b": {"c": 1}
}
});
// RFC 9535 2.3.5.2: a test expression is true when the nodelist it
// produces is non-empty, whatever the value of the nodes.
assert_eq!(
js_path("$.a[?@.b]", &json),
Ok(vec![
(&json!({"b": []}), "$['a']['empty_array']".to_string()).into(),
(&json!({"b": {}}), "$['a']['empty_object']".to_string()).into(),
(&json!({"b": ""}), "$['a']['empty_string']".to_string()).into(),
])
);
}

#[test]
fn existence_or() {
let json = json!({
Expand Down