Skip to content
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

RUST-1743 Fix gridfs numeric type serialization #941

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/gridfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ pub(crate) struct Chunk<'a> {
#[serde(rename = "_id")]
id: ObjectId,
files_id: Bson,
#[serde(serialize_with = "u32_as_i32")]
n: u32,
#[serde(borrow)]
data: RawBinaryRef<'a>,
}

fn u32_as_i32<S>(value: &u32, ser: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
ser.serialize_i32((*value).try_into().map_err(serde::ser::Error::custom)?)
}

/// A model for the documents stored in a GridFS bucket's files
/// collection.
#[derive(Clone, Debug, Deserialize, Serialize)]
Expand All @@ -51,7 +59,7 @@ pub struct FilesCollectionDocument {
pub length: u64,

/// The size of the file's chunks in bytes.
#[serde(rename = "chunkSize")]
#[serde(rename = "chunkSize", serialize_with = "u32_as_i32")]
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you should be able to use this method from bson::serde_helpers here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Entirely forgot we had those, fixed :)

pub chunk_size_bytes: u32,

/// The time at which the file was uploaded.
Expand Down