Skip to content

Commit

Permalink
Fix empty Schema::metadata deserialization error
Browse files Browse the repository at this point in the history
Hope this fixes issue apache#241
  • Loading branch information
hulunbier committed May 5, 2021
1 parent 508f25c commit 7b678de
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions arrow/src/datatypes/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Schema {
pub(crate) fields: Vec<Field>,
/// A map of key-value pairs containing additional meta data.
#[serde(skip_serializing_if = "HashMap::is_empty")]
#[serde(default)]
pub(crate) metadata: HashMap<String, String>,
}

Expand Down Expand Up @@ -335,3 +336,35 @@ struct MetadataKeyValue {
key: String,
value: String,
}

#[cfg(test)]
mod tests {
use crate::datatypes::DataType;

use super::*;

#[test]
fn test_empty_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);
}
}

0 comments on commit 7b678de

Please sign in to comment.