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

Deserialize ObjectId from hex string? #418

Closed
pianocomposer321 opened this issue Jun 17, 2023 · 5 comments
Closed

Deserialize ObjectId from hex string? #418

pianocomposer321 opened this issue Jun 17, 2023 · 5 comments
Assignees
Labels
tracked-in-jira Ticket filed in Mongo's Jira system

Comments

@pianocomposer321
Copy link

pianocomposer321 commented Jun 17, 2023

I'm using mongodb for the backend api of a project I'm working on, and I want the api to return the hex string in the json, so I'm using serialize_with = "bson::serde_helpers::serialize_object_id_as_hex_string". But this causes errors when I try to deserialize from the json response, which has the id as the raw hex string, because that's how it was serialized.

The serde_helpers module has three functions for objectid's: serialize_object_id_as_hex_string, deserialize_hex_string_from_object_id, and serialize_hex_string_as_object_id.

Is there a reason that there is no fourth, something like deserialize_object_id_from_hex_string?

Thanks!

@abr-egn
Copy link
Contributor

abr-egn commented Jun 20, 2023

Hi! Thanks for raising this - there's no particular reason that's lacking, we should definitely add it :) I've filed https://jira.mongodb.org/browse/RUST-1681 to track this. We'd also be happy to accept a PR if you'd like to implement it!

@abr-egn abr-egn added tracked-in-jira Ticket filed in Mongo's Jira system and removed triage labels Jun 20, 2023
sunsided added a commit to sunsided/bson-rust that referenced this issue Jul 6, 2023
@sunsided
Copy link

sunsided commented Jul 6, 2023

I took the liberty to give it a try in #421.

@abr-egn
Copy link
Contributor

abr-egn commented Jul 10, 2023

Sorry that I didn't notice this earlier - as @patrickfreed quite correctly points out, the implementation of Deserialize for ObjectId already accepts a hex string (#421 just passes through to that implementation), so this should work without an extra deserialize helper. Can you give more details about the error you're seeing?

@pianocomposer321
Copy link
Author

Yes, so I realized this after opening the issue. The problem I was having wasn't actually related to this, I was just thinking that maybe doing this would fix it. I wanted to have the object ids represented with the ObjectId type in my code, and as a bson object id in mongodb, but as a string for the json serialization. The problem was that if I changed the serializer, it would serialize it as a string when interacting with mongodb as well, so it wouldn't store it as an ObjectId there either. I thought maybe having this feature would solve that, but now I don't think it would, because the issue is that it is serializing it the same for json and bson, when I don't want it to do that.

What I ended up doing is creating a new struct for just my json api that serializes to a hex string and keeping my main struct for interacting with mongodb the same. If there's a way to do this with a single struct by differentiating between serializing for json vs bson, I'd love to know about it. But, I think this issue can probably be closed.

@abr-egn
Copy link
Contributor

abr-egn commented Jul 10, 2023

Having distinct structs is probably the best way to go there, thanks for clarifying!

@abr-egn abr-egn closed this as completed Jul 10, 2023
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