Skip to content

Commit ce26e04

Browse files
authored
RUST-1841 Allow double-valued connectionIds (#1025)
1 parent a264462 commit ce26e04

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/hello.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,27 @@ pub(crate) struct HelloCommandResponse {
179179

180180
/// The server-generated ID for the connection the "hello" command was run on.
181181
/// Present on server versions 4.2+.
182+
#[serde(deserialize_with = "deserialize_connection_id", default)]
182183
pub connection_id: Option<i64>,
183184
}
184185

186+
fn deserialize_connection_id<'de, D: serde::Deserializer<'de>>(
187+
de: D,
188+
) -> std::result::Result<Option<i64>, D::Error> {
189+
#[derive(Deserialize)]
190+
#[serde(untagged)]
191+
enum Helper {
192+
Int32(i32),
193+
Int64(i64),
194+
Double(f64),
195+
}
196+
Ok(Some(match Helper::deserialize(de)? {
197+
Helper::Int32(v) => v as i64,
198+
Helper::Int64(v) => v,
199+
Helper::Double(v) => v as i64,
200+
}))
201+
}
202+
185203
impl HelloCommandResponse {
186204
pub(crate) fn server_type(&self) -> ServerType {
187205
if self.msg.as_deref() == Some("isdbgrid") {

src/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mod cursor;
1212
mod db;
1313
#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
1414
mod documentation_examples;
15+
mod hello;
1516
mod index_management;
1617
#[cfg(all(not(feature = "sync"), not(feature = "tokio-sync")))]
1718
mod lambda_examples;

src/test/hello.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use bson::{doc, Bson};
2+
3+
use crate::hello::HelloCommandResponse;
4+
5+
#[test]
6+
fn parse_connection_id() {
7+
let mut parsed: HelloCommandResponse = bson::from_document(doc! {
8+
"connectionId": Bson::Int32(42),
9+
"maxBsonObjectSize": 0,
10+
"maxMessageSizeBytes": 0,
11+
})
12+
.unwrap();
13+
assert_eq!(parsed.connection_id, Some(42));
14+
15+
parsed = bson::from_document(doc! {
16+
"connectionId": Bson::Int64(13),
17+
"maxBsonObjectSize": 0,
18+
"maxMessageSizeBytes": 0,
19+
})
20+
.unwrap();
21+
assert_eq!(parsed.connection_id, Some(13));
22+
23+
parsed = bson::from_document(doc! {
24+
"connectionId": Bson::Double(1066.0),
25+
"maxBsonObjectSize": 0,
26+
"maxMessageSizeBytes": 0,
27+
})
28+
.unwrap();
29+
assert_eq!(parsed.connection_id, Some(1066));
30+
31+
parsed = bson::from_document(doc! {
32+
"maxBsonObjectSize": 0,
33+
"maxMessageSizeBytes": 0,
34+
})
35+
.unwrap();
36+
assert_eq!(parsed.connection_id, None);
37+
}

0 commit comments

Comments
 (0)