Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle null enumerated values properly #83

Merged
merged 1 commit into from
Oct 21, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.0.0-rc.1 (2023-10-21)

- Fix handling of null values in `enum` for string, number, integer, and boolean

## 2.0.0-rc.0 (2023-10-21)

- Bump `indexmap` dependency to 2.0.0
Expand Down
33 changes: 31 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ where
F: Fn(&serde_json::Value) -> bool,
{
match enumeration {
Some(values) => values.iter().all(check),
Some(values) => values.iter().all(|value| value.is_null() || check(value)),
None => true,
}
}
Expand Down Expand Up @@ -796,7 +796,9 @@ impl FromStr for StringFormat {
mod tests {
use serde_json::json;

use crate::{AnySchema, Schema, SchemaData, SchemaKind};
use crate::{
AnySchema, Schema, SchemaData, SchemaKind, StringType, Type, VariantOrUnknownOrEmpty,
};

#[test]
fn test_schema_with_extensions() {
Expand Down Expand Up @@ -890,4 +892,31 @@ mod tests {
_ => panic!("incorrect kind {:#?}", schema),
}
}

#[test]
fn test_enum_with_null() {
let value = json! {
{
"type": "string",
"nullable": true,
"enum": [ null, "howdy" ]
}
};

let schema = serde_json::from_value::<Schema>(value).unwrap();
assert!(schema.schema_data.nullable);

match schema.schema_kind {
SchemaKind::Type(Type::String(StringType {
format: VariantOrUnknownOrEmpty::Empty,
pattern: None,
enumeration,
min_length: None,
max_length: None,
})) => {
assert_eq!(enumeration, vec![None, Some("howdy".to_string())]);
}
_ => panic!("incorrect kind {:#?}", schema),
}
}
}
Loading