Skip to content

Replacements in collections use different serializer than insertions #917

Closed
@kodemartin

Description

@kodemartin

Versions/Environment

  1. What version of Rust are you using?
    cargo 1.68.2 (6feb7c9cf 2023-03-26)
    
  2. What operating system are you using?
    Linux 6.4.3-arch1-2 x86_64 GNU/Linux
    
  3. What versions of the driver and its dependencies are you using? (Run cargo pkgid mongodb & cargo pkgid bson)
    https://github.com/rust-lang/crates.io-index#mongodb@2.6.0
    https://github.com/rust-lang/crates.io-index#bson@2.6.1
    
  4. What version of MongoDB are you using? (Check with the MongoDB shell using db.version())
    6.0
    
  5. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
    replica set
    

Describe the bug

For types that use different a serializer for human-readable serialization, we face the following issue:

When inserted, values are serialized by the non-human readable serializer on account of using bson::to_vec under the hood.

However, if we try to replace such values all replace methods in Collection use internally bson::to_document that uses the human-readable serializer.

This implies that if we attempt to find such a value that has been replaced and deserialize, we will either get a deserialization error, or incorrect data.

To Reproduce
Steps to reproduce the behavior:

  1. Define a type that implements serde_with::SerializeAs using different serializers for human-readable (e.g. String) and non-readable (e.g. Bytes).
  2. Then insert a value of this type into a collection. Store it as original
  3. After doing that, replace that value with a clone of itself using any of the Collection methods that replace values (e.g. replace_one).
  4. Get the replaced value replaced from the database and verify that !assert_eq!(original, replaced).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions