jmap: deserialize `null` to empty vec for messageId

The spec says MessageId can be `null`, handle that case and deserialize
it to an empty Vec.
pull/182/head
Johannes Schilling 2023-03-05 22:02:07 +01:00 committed by Manos Pitsidianakis
parent 256a3e252e
commit fbc1007ff4
1 changed files with 15 additions and 1 deletions

View File

@ -154,7 +154,7 @@ pub struct EmailObject {
pub size: u64,
#[serde(default)]
pub received_at: String,
#[serde(default)]
#[serde(default, deserialize_with = "deserialize_none_default")]
pub message_id: Vec<String>,
#[serde(default)]
pub to: Option<SmallVec<[EmailAddress; 1]>>,
@ -199,6 +199,20 @@ pub struct EmailObject {
pub extra: HashMap<String, Value>,
}
/// Deserializer that uses `Default::default()` in place of a present but `null`
/// value. Note that `serde(default)` doesn't apply if the key is present but
/// has a value of `null`.
fn deserialize_none_default<'de, D, T>(
deserializer: D,
) -> std::result::Result<T, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de> + Default,
{
let v = Option::<T>::deserialize(deserializer)?;
Ok(v.unwrap_or(T::default()))
}
impl EmailObject {
_impl!(get keywords, keywords: HashMap<String, bool>);
}