From ba0330dbdf0a02ef576f512eddeefe5e676a740f Mon Sep 17 00:00:00 2001 From: Abraham Egnor Date: Wed, 27 Aug 2025 12:10:39 +0100 Subject: [PATCH 1/3] RUST-2184 Accept any BSON number for CreateCollectionOptions::size --- src/db/options.rs | 5 ++++- src/serde_util.rs | 15 +++++++++++++++ src/test/db.rs | 10 ++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/db/options.rs b/src/db/options.rs index ee987ce28..ba3597fdf 100644 --- a/src/db/options.rs +++ b/src/db/options.rs @@ -44,7 +44,10 @@ pub struct CreateCollectionOptions { /// The maximum size (in bytes) for a capped collection. This option is ignored if `capped` is /// not set to true. - #[serde(serialize_with = "serde_util::serialize_u64_option_as_i64")] + #[serde( + serialize_with = "serde_util::serialize_u64_option_as_i64", + deserialize_with = "serde_util::deserialize_option_u64_from_bson_number" + )] pub size: Option, /// The maximum number of documents in a capped collection. The `size` limit takes precedence diff --git a/src/serde_util.rs b/src/serde_util.rs index d3dcda433..b8046bd37 100644 --- a/src/serde_util.rs +++ b/src/serde_util.rs @@ -118,6 +118,21 @@ where .ok_or_else(|| serde::de::Error::custom(format!("could not deserialize u64 from {bson:?}"))) } +pub(crate) fn deserialize_option_u64_from_bson_number<'de, D>( + deserializer: D, +) -> std::result::Result, D::Error> +where + D: Deserializer<'de>, +{ + Option::::deserialize(deserializer)? + .map(|bson| { + get_u64(&bson).ok_or_else(|| { + serde::de::Error::custom(format!("could not deserialize u64 from {bson:?}")) + }) + }) + .transpose() +} + pub(crate) fn serialize_error_as_string( val: &Error, serializer: S, diff --git a/src/test/db.rs b/src/test/db.rs index b13df1abc..bcd6df837 100644 --- a/src/test/db.rs +++ b/src/test/db.rs @@ -468,3 +468,13 @@ async fn test_run_command() { assert_eq!(v[0].as_ref().unwrap().get_str("foo").unwrap(), "bar"); } } + +#[test] +fn create_collection_options_deserialize() { + let source = doc! { + "capped": true, + "size": 5253511168.0, + "autoIndexId": false, + }; + let _: CreateCollectionOptions = crate::bson::deserialize_from_document(source).unwrap(); +} From 25c2a57e606de9d552ea99ce5ac86c9ff39d5c84 Mon Sep 17 00:00:00 2001 From: Abraham Egnor Date: Wed, 27 Aug 2025 14:17:29 +0100 Subject: [PATCH 2/3] compat --- src/test/db.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/db.rs b/src/test/db.rs index bcd6df837..c7efccaed 100644 --- a/src/test/db.rs +++ b/src/test/db.rs @@ -476,5 +476,5 @@ fn create_collection_options_deserialize() { "size": 5253511168.0, "autoIndexId": false, }; - let _: CreateCollectionOptions = crate::bson::deserialize_from_document(source).unwrap(); + let _: CreateCollectionOptions = crate::bson_compat::deserialize_from_document(source).unwrap(); } From 54fb69575eadac85d91cc79df07a4dbbc46ae1ab Mon Sep 17 00:00:00 2001 From: Abraham Egnor Date: Wed, 27 Aug 2025 14:43:35 +0100 Subject: [PATCH 3/3] default --- src/db/options.rs | 3 ++- src/test/db.rs | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/db/options.rs b/src/db/options.rs index ba3597fdf..03848e841 100644 --- a/src/db/options.rs +++ b/src/db/options.rs @@ -46,7 +46,8 @@ pub struct CreateCollectionOptions { /// not set to true. #[serde( serialize_with = "serde_util::serialize_u64_option_as_i64", - deserialize_with = "serde_util::deserialize_option_u64_from_bson_number" + deserialize_with = "serde_util::deserialize_option_u64_from_bson_number", + default )] pub size: Option, diff --git a/src/test/db.rs b/src/test/db.rs index c7efccaed..3b4a25213 100644 --- a/src/test/db.rs +++ b/src/test/db.rs @@ -477,4 +477,9 @@ fn create_collection_options_deserialize() { "autoIndexId": false, }; let _: CreateCollectionOptions = crate::bson_compat::deserialize_from_document(source).unwrap(); + let source = doc! { + "capped": true, + "autoIndexId": false, + }; + let _: CreateCollectionOptions = crate::bson_compat::deserialize_from_document(source).unwrap(); }