Skip to content

RUST-1899 Deserialization with UUID #467

Closed
@Bryson14

Description

@Bryson14

Versions/Environment

  1. What version of Rust are you using? 1.77
  2. What operating system are you using? windows + wsl2
  3. What versions of the driver and its dependencies are you using? (Run
    cargo pkgid mongodb & cargo pkgid bson) 2.9.0 & 2.8.2
  4. What version of MongoDB are you using? (Check with the MongoDB shell using db.version()) 7.2.0
  5. What is your MongoDB topology (standalone, replica set, sharded cluster, serverless)? replica set

Describe the bug

A clear and concise description of what the bug is.

I am migrating data from another database which includes UUIDs stored as strings. I understand that bson::uuid stores data as binary base-64 data with a subtype flag = 4, however my current database is using strings and the rust code interacting with it is expected deserialization to and from strings.

My current struct definition:

#[derive(Serialize, Deserialize, Debug, PartialEq, PartialOrd, Clone)]
pub struct UserMetadata {
    /// A unique Single Sign On at GEHC
    pub sso: String,

    /// A unique identifier for the user.
    /// If not provided during deserialization, a random UUID will be generated.
    #[serde(default = "Uuid::now_v7")]
    pub user_id: Uuid,

    // The time the user was created.
    /// If not provided during deserialization, the current UTC time will be used.
    #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
    #[serde(default = "Utc::now")]
    pub last_access_time: DateTime<Utc>,

    // The time the user was created.
    /// If not provided during deserialization, the current UTC time will be used.
    #[serde(with = "bson::serde_helpers::chrono_datetime_as_bson_datetime")]
    #[serde(default = "Utc::now")]
    pub create_time: DateTime<Utc>,

    /// Team ID at GEHC
    #[serde(skip_serializing_if = "Option::is_none")]
    pub team_id: Option<String>,

    /// Organization ID at GEHC
    #[serde(skip_serializing_if = "Option::is_none")]
    pub org_id: Option<String>,

    /// User Type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_type: Option<String>,

    /// Content for Content Privacy
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default = "default_deny")]
    pub content_privacy_consent: Option<PrivacyConsent>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_privacy_consent_date: Option<DateTime<Utc>>,
}

When I run find() to get items from this collection I get,

called `Result::unwrap()` on an `Err` value: Error { kind: BsonDeserialization(DeserializationError { 
message: "invalid type: string \"018d6b0c-4a48-76e0-8d61-eb7977071904\", expected bytes" }), labels: {}, wire_version: None, source: None }

I tried to remedy this by changed uuid::Uuid in the struct definition to bson::Uuid but got a similar error:

called `Result::unwrap()` on an `Err` value: Error { kind: BsonDeserialization(DeserializationError { message: "expected Binary with subtype Uuid, instead got 
String" }), labels: {}, wire_version: None, source: None }

Trying to use the serde_with-3 flag didn't help and I actually got compiler errors. This was after adding the feature flag with cargo add bson -F serde_with-3. The documentation didn't say that I had to add other dependancies to get this to work.

error[E0433]: failed to resolve: use of undeclared crate or module `serde_with_3`
  --> src\models\user_metadata_bson.rs:27:3
   |
27 | #[serde_with_3::serde_as]
   |   ^^^^^^^^^^^^ use of undeclared crate or module `serde_with_3`

error: cannot find attribute `serde_as` in this scope
  --> src\models\user_metadata_bson.rs:36:7
   |
36 |     #[serde_as(as = "Option<bson::Uuid>")]
   |       ^^^^^^^^

error: cannot find attribute `serde_as` in this scope
  --> src\models\user_metadata_bson.rs:81:7
   |
81 |     #[serde_as(as = "Option<bson::Uuid>")]
   |       ^^^^^^^^

what to do:

So I'm thinking there are two fixes to this. Either I fix this issue in code and get the serialization and deserialization to play well with uuid::Uuid, or I migrate all the fields that contain string uuid in the db to Mongo's UUID format. In that case, I'd appreciate any tools to do that, but might just have to bite the bullet and write a small python scipt to do that.

To Reproduce

  1. create a document in mongo with a field that contains a string
  2. attempt to find from that collection into a uuid::UUid

Cargo.toml

[dependencies]
aws-config = { version = "1.1.5", features = ["behavior-version-latest"]}
aws-sdk-s3 = "1.15.0"
aws-smithy-types = "1.1.5"
axum = "0.7.4"
axum-test = "14.2.2"
bson = { version = "2.9.0", features = ["chrono-0_4", "uuid-1", "serde_with-3"] }
chrono = { version = "0.4.34", features = ["serde"] }
futures = "0.3.30"
http = "1.0.0"
lambda_http = "0.9.3"
lambda_runtime = "0.9.2"
mongodb = {version = "2.8.2" , features = ["tokio-runtime"]}
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0.56"
tokio = { version = "1", features = ["macros"] }
tower-http = { version = "0.5.0", features = ["trace", "add-extension"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
uuid = { version = "1.7", features = ["v4", "v7", "serde"] }

Metadata

Metadata

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