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

Deserialization implementation for Document doesn't handle documents with extjson keys #419

Open
PenguinToast opened this issue Jun 26, 2023 · 1 comment
Assignees
Labels
tracked-in-jira Ticket filed in Mongo's Jira system

Comments

@PenguinToast
Copy link

Describe the bug

See reproduction below:

    let test_doc = doc! {
        "acutalTimestamp": bson::Timestamp {increment: 0, time: 12345},
        "operationTime": {"$timestamp": "7248047827882870681"},
    };
    let mut bson_bytes = Vec::<u8>::new();
    bson::Document::to_writer(&test_doc, &mut bson_bytes)?;
    let deserialized = bson::from_slice::<bson::Document>(&bson_bytes)?;

This errors with:

Error: invalid type: string "7248047827882870681", expected struct TimestampBody

test_doc is a valid BSON document, however the deserializer doesn't make any distinction between a $timestamp from an actual Timestamp vs. just a document with a $timestamp key. This causes issues with custom Deserialize implementations for data structures, where I can't figure out how to differentiate between a real Timestamp and a Document.

BE SPECIFIC:

  • What is the expected behavior and what is actually happening?
    • This should not error, but it does.
@abr-egn
Copy link
Contributor

abr-egn commented Jun 28, 2023

Hi!

You're right, that shouldn't be an error. This is an unfortunate side effect of how our implementation interacts with serde; the information that the value is a subdocument rather than a timestamp is lost in the parsing. I've filed RUST-1693 to track this.

In the meanwhile, you can use the raw bson types to work around this - if you change your final line to

let deserialized = bson::from_slice::<bson::RawDocumentBuf>(&bson_bytes)?.to_document()?;

it works as expected.

@abr-egn abr-egn added tracked-in-jira Ticket filed in Mongo's Jira system and removed triage labels Jun 28, 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