Skip to content

RUST-1841 Allow double-valued connectionIds #1025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64>,
}

fn deserialize_connection_id<'de, D: serde::Deserializer<'de>>(
de: D,
) -> std::result::Result<Option<i64>, 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") {
Expand Down
1 change: 1 addition & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions src/test/hello.rs
Original file line number Diff line number Diff line change
@@ -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);
}