mongodb / bson-rust

Encoding and decoding support for BSON in Rust
MIT License
405 stars 134 forks source link

[feat] Ordering feature for bson serialization #490

Closed mahmoud-moursy closed 3 months ago

mahmoud-moursy commented 3 months ago

I was trying to cryptographically sign some BSON documents using the dryoc crate, and noticed that, sometimes my tests would pass, other times they would somehow fail!? And when I made some seemingly random changes to the code, it would start working again!?

It turns out the root cause was that, when I was serializing the same data, it was not always in the same order, causing the signature to be different! 😮

I guess that's on me for trying this with an unstructured data format, but it would be nice to have a feature that automatically sorts entries. I currently hacked together a to_sorted function that sorts the entries using &str's handy built-in Cmp.

abr-egn commented 3 months ago

Yep, the Document type is backed by a standard HashMap, which is nondeterministic in iteration order. Would the RawDocument type work for your purposes? Iteration there is deterministic (it's whatever order the serialized data is in).

mahmoud-moursy commented 3 months ago

I fixed my problems just using a BTreeMap without needing any extra trickery 😁. BTreeMap keeps everything in a deterministic order thanks to its structure, and that seems to carry over when serializing it into a Document 👍