Skip to content

Commit

Permalink
Handle null value when deserializing Option (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
s1ck authored Nov 8, 2023
1 parent 7f4f86f commit d32a07a
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lib/src/types/serde/typ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,11 @@ impl<'de> Deserializer<'de> for BoltTypeDeserializer<'de> {
where
V: Visitor<'de>,
{
visitor.visit_some(self)
if let BoltType::Null(_) = self.value {
visitor.visit_none()
} else {
visitor.visit_some(self)
}
}

fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
Expand Down Expand Up @@ -976,6 +980,30 @@ mod tests {
assert_eq!(actual, expected);
}

#[test]
fn struct_with_null_value() {
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
struct Person {
name: String,
age: Option<u8>,
}

let map = [
(BoltString::from("name"), BoltType::from("Alice")),
(BoltString::from("age"), BoltType::Null(BoltNull)),
]
.into_iter()
.collect::<BoltMap>();

let actual = map.to::<Person>().unwrap();
let expected = Person {
name: "Alice".into(),
age: None,
};

assert_eq!(actual, expected);
}

#[test]
fn tuple_struct_from_list() {
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
Expand Down

0 comments on commit d32a07a

Please sign in to comment.