Skip to content

fixing the find or delete serde inconsistency (#948) #955

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 1 commit into from
Sep 18, 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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ time = "0.3.9"
tokio = { version = ">= 0.0.0", features = ["fs"] }
tracing-subscriber = "0.3.16"
regex = "1.6.0"
serde-hex = "0.1.0"

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
Expand All @@ -210,4 +211,4 @@ features = [
"aws-auth",
"tracing-unstable",
"in-use-encryption-unstable"
]
]
16 changes: 8 additions & 8 deletions src/operation/find_and_modify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ mod test;

use std::fmt::Debug;

use bson::{from_slice, RawBson};
use serde::{de::DeserializeOwned, Deserialize};

use self::options::FindAndModifyOptions;
use crate::{
bson::{doc, from_document, Bson, Document},
bson::{doc, Document},
bson_util,
cmap::{Command, RawCommandResponse, StreamDescription},
coll::{
Expand Down Expand Up @@ -133,11 +134,15 @@ where
response: RawCommandResponse,
_description: &StreamDescription,
) -> Result<Self::O> {
#[derive(Debug, Deserialize)]
pub(crate) struct Response {
value: RawBson,
}
let response: Response = response.body()?;

match response.value {
Bson::Document(doc) => Ok(Some(from_document(doc)?)),
Bson::Null => Ok(None),
RawBson::Document(doc) => Ok(Some(from_slice(doc.as_bytes())?)),
RawBson::Null => Ok(None),
other => Err(ErrorKind::InvalidResponse {
message: format!(
"expected document for value field of findAndModify response, but instead got \
Expand All @@ -157,8 +162,3 @@ where
Retryability::Write
}
}

#[derive(Debug, Deserialize)]
pub(crate) struct Response {
value: Bson,
}
35 changes: 34 additions & 1 deletion src/test/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, collections::HashMap, sync::Arc, time::Duration};

use bson::Document;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use tokio::sync::{RwLockReadGuard, RwLockWriteGuard};

use crate::{
Expand Down Expand Up @@ -976,6 +976,39 @@ async fn manual_shutdown_immediate_with_resources() {
assert!(events.get_command_started_events(&["delete"]).is_empty());
}

#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
async fn find_one_and_delete_serde_consistency() {
let client = Client::test_builder().build().await;

let coll = client
.database("find_one_and_delete_serde_consistency")
.collection("test");

#[derive(Debug, Serialize, Deserialize)]
struct Foo {
#[serde(with = "serde_hex::SerHexSeq::<serde_hex::StrictPfx>")]
problematic: Vec<u8>,
}

let doc = Foo {
problematic: vec![0, 1, 2, 3, 4, 5, 6, 7],
};

coll.insert_one(&doc, None).await.unwrap();
let rec: Foo = coll.find_one(doc! {}, None).await.unwrap().unwrap();
assert_eq!(doc.problematic, rec.problematic);
let rec: Foo = coll
.find_one_and_delete(doc! {}, None)
.await
.unwrap()
.unwrap();
assert_eq!(doc.problematic, rec.problematic);

let nothing = coll.find_one_and_delete(doc! {}, None).await.unwrap();
assert!(nothing.is_none());
}

// Verifies that `Client::warm_connection_pool` succeeds.
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
Expand Down