Skip to content

Commit

Permalink
RUST-1899 Fix UUID string deserialization (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelatkinson authored Apr 1, 2024
1 parent 3fc1879 commit 0bec43e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
9 changes: 0 additions & 9 deletions src/de/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,6 @@ impl<'de> Deserializer<'de> {
where
V: serde::de::Visitor<'de>,
{
if let DeserializerHint::BinarySubtype(expected_st) = hint {
if self.current_type != ElementType::Binary {
return Err(Error::custom(format!(
"expected Binary with subtype {:?}, instead got {:?}",
expected_st, self.current_type
)));
}
}

match self.current_type {
ElementType::Int32 => visitor.visit_i32(read_i32(&mut self.bytes)?),
ElementType::Int64 => visitor.visit_i64(read_i64(&mut self.bytes)?),
Expand Down
11 changes: 5 additions & 6 deletions src/de/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,12 @@ impl Deserializer {

let is_rawbson = matches!(hint, DeserializerHint::RawBson);

if let DeserializerHint::BinarySubtype(expected_st) = hint {
match value {
Bson::Binary(ref b) if b.subtype == expected_st => {}
ref b => {
if let DeserializerHint::BinarySubtype(expected_subtype) = hint {
if let Bson::Binary(ref binary) = value {
if binary.subtype != expected_subtype {
return Err(Error::custom(format!(
"expected Binary with subtype {:?}, instead got {:?}",
expected_st, b
"expected Binary with subtype {:?}, instead got subtype {:?}",
expected_subtype, binary.subtype
)));
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/uuid/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{
from_document,
from_slice,
spec::BinarySubtype,
uuid::{Uuid, UuidRepresentation},
Binary,
Expand Down Expand Up @@ -269,3 +271,22 @@ fn interop_1() {
let d_uuid = doc! { "uuid": uuid_uuid };
assert_eq!(d_bson, d_uuid);
}

#[test]
fn deserialize_uuid_from_string() {
#[derive(Deserialize)]
struct UuidWrapper {
uuid: Uuid,
}

let uuid = Uuid::new();

let doc = doc! { "uuid": uuid.to_string() };
let wrapper: UuidWrapper = from_document(doc).expect("failed to deserialize document");
assert_eq!(wrapper.uuid, uuid);

let raw_doc = rawdoc! { "uuid": uuid.to_string() };
let wrapper: UuidWrapper =
from_slice(raw_doc.as_bytes()).expect("failed to deserialize raw document");
assert_eq!(wrapper.uuid, uuid);
}

0 comments on commit 0bec43e

Please sign in to comment.