Skip to content

Commit c8f4e5f

Browse files
authored
RUST-1607: allow uuidRepresentation in connection string (#838)
1 parent 5d28084 commit c8f4e5f

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/client/options/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::{
1515
time::Duration,
1616
};
1717

18+
use bson::UuidRepresentation;
1819
use derivative::Derivative;
1920
use lazy_static::lazy_static;
2021
use serde::{de::Unexpected, Deserialize, Deserializer, Serialize};
@@ -74,6 +75,7 @@ const URI_OPTIONS: &[&str] = &[
7475
"tlsallowinvalidcertificates",
7576
"tlscafile",
7677
"tlscertificatekeyfile",
78+
"uuidRepresentation",
7779
"w",
7880
"waitqueuetimeoutms",
7981
"wtimeoutms",
@@ -813,6 +815,12 @@ pub struct ConnectionString {
813815
/// Default read preference for the client.
814816
pub read_preference: Option<ReadPreference>,
815817

818+
/// The [`UuidRepresentation`] to use when decoding [`Binary`](bson::Binary) values with the
819+
/// [`UuidOld`](bson::spec::BinarySubtype::UuidOld) subtype. This is not used by the
820+
/// driver; client code can use this when deserializing relevant values with
821+
/// [`Binary::to_uuid_with_representation`](bson::binary::Binary::to_uuid_with_representation).
822+
pub uuid_representation: Option<UuidRepresentation>,
823+
816824
wait_queue_timeout: Option<Duration>,
817825
tls_insecure: Option<bool>,
818826

@@ -2115,6 +2123,22 @@ impl ConnectionString {
21152123
))
21162124
}
21172125
},
2126+
"uuidrepresentation" => match value.to_lowercase().as_str() {
2127+
"csharplegacy" => self.uuid_representation = Some(UuidRepresentation::CSharpLegacy),
2128+
"javalegacy" => self.uuid_representation = Some(UuidRepresentation::JavaLegacy),
2129+
"pythonlegacy" => self.uuid_representation = Some(UuidRepresentation::PythonLegacy),
2130+
_ => {
2131+
return Err(ErrorKind::InvalidArgument {
2132+
message: format!(
2133+
"connection string `uuidRepresentation` option can be one of \
2134+
`csharpLegacy`, `javaLegacy`, or `pythonLegacy`. Received invalid \
2135+
`{}`",
2136+
value
2137+
),
2138+
}
2139+
.into())
2140+
}
2141+
},
21182142
"w" => {
21192143
let mut write_concern = self.write_concern.get_or_insert_with(Default::default);
21202144

src/client/options/test.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::time::Duration;
22

3+
use bson::UuidRepresentation;
34
use pretty_assertions::assert_eq;
45
use serde::Deserialize;
56

@@ -218,6 +219,47 @@ async fn parse_uri(option: &str, suggestion: Option<&str>) {
218219
}
219220
}
220221

222+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
223+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
224+
async fn uuid_representations() {
225+
let mut uuid_repr = parse_uri_with_uuid_representation("csharpLegacy")
226+
.await
227+
.expect("expected `csharpLegacy` to be a valid argument for `uuidRepresentation`");
228+
assert_eq!(UuidRepresentation::CSharpLegacy, uuid_repr);
229+
230+
uuid_repr = parse_uri_with_uuid_representation("javaLegacy")
231+
.await
232+
.expect("expected `javaLegacy` to be a valid argument for `uuidRepresentation`");
233+
assert_eq!(UuidRepresentation::JavaLegacy, uuid_repr);
234+
235+
uuid_repr = parse_uri_with_uuid_representation("pythonLegacy")
236+
.await
237+
.expect("expected `pythonLegacy` to be a valid argument for `uuidRepresentation`");
238+
assert_eq!(UuidRepresentation::PythonLegacy, uuid_repr);
239+
240+
let uuid_err = parse_uri_with_uuid_representation("unknownLegacy")
241+
.await
242+
.expect_err("expect `unknownLegacy` to be an invalid argument for `uuidRepresentation`");
243+
assert_eq!(
244+
"connection string `uuidRepresentation` option can be one of `csharpLegacy`, \
245+
`javaLegacy`, or `pythonLegacy`. Received invalid `unknownLegacy`"
246+
.to_string(),
247+
uuid_err
248+
);
249+
}
250+
251+
async fn parse_uri_with_uuid_representation(uuid_repr: &str) -> Result<UuidRepresentation, String> {
252+
match ConnectionString::parse(format!(
253+
"mongodb://localhost:27017/?uuidRepresentation={}",
254+
uuid_repr
255+
))
256+
.map_err(|e| e.message().unwrap())
257+
{
258+
Ok(cs) => Ok(cs.uuid_representation.unwrap()),
259+
Err(e) => Err(e),
260+
}
261+
}
262+
221263
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
222264
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
223265
async fn parse_unknown_options() {

0 commit comments

Comments
 (0)