diff --git a/src/hello.rs b/src/hello.rs index 37229ec5b..1c1c14348 100644 --- a/src/hello.rs +++ b/src/hello.rs @@ -179,9 +179,27 @@ pub(crate) struct HelloCommandResponse { /// The server-generated ID for the connection the "hello" command was run on. /// Present on server versions 4.2+. + #[serde(deserialize_with = "deserialize_connection_id", default)] pub connection_id: Option, } +fn deserialize_connection_id<'de, D: serde::Deserializer<'de>>( + de: D, +) -> std::result::Result, D::Error> { + #[derive(Deserialize)] + #[serde(untagged)] + enum Helper { + Int32(i32), + Int64(i64), + Double(f64), + } + Ok(Some(match Helper::deserialize(de)? { + Helper::Int32(v) => v as i64, + Helper::Int64(v) => v, + Helper::Double(v) => v as i64, + })) +} + impl HelloCommandResponse { pub(crate) fn server_type(&self) -> ServerType { if self.msg.as_deref() == Some("isdbgrid") { diff --git a/src/test.rs b/src/test.rs index 378ddea6e..dec6e3388 100644 --- a/src/test.rs +++ b/src/test.rs @@ -12,6 +12,7 @@ mod cursor; mod db; #[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))] mod documentation_examples; +mod hello; mod index_management; #[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))] mod lambda_examples; diff --git a/src/test/hello.rs b/src/test/hello.rs new file mode 100644 index 000000000..2b21c6562 --- /dev/null +++ b/src/test/hello.rs @@ -0,0 +1,37 @@ +use bson::{doc, Bson}; + +use crate::hello::HelloCommandResponse; + +#[test] +fn parse_connection_id() { + let mut parsed: HelloCommandResponse = bson::from_document(doc! { + "connectionId": Bson::Int32(42), + "maxBsonObjectSize": 0, + "maxMessageSizeBytes": 0, + }) + .unwrap(); + assert_eq!(parsed.connection_id, Some(42)); + + parsed = bson::from_document(doc! { + "connectionId": Bson::Int64(13), + "maxBsonObjectSize": 0, + "maxMessageSizeBytes": 0, + }) + .unwrap(); + assert_eq!(parsed.connection_id, Some(13)); + + parsed = bson::from_document(doc! { + "connectionId": Bson::Double(1066.0), + "maxBsonObjectSize": 0, + "maxMessageSizeBytes": 0, + }) + .unwrap(); + assert_eq!(parsed.connection_id, Some(1066)); + + parsed = bson::from_document(doc! { + "maxBsonObjectSize": 0, + "maxMessageSizeBytes": 0, + }) + .unwrap(); + assert_eq!(parsed.connection_id, None); +}