Skip to content

RUST-1886 Provide an opt-in "serialize as bytes" helper for ObjectId #582

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

abr-egn
Copy link
Contributor

@abr-egn abr-egn commented Jul 24, 2025

RUST-1886

The default extjson representation used when serializing can be problematic for some serde serializers (see #457). However, changing it to always directly serialize as a byte array (the "newtype pattern" referenced in that bug) runs the risk of breaking other users who might be, intentionally or not, relying on the current serialization format.

This PR threads the needle by providing a serde_with helper so that people who need the byte array serialization behavior can opt into it; care is taken to make sure all the normal serialization paths still work when the helper is enabled, i.e. objectids are still serialized as objectids and not generic arrays.

@abr-egn abr-egn marked this pull request as ready for review July 24, 2025 14:56
@abr-egn abr-egn requested a review from a team as a code owner July 24, 2025 14:56
@abr-egn abr-egn requested a review from isabelatkinson July 24, 2025 14:56
@@ -50,6 +53,10 @@ enum SerializerHint {

/// The next call to `serialize_bytes` is for the purposes of serializing a raw array.
RawArray,

/// The next call to `serialize_bytes` is for the porpoises of serializing an ObjectId.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐬

@@ -70,6 +70,40 @@ pub mod object_id {
Ok(oid.to_hex())
}
);

pub(crate) const OID_BYTES_NEWTYPE: &str = "$__bson_private_oid_bytes";
/// When serializing to a non-human-readable destination, will represent the [`ObjectId`] as a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we eliminated the human-readable-specific logic here, could users get the same effect with:

#[serde_as(as = "IfIsHumanReadable<serde_with::Same, object_id::AsBytes>")]
oid: ObjectId,

?
I'd prefer not to be opinionated about human-readable in these helpers if we can avoid it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants