Skip to content
Open
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
17 changes: 17 additions & 0 deletions typify-impl/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,23 @@ mod tests {
}
}

#[test]
fn test_empty_any_of() {
// A degenerate "anyOf" with no subschemas should not panic (or hang
// in release builds) while checking for mutual exclusivity.
let schema_json = r#"
{
"title": "EmptyAnyOf",
"anyOf": []
}
"#;

let schema: RootSchema = serde_json::from_str(schema_json).unwrap();

let mut type_space = TypeSpace::default();
let _ = type_space.add_type(&schema.schema.into()).unwrap();
}

#[test]
fn test_overridden_conversion() {
let schema_json = r#"
Expand Down
19 changes: 18 additions & 1 deletion typify-impl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ pub(crate) fn all_mutually_exclusive(
definitions: &BTreeMap<RefKey, Schema>,
) -> bool {
let len = subschemas.len();
// With fewer than two subschemas there are no pairs to compare, so the
// schemas are vacuously mutually exclusive. This also avoids underflow
// in `len - 1` below when `subschemas` is empty.
if len < 2 {
return true;
}
// Consider all pairs
(0..len - 1)
.flat_map(|ii| (ii + 1..len).map(move |jj| (ii, jj)))
Expand Down Expand Up @@ -1001,7 +1007,10 @@ mod tests {
};

use crate::{
util::{decode_segment, sanitize, schemas_mutually_exclusive, Case, ReorderedInstanceType},
util::{
all_mutually_exclusive, decode_segment, sanitize, schemas_mutually_exclusive, Case,
ReorderedInstanceType,
},
Name,
};

Expand Down Expand Up @@ -1107,6 +1116,14 @@ mod tests {
assert!(schemas_mutually_exclusive(&b, &a, &BTreeMap::new()));
}

#[test]
fn test_all_mutually_exclusive_empty() {
// An empty slice of subschemas has no pairs to compare, so it's
// vacuously true. This should not panic (previously `len - 1`
// underflowed for an empty slice).
assert!(all_mutually_exclusive(&[], &BTreeMap::new()));
}

#[test]
fn test_decode_segment() {
assert_eq!(decode_segment("foo~1bar"), "foo/bar");
Expand Down