diff --git a/arrow/src/datatypes/schema.rs b/arrow/src/datatypes/schema.rs index ad89b29cacda..cfc0744954b1 100644 --- a/arrow/src/datatypes/schema.rs +++ b/arrow/src/datatypes/schema.rs @@ -35,6 +35,7 @@ pub struct Schema { pub(crate) fields: Vec, /// A map of key-value pairs containing additional meta data. #[serde(skip_serializing_if = "HashMap::is_empty")] + #[serde(default)] pub(crate) metadata: HashMap, } @@ -335,3 +336,35 @@ struct MetadataKeyValue { key: String, value: String, } + +#[cfg(test)] +mod tests { + use crate::datatypes::DataType; + + use super::*; + + #[test] + fn test_ser_de_metadata() { + // ser/de with empty metadata + let mut schema = Schema::new(vec![ + Field::new("name", DataType::Utf8, false), + Field::new("address", DataType::Utf8, false), + Field::new("priority", DataType::UInt8, false), + ]); + + let json = serde_json::to_string(&schema).unwrap(); + let de_schema = serde_json::from_str(&json).unwrap(); + + assert_eq!(schema, de_schema); + + // ser/de with non-empty metadata + schema.metadata = [("key".to_owned(), "val".to_owned())] + .iter() + .cloned() + .collect(); + let json = serde_json::to_string(&schema).unwrap(); + let de_schema = serde_json::from_str(&json).unwrap(); + + assert_eq!(schema, de_schema); + } +}