Skip to content

Commit 0bec43e

Browse files
RUST-1899 Fix UUID string deserialization (#468)
1 parent 3fc1879 commit 0bec43e

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

Diff for: src/de/raw.rs

-9
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,6 @@ impl<'de> Deserializer<'de> {
208208
where
209209
V: serde::de::Visitor<'de>,
210210
{
211-
if let DeserializerHint::BinarySubtype(expected_st) = hint {
212-
if self.current_type != ElementType::Binary {
213-
return Err(Error::custom(format!(
214-
"expected Binary with subtype {:?}, instead got {:?}",
215-
expected_st, self.current_type
216-
)));
217-
}
218-
}
219-
220211
match self.current_type {
221212
ElementType::Int32 => visitor.visit_i32(read_i32(&mut self.bytes)?),
222213
ElementType::Int64 => visitor.visit_i64(read_i64(&mut self.bytes)?),

Diff for: src/de/serde.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -637,13 +637,12 @@ impl Deserializer {
637637

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

640-
if let DeserializerHint::BinarySubtype(expected_st) = hint {
641-
match value {
642-
Bson::Binary(ref b) if b.subtype == expected_st => {}
643-
ref b => {
640+
if let DeserializerHint::BinarySubtype(expected_subtype) = hint {
641+
if let Bson::Binary(ref binary) = value {
642+
if binary.subtype != expected_subtype {
644643
return Err(Error::custom(format!(
645-
"expected Binary with subtype {:?}, instead got {:?}",
646-
expected_st, b
644+
"expected Binary with subtype {:?}, instead got subtype {:?}",
645+
expected_subtype, binary.subtype
647646
)));
648647
}
649648
}

Diff for: src/uuid/test.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::{
2+
from_document,
3+
from_slice,
24
spec::BinarySubtype,
35
uuid::{Uuid, UuidRepresentation},
46
Binary,
@@ -269,3 +271,22 @@ fn interop_1() {
269271
let d_uuid = doc! { "uuid": uuid_uuid };
270272
assert_eq!(d_bson, d_uuid);
271273
}
274+
275+
#[test]
276+
fn deserialize_uuid_from_string() {
277+
#[derive(Deserialize)]
278+
struct UuidWrapper {
279+
uuid: Uuid,
280+
}
281+
282+
let uuid = Uuid::new();
283+
284+
let doc = doc! { "uuid": uuid.to_string() };
285+
let wrapper: UuidWrapper = from_document(doc).expect("failed to deserialize document");
286+
assert_eq!(wrapper.uuid, uuid);
287+
288+
let raw_doc = rawdoc! { "uuid": uuid.to_string() };
289+
let wrapper: UuidWrapper =
290+
from_slice(raw_doc.as_bytes()).expect("failed to deserialize raw document");
291+
assert_eq!(wrapper.uuid, uuid);
292+
}

0 commit comments

Comments
 (0)