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-1243 Failed to deserialize to hashmap when key is enum #613

Closed
George-Miao opened this issue Mar 27, 2022 · 6 comments
Closed

RUST-1243 Failed to deserialize to hashmap when key is enum #613

George-Miao opened this issue Mar 27, 2022 · 6 comments
Assignees
Labels
tracked-in-jira Ticket filed in Mongo's Jira system

Comments

@George-Miao
Copy link

George-Miao commented Mar 27, 2022

Versions/Environment

  1. What version of Rust are you using?
    rustc 1.61.0-nightly (8d60bf427 2022-03-19) and rustc 1.59.0 (9d1b2106e 2022-02-23)

Not using the latest nightly because ring is broken.

  1. What operating system are you using?
    Arch linux 5.16.8-zen1-1-zen

  2. What versions of the driver and its dependencies are you using? (Run
    cargo pkgid mongodb & cargo pkgid bson)
    https://github.com/mongodb/mongo-rust-driver.git#mongodb:2.2.0-beta
    https://github.com/rust-lang/crates.io-index#bson:2.2.0-beta

  3. What version of MongoDB are you using? (Check with the MongoDB shell using db.version())
    4.4.4

  4. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)?
    standalone

Describe the bug

With structs that have a HashMap<K, V> where K is an enum that implements Hash, mongodb will not correctly deserialize it from the database. This issue only occurs when I use branch 2.2.x. Previous versions that I installed from crates.io works fine. So I assume this is broken by some newly introduced changes.

Output:

Error { kind: BsonDeserialization(DeserializationError { message: "invalid type: string \"Baz\", expected enum Bar" }), labels: {}, wire_version: None, source: None }', src/main.rs:31:46

Code:

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct Foo {
    map: HashMap<Bar, String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
enum Bar {
    Baz,
}

#[tokio::main]
async fn main() {
    let col = mongodb::Client::with_uri_str("...")
        .await
        .unwrap()
        .database("test")
        .collection::<Foo>("test");

    col.drop(None).await.unwrap();

    let obj = Foo {
        map: HashMap::from_iter([(Bar::Baz, "1".to_owned()), (Bar::Baz, "2".to_owned())]),
    };

    col.insert_one(&obj, None).await.unwrap();

    let ret = col.find_one(None, None).await.unwrap().unwrap();

    assert_eq!(ret, obj);
}
@abr-egn
Copy link
Contributor

abr-egn commented Mar 28, 2022

Thank you for the detailed and timely report! I've reproduced this failure locally and am investigating.

@bajanam bajanam added tracked-in-jira Ticket filed in Mongo's Jira system and removed triage labels Mar 28, 2022
@bajanam bajanam changed the title Failed to deserialize to hashmap when key is enum RUST-1243 Failed to deserialize to hashmap when key is enum Mar 28, 2022
@abr-egn
Copy link
Contributor

abr-egn commented Mar 28, 2022

I just pushed a fix for this to the bson 2.2.x branch - can you update your local environment to use that and verify that it resolves your issue?

@George-Miao
Copy link
Author

I just pushed a fix for this to the bson 2.2.x branch - can you update your local environment to use that and verify that it resolves your issue?

Just did the test again, runs perfectlly. Thanks for such a quick update! And may I ask when will this change be published to crates.io?

@abr-egn
Copy link
Contributor

abr-egn commented Mar 29, 2022

There's another bug I'd like to track down before rolling out a beta.1 release, so probably early next week.

@George-Miao
Copy link
Author

There's another bug I'd like to track down before rolling out a beta.1 release, so probably early next week.

I see. Then I shall close the issue.

@abr-egn
Copy link
Contributor

abr-egn commented Mar 31, 2022

FYI, I just published 2.2.0-beta.1, which contains the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracked-in-jira Ticket filed in Mongo's Jira system
Projects
None yet
Development

No branches or pull requests

3 participants