Skip to content

RUST-1826 Use serde attribute to remove empty write concerns #1392

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 4 commits into from
Jun 10, 2025
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 src/client/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::{
options::ReadConcernLevel,
sdam::{verify_max_staleness, DEFAULT_HEARTBEAT_FREQUENCY, MIN_HEARTBEAT_FREQUENCY},
selection_criteria::{ReadPreference, SelectionCriteria, TagSet},
serde_util,
serde_util::{self, write_concern_is_empty},
srv::{OriginalSrvInfo, SrvResolver},
};

Expand Down Expand Up @@ -2670,6 +2670,7 @@ pub struct TransactionOptions {

/// The write concern to use when committing or aborting a transaction.
#[builder(default)]
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// The selection criteria to use for all read operations in a transaction.
Expand Down
3 changes: 2 additions & 1 deletion src/client/options/bulk_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
bson_util::{get_or_prepend_id_field, replacement_document_check, update_document_check},
error::Result,
options::{UpdateModifications, WriteConcern},
serde_util::serialize_bool_or_true,
serde_util::{serialize_bool_or_true, write_concern_is_empty},
Collection,
Namespace,
};
Expand Down Expand Up @@ -48,6 +48,7 @@ pub struct BulkWriteOptions {
pub let_vars: Option<Document>,

/// The write concern to use for this operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,
}

Expand Down
10 changes: 10 additions & 0 deletions src/coll/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub struct InsertOneOptions {
pub bypass_document_validation: Option<bool>,

/// The write concern for the operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
Expand Down Expand Up @@ -295,6 +296,7 @@ pub struct ReplaceOptions {
pub hint: Option<Hint>,

/// The write concern for the operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// Map of parameter names and values. Values must be constant or closed
Expand Down Expand Up @@ -335,6 +337,7 @@ pub struct DeleteOptions {
pub collation: Option<Collation>,

/// The write concern for the operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// The index to use for the operation.
Expand Down Expand Up @@ -379,6 +382,7 @@ pub struct FindOneAndDeleteOptions {
pub sort: Option<Document>,

/// The level of the write concern
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// The collation to use for the operation.
Expand Down Expand Up @@ -438,6 +442,7 @@ pub struct FindOneAndReplaceOptions {
pub upsert: Option<bool>,

/// The level of the write concern
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// The collation to use for the operation.
Expand Down Expand Up @@ -503,6 +508,7 @@ pub struct FindOneAndUpdateOptions {
pub upsert: Option<bool>,

/// The level of the write concern
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// The collation to use for the operation.
Expand Down Expand Up @@ -615,6 +621,7 @@ pub struct AggregateOptions {
///
/// If none is specified, the write concern defined on the object executing this operation will
/// be used.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// A document with any amount of parameter names, each followed by definitions of constants in
Expand Down Expand Up @@ -1056,6 +1063,7 @@ pub struct CreateIndexOptions {
pub max_time: Option<Duration>,

/// The write concern for the operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
Expand All @@ -1075,6 +1083,7 @@ pub struct CreateIndexOptions {
#[export_tokens]
pub struct DropCollectionOptions {
/// The write concern for the operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// Map of encrypted fields for the collection.
Expand Down Expand Up @@ -1108,6 +1117,7 @@ pub struct DropIndexOptions {
pub max_time: Option<Duration>,

/// The write concern for the operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
Expand Down
4 changes: 3 additions & 1 deletion src/db/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
concern::{ReadConcern, WriteConcern},
options::{Collation, CursorType},
selection_criteria::SelectionCriteria,
serde_util,
serde_util::{self, write_concern_is_empty},
};

/// These are the valid options for creating a [`Database`](../struct.Database.html) with
Expand Down Expand Up @@ -84,6 +84,7 @@ pub struct CreateCollectionOptions {
pub collation: Option<Collation>,

/// The write concern for the operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,

/// The default configuration for indexes created on this collection, including the _id index.
Expand Down Expand Up @@ -288,6 +289,7 @@ pub enum TimeseriesGranularity {
#[export_tokens]
pub struct DropDatabaseOptions {
/// The write concern for the operation.
#[serde(skip_serializing_if = "write_concern_is_empty")]
pub write_concern: Option<WriteConcern>,
}

Expand Down
14 changes: 0 additions & 14 deletions src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,17 +531,3 @@ where
Ok(SingleCursorResult(full_body.cursor.first_batch.pop()))
}
}

macro_rules! remove_empty_write_concern {
($opts:expr) => {
if let Some(ref mut options) = $opts {
if let Some(ref write_concern) = options.write_concern {
if write_concern.is_empty() {
options.write_concern = None;
}
}
}
};
}

pub(crate) use remove_empty_write_concern;
3 changes: 1 addition & 2 deletions src/operation/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
cmap::{Command, RawCommandResponse, StreamDescription},
cursor::CursorSpecification,
error::Result,
operation::{append_options, remove_empty_write_concern, Retryability},
operation::{append_options, Retryability},
options::{AggregateOptions, ReadPreference, SelectionCriteria, WriteConcern},
Namespace,
};
Expand Down Expand Up @@ -55,7 +55,6 @@ impl OperationWithDefaults for Aggregate {
"cursor": {}
};

remove_empty_write_concern!(self.options);
append_options(&mut body, self.options.as_ref())?;

if self.is_out_or_merge() {
Expand Down
8 changes: 1 addition & 7 deletions src/operation/commit_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ use crate::bson::rawdoc;
use crate::{
cmap::{Command, RawCommandResponse, StreamDescription},
error::Result,
operation::{
append_options_to_raw_document,
remove_empty_write_concern,
OperationWithDefaults,
Retryability,
},
operation::{append_options_to_raw_document, OperationWithDefaults, Retryability},
options::{Acknowledgment, TransactionOptions, WriteConcern},
};

Expand All @@ -36,7 +31,6 @@ impl OperationWithDefaults for CommitTransaction {
Self::NAME: 1,
};

remove_empty_write_concern!(self.options);
append_options_to_raw_document(&mut body, self.options.as_ref())?;

Ok(Command::new(
Expand Down
8 changes: 1 addition & 7 deletions src/operation/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ use crate::bson::rawdoc;
use crate::{
cmap::{Command, RawCommandResponse, StreamDescription},
error::Result,
operation::{
append_options_to_raw_document,
remove_empty_write_concern,
OperationWithDefaults,
WriteConcernOnlyBody,
},
operation::{append_options_to_raw_document, OperationWithDefaults, WriteConcernOnlyBody},
options::{CreateCollectionOptions, WriteConcern},
Namespace,
};
Expand Down Expand Up @@ -37,7 +32,6 @@ impl OperationWithDefaults for Create {
Self::NAME: self.ns.coll.clone(),
};

remove_empty_write_concern!(self.options);
append_options_to_raw_document(&mut body, self.options.as_ref())?;

Ok(Command::new(
Expand Down
7 changes: 1 addition & 6 deletions src/operation/create_indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ use crate::{
cmap::{Command, RawCommandResponse, StreamDescription},
error::{ErrorKind, Result},
index::IndexModel,
operation::{
append_options_to_raw_document,
remove_empty_write_concern,
OperationWithDefaults,
},
operation::{append_options_to_raw_document, OperationWithDefaults},
options::{CreateIndexOptions, WriteConcern},
results::CreateIndexesResult,
Namespace,
Expand Down Expand Up @@ -65,7 +61,6 @@ impl OperationWithDefaults for CreateIndexes {
"indexes": indexes,
};

remove_empty_write_concern!(self.options);
append_options_to_raw_document(&mut body, self.options.as_ref())?;

Ok(Command::new(
Expand Down
9 changes: 1 addition & 8 deletions src/operation/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ use crate::{
coll::Namespace,
collation::Collation,
error::{convert_insert_many_error, Result},
operation::{
append_options,
remove_empty_write_concern,
OperationWithDefaults,
Retryability,
WriteResponseBody,
},
operation::{append_options, OperationWithDefaults, Retryability, WriteResponseBody},
options::{DeleteOptions, Hint, WriteConcern},
results::DeleteResult,
};
Expand Down Expand Up @@ -70,7 +64,6 @@ impl OperationWithDefaults for Delete {
"ordered": true, // command monitoring tests expect this (SPEC-1130)
};

remove_empty_write_concern!(self.options);
append_options(&mut body, self.options.as_ref())?;

Ok(Command::new(
Expand Down
8 changes: 1 addition & 7 deletions src/operation/drop_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ use crate::bson::rawdoc;
use crate::{
cmap::{Command, RawCommandResponse, StreamDescription},
error::{Error, Result},
operation::{
append_options_to_raw_document,
remove_empty_write_concern,
OperationWithDefaults,
WriteConcernOnlyBody,
},
operation::{append_options_to_raw_document, OperationWithDefaults, WriteConcernOnlyBody},
options::{DropCollectionOptions, WriteConcern},
Namespace,
};
Expand Down Expand Up @@ -37,7 +32,6 @@ impl OperationWithDefaults for DropCollection {
Self::NAME: self.ns.coll.clone(),
};

remove_empty_write_concern!(self.options);
append_options_to_raw_document(&mut body, self.options.as_ref())?;

Ok(Command::new(
Expand Down
8 changes: 1 addition & 7 deletions src/operation/drop_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ use crate::{
cmap::{Command, RawCommandResponse, StreamDescription},
db::options::DropDatabaseOptions,
error::Result,
operation::{
append_options_to_raw_document,
remove_empty_write_concern,
OperationWithDefaults,
WriteConcernOnlyBody,
},
operation::{append_options_to_raw_document, OperationWithDefaults, WriteConcernOnlyBody},
options::WriteConcern,
};

Expand Down Expand Up @@ -37,7 +32,6 @@ impl OperationWithDefaults for DropDatabase {
Self::NAME: 1,
};

remove_empty_write_concern!(self.options);
append_options_to_raw_document(&mut body, self.options.as_ref())?;

Ok(Command::new(
Expand Down
7 changes: 1 addition & 6 deletions src/operation/drop_indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ use crate::bson::rawdoc;
use crate::{
cmap::{Command, RawCommandResponse, StreamDescription},
error::Result,
operation::{
append_options_to_raw_document,
remove_empty_write_concern,
OperationWithDefaults,
},
operation::{append_options_to_raw_document, OperationWithDefaults},
options::{DropIndexOptions, WriteConcern},
Namespace,
};
Expand Down Expand Up @@ -36,7 +32,6 @@ impl OperationWithDefaults for DropIndexes {
"index": self.name.clone(),
};

remove_empty_write_concern!(self.options);
append_options_to_raw_document(&mut body, self.options.as_ref())?;

Ok(Command::new(
Expand Down
4 changes: 0 additions & 4 deletions src/operation/find_and_modify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{
operation::{
append_options_to_raw_document,
find_and_modify::options::Modification,
remove_empty_write_concern,
OperationWithDefaults,
Retryability,
},
Expand Down Expand Up @@ -82,9 +81,6 @@ impl<T: DeserializeOwned> OperationWithDefaults for FindAndModify<T> {
}
}

if let Some(ref mut options) = self.options {
remove_empty_write_concern!(Some(options));
}
append_options_to_raw_document(&mut body, self.options.as_ref())?;

Ok(Command::new(
Expand Down
3 changes: 2 additions & 1 deletion src/operation/find_and_modify/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
collation::Collation,
concern::WriteConcern,
operation::UpdateOrReplace,
serde_util,
serde_util::{self, write_concern_is_empty},
};

#[derive(Clone, Debug)]
Expand All @@ -37,6 +37,7 @@ pub(crate) struct FindAndModifyOptions {

pub(crate) bypass_document_validation: Option<bool>,

#[serde(skip_serializing_if = "write_concern_is_empty")]
pub(crate) write_concern: Option<WriteConcern>,

pub(crate) array_filters: Option<Vec<Document>>,
Expand Down