Skip to content

Commit 2322c42

Browse files
authored
fixing the find or delete serde inconsistency (#948)
1 parent 85de932 commit 2322c42

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ time = "0.3.9"
195195
tokio = { version = ">= 0.0.0", features = ["fs"] }
196196
tracing-subscriber = "0.3.16"
197197
regex = "1.6.0"
198+
serde-hex = "0.1.0"
198199

199200
[package.metadata.docs.rs]
200201
rustdoc-args = ["--cfg", "docsrs"]
@@ -210,4 +211,4 @@ features = [
210211
"aws-auth",
211212
"tracing-unstable",
212213
"in-use-encryption-unstable"
213-
]
214+
]

src/operation/find_and_modify.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ mod options;
22

33
use std::fmt::Debug;
44

5+
use bson::{from_slice, RawBson};
56
use serde::{de::DeserializeOwned, Deserialize, Serialize};
67

78
use self::options::FindAndModifyOptions;
89
use crate::{
9-
bson::{doc, from_document, rawdoc, Bson, Document, RawDocumentBuf},
10+
bson::{doc, rawdoc, Document, RawDocumentBuf},
1011
bson_util,
1112
cmap::{Command, RawCommandResponse, StreamDescription},
1213
coll::{
@@ -146,11 +147,15 @@ impl<'a, R: Serialize, T: DeserializeOwned> OperationWithDefaults for FindAndMod
146147
response: RawCommandResponse,
147148
_description: &StreamDescription,
148149
) -> Result<Self::O> {
150+
#[derive(Debug, Deserialize)]
151+
pub(crate) struct Response {
152+
value: RawBson,
153+
}
149154
let response: Response = response.body()?;
150155

151156
match response.value {
152-
Bson::Document(doc) => Ok(Some(from_document(doc)?)),
153-
Bson::Null => Ok(None),
157+
RawBson::Document(doc) => Ok(Some(from_slice(doc.as_bytes())?)),
158+
RawBson::Null => Ok(None),
154159
other => Err(ErrorKind::InvalidResponse {
155160
message: format!(
156161
"expected document for value field of findAndModify response, but instead got \
@@ -170,8 +175,3 @@ impl<'a, R: Serialize, T: DeserializeOwned> OperationWithDefaults for FindAndMod
170175
Retryability::Write
171176
}
172177
}
173-
174-
#[derive(Debug, Deserialize)]
175-
pub(crate) struct Response {
176-
value: Bson,
177-
}

src/test/client.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{borrow::Cow, collections::HashMap, sync::Arc, time::Duration};
22

33
use bson::Document;
4-
use serde::Deserialize;
4+
use serde::{Deserialize, Serialize};
55

66
use crate::{
77
bson::{doc, Bson},
@@ -940,6 +940,39 @@ async fn manual_shutdown_immediate_with_resources() {
940940
assert!(events.get_command_started_events(&["delete"]).is_empty());
941941
}
942942

943+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
944+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
945+
async fn find_one_and_delete_serde_consistency() {
946+
let client = Client::test_builder().build().await;
947+
948+
let coll = client
949+
.database("find_one_and_delete_serde_consistency")
950+
.collection("test");
951+
952+
#[derive(Debug, Serialize, Deserialize)]
953+
struct Foo {
954+
#[serde(with = "serde_hex::SerHexSeq::<serde_hex::StrictPfx>")]
955+
problematic: Vec<u8>,
956+
}
957+
958+
let doc = Foo {
959+
problematic: vec![0, 1, 2, 3, 4, 5, 6, 7],
960+
};
961+
962+
coll.insert_one(&doc, None).await.unwrap();
963+
let rec: Foo = coll.find_one(doc! {}, None).await.unwrap().unwrap();
964+
assert_eq!(doc.problematic, rec.problematic);
965+
let rec: Foo = coll
966+
.find_one_and_delete(doc! {}, None)
967+
.await
968+
.unwrap()
969+
.unwrap();
970+
assert_eq!(doc.problematic, rec.problematic);
971+
972+
let nothing = coll.find_one_and_delete(doc! {}, None).await.unwrap();
973+
assert!(nothing.is_none());
974+
}
975+
943976
// Verifies that `Client::warm_connection_pool` succeeds.
944977
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
945978
#[cfg_attr(feature = "async-std-runtime", async_std::test)]

0 commit comments

Comments
 (0)