mongodb / bson-rust

Encoding and decoding support for BSON in Rust
MIT License
389 stars 130 forks source link

Bson fails to serialize AND deserialize IPAddr keys in hashmaps #439

Closed annmuor closed 7 months ago

annmuor commented 8 months ago

Versions/Environment

  1. Rust 1.73
  2. Arch Linux / 6.1.60 kernel
  3. https://github.com/rust-lang/crates.io-index#bson@2.7.0
  4. None
  5. None

Describe the bug

A clear and concise description of what the bug is.

bson encoder fails to properly encode IpAddr ( 2-variant enum ) into HashMap. But it works fine with Vec.

To Reproduce

    #[test]
    fn bson_serialize_failure() {
        #[derive(serde::Serialize, serde::Deserialize)]
        struct Test {
            field: HashMap<IpAddr, usize>,
        };

        let test = Test {
            field: {
                let mut m = HashMap::new();
                m.insert(IpAddr::from([127u8, 0, 0, 1]), 10);
                m
            },
        };
        let buf = bson::to_vec(&test).unwrap();
        let test1: Test = bson::from_slice(&buf).unwrap();
    }
isabelatkinson commented 8 months ago

Hi @annmuor, thanks for opening this issue! The problem you're experiencing is due to the fact that a HashMap will serialize to a Document when converting to BSON, which only supports string keys. I added the following lines to your test case:

let doc: Document = bson::from_slice(&buf).unwrap();
dbg!("{}", doc);

and the output printed was:

doc = Document({
    "field": Document({
        "127.0.0.1": Int64(
            10,
        ),
    }),
})

which differs from how a Vec<IpAddr> will be serialized because our serializer needs to serialize the IpAddr keys as strings.

When you call bson::from_slice with the buffer you've created, our raw BSON deserializer will try to deserialize the key string into the specified type, which is IpAddr in your case. I looked into the Deserialize implementation for IpAddr, and it looks like deserializing from a string is only supported when the deserializer is human readable. Our deserializer for raw BSON is not human readable, so the deserializer tries to deserialize an enum and fails when it encounters a string. That said, our non-raw BSON deserializer is human readable, so changing the last two lines in your test to the following will work just fine:

let doc = bson::to_document(&test).unwrap();
let _: Test = bson::from_document(doc).unwrap();

I recommend using this approach instead to fix the error you're encountering. If that's not feasible, you'll likely need to write a custom deserialization method for your HashMap<IpAddr, usize> field and use it in a deserialize_with attribute. I can help out with writing that method if needed. Please let me know if you have any further questions!

github-actions[bot] commented 7 months ago

There has not been any recent activity on this ticket, so we are marking it as stale. If we do not hear anything further from you, this issue will be automatically closed in one week.

annmuor commented 7 months ago

Thank you very much.