Skip to content

RUST-1607: allow uuidRepresentation in connection string #838

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 6 commits into from
Mar 13, 2023
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
24 changes: 24 additions & 0 deletions src/client/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
time::Duration,
};

use bson::UuidRepresentation;
use derivative::Derivative;
use lazy_static::lazy_static;
use serde::{de::Unexpected, Deserialize, Deserializer, Serialize};
Expand Down Expand Up @@ -74,6 +75,7 @@ const URI_OPTIONS: &[&str] = &[
"tlsallowinvalidcertificates",
"tlscafile",
"tlscertificatekeyfile",
"uuidRepresentation",
"w",
"waitqueuetimeoutms",
"wtimeoutms",
Expand Down Expand Up @@ -813,6 +815,12 @@ pub struct ConnectionString {
/// Default read preference for the client.
pub read_preference: Option<ReadPreference>,

/// The [`UuidRepresentation`] to use when decoding [`Binary`](bson::Binary) values with the
/// [`UuidOld`](bson::spec::BinarySubtype::UuidOld) subtype. This is not used by the
/// driver; client code can use this when deserializing relevant values with
/// [`Binary::to_uuid_with_representation`](bson::binary::Binary::to_uuid_with_representation).
pub uuid_representation: Option<UuidRepresentation>,

wait_queue_timeout: Option<Duration>,
tls_insecure: Option<bool>,

Expand Down Expand Up @@ -2115,6 +2123,22 @@ impl ConnectionString {
))
}
},
"uuidrepresentation" => match value.to_lowercase().as_str() {
"csharplegacy" => self.uuid_representation = Some(UuidRepresentation::CSharpLegacy),
"javalegacy" => self.uuid_representation = Some(UuidRepresentation::JavaLegacy),
"pythonlegacy" => self.uuid_representation = Some(UuidRepresentation::PythonLegacy),
_ => {
return Err(ErrorKind::InvalidArgument {
message: format!(
"connection string `uuidRepresentation` option can be one of \
`csharpLegacy`, `javaLegacy`, or `pythonLegacy`. Received invalid \
`{}`",
value
),
}
.into())
}
},
"w" => {
let mut write_concern = self.write_concern.get_or_insert_with(Default::default);

Expand Down
42 changes: 42 additions & 0 deletions src/client/options/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use bson::UuidRepresentation;
use pretty_assertions::assert_eq;
use serde::Deserialize;

Expand Down Expand Up @@ -218,6 +219,47 @@ async fn parse_uri(option: &str, suggestion: Option<&str>) {
}
}

#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
async fn uuid_representations() {
let mut uuid_repr = parse_uri_with_uuid_representation("csharpLegacy")
.await
.expect("expected `csharpLegacy` to be a valid argument for `uuidRepresentation`");
assert_eq!(UuidRepresentation::CSharpLegacy, uuid_repr);

uuid_repr = parse_uri_with_uuid_representation("javaLegacy")
.await
.expect("expected `javaLegacy` to be a valid argument for `uuidRepresentation`");
assert_eq!(UuidRepresentation::JavaLegacy, uuid_repr);

uuid_repr = parse_uri_with_uuid_representation("pythonLegacy")
.await
.expect("expected `pythonLegacy` to be a valid argument for `uuidRepresentation`");
assert_eq!(UuidRepresentation::PythonLegacy, uuid_repr);

let uuid_err = parse_uri_with_uuid_representation("unknownLegacy")
.await
.expect_err("expect `unknownLegacy` to be an invalid argument for `uuidRepresentation`");
assert_eq!(
"connection string `uuidRepresentation` option can be one of `csharpLegacy`, \
`javaLegacy`, or `pythonLegacy`. Received invalid `unknownLegacy`"
.to_string(),
uuid_err
);
}

async fn parse_uri_with_uuid_representation(uuid_repr: &str) -> Result<UuidRepresentation, String> {
match ConnectionString::parse(format!(
"mongodb://localhost:27017/?uuidRepresentation={}",
uuid_repr
))
.map_err(|e| e.message().unwrap())
{
Ok(cs) => Ok(cs.uuid_representation.unwrap()),
Err(e) => Err(e),
}
}

#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
async fn parse_unknown_options() {
Expand Down