mongodb / bson-rust

Encoding and decoding support for BSON in Rust
MIT License
400 stars 132 forks source link

Add Option<ObjectId> serialize_object_id_as_hex_string support #428

Closed Chu-4hun closed 1 year ago

Chu-4hun commented 1 year ago

Versions/Environment

  1. The currently active rustc version is `rustc 1.71.1 (eb26296b5 2023-08-03)
  2. Windows 10
  3. mongodb@2.6.1 & bson@2.6.1
  4. mongo:6.0.9
  5. standalone

Serde serialize_with "serialize_object_id_as_hex_string" does not work with Option<> type

I have a rust struct:

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Excursion {
    #[serde(
        rename = "_id",
        skip_serializing_if = "Option::is_none",
    )]
    pub id: Option<ObjectId>,
    pub name: String,
    pub description: String,
    pub ex_type: ExcursionType,
    pub costs: Vec<Cost>,
    pub photos: Vec<String>,
    pub is_active: bool,
    pub tickets_available: i32,
}

and _id deserializes like

"_id": {
            "$oid": "64e88fd109733dc8391a9df4"
        },

but I need only "64e88fd109733dc8391a9df4" part in my json I tried using serialize_with = "serialize_object_id_as_hex_string", but got an error

error mismatched types
expected `&ObjectId`, found `&Option<ObjectId>`

I really need that Option<> type - it makes it so much easier to ignore id when creating a new struct and getting one from DB with ObjectId

isabelatkinson commented 1 year ago

Hey @Chu-4hun, thanks for filing this issue! Unfortunately the serde helpers we provide are fairly brittle when it comes to working with optional/nested types. I discussed this issue with the team and we'd prefer not to add duplicates of the helpers for these types; however, we did discuss converting the helpers to serde_with::(De)SerializeAs implementations to allow for more flexibility in use cases like yours. We don't have the bandwidth for this work right now (and it would be a breaking change), but I filed RUST-1748 for consideration in the next major version.

In the meantime, I'd recommend using your own custom serialization method. The following should do the trick:

fn serialize_object_id_option_as_hex_string<S: Serializer>(
    val: &Option<ObjectId>,
    serializer: S
) -> Result<S::Ok, S::Error> {
    match val {
        Some(oid) => oid.to_hex().serialize(serializer),
        None => serializer.serialize_none(),
    }
}
Chu-4hun commented 1 year ago

Thanks for the reply. Glad I could help

bastibense commented 11 months ago

I'm trying to rewrite a backend in rust and this is one of the first thing I stumbled across.

We have a MongoDB collection where some documents have optional references to other documents.

These references can be null, so like the example above I'm using Option<ObjectId> to store the value.

isabelatkinson's workaround does the job, but shouldn't this be included by default? Feels awkward to keep a helper snippet of code around for a basic task like this.

Please reconsider to support this somehow. Or make it at least clearer in the documentation and provide some guidance for people who just stumbled across this.

I mean, the main idea is to have a simple hex representation of an ObjectID (in my case, for a REST API). I imagine a lot of other developers are facing the same issue. If so, are there better workarounds for this?

Anyway, I'd like to +1 this.