Skip to content

Commit

Permalink
handle null enumerated values properly (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahl authored Oct 21, 2023
1 parent b500ca1 commit 039175d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
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),
}
}
}

0 comments on commit 039175d

Please sign in to comment.