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-1107 Fix array relaxed extended json #325

Merged
merged 2 commits into from
Nov 16, 2021

Conversation

pacifistes
Copy link
Contributor

@pacifistes pacifistes commented Nov 13, 2021

Hi,

It seems that relaxed extended json doesn't work for arrays, here is an example:

use bson::serde_helpers::chrono_datetime_as_bson_datetime;
use serde_json::Value;
use chrono::Utc;
use chrono::TimeZone;
use serde::{
    Deserialize,
    Serialize,
};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Foo {
    #[serde(with = "chrono_datetime_as_bson_datetime")]
    pub date: chrono::DateTime<Utc>,
}

fn main() {
    let foo = Foo {
        date: Utc.ymd(2018, 1, 1).and_hms(0, 0, 0),
    };
    let bson = bson::to_bson(&foo).unwrap();
    let expected: Value = serde_json::from_str(r#"{"date":{"$date": "2018-01-01T00:00:00Z"}}"#).unwrap();
    println!("{:?}", expected); //Object({"date": Object({"$date": String("2018-01-01T00:00:00Z")})})
    assert_eq!(expected, bson.into_relaxed_extjson()); // WORK

    let list = vec![foo];
    let bson = bson::to_bson(&list).unwrap();
    let expected: Value = serde_json::from_str(r#"[{"date":{"$date": "2018-01-01T00:00:00Z"}}]"#).unwrap();
    println!("{:?}", expected); //Array([Object({"date": Object({"$date": String("2018-01-01T00:00:00Z")})})])
    assert_eq!(expected, bson.into_relaxed_extjson()); // NOT WORK
}

If i run, i get:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Array([Object({"date": Object({"$date": String("2018-01-01T00:00:00Z")})})])`,
 right: `Array([Object({"date": Object({"$date": Object({"$numberLong": String("1514764800000")})})})])`', src/main.rs:30:5

So i changed this:

Bson::Array(v) => json!(v)

to this:

Bson::Array(v) => Value::Array(v.into_iter().map(Bson::into_relaxed_extjson).collect())

@bajanam bajanam assigned bajanam and patrickfreed and unassigned bajanam Nov 15, 2021
@bajanam
Copy link
Contributor

bajanam commented Nov 15, 2021

Hello and thanks for the feedback, our team will review the issue/pull request and comment back.

@bajanam bajanam added the tracked-in-jira Ticket filed in Mongo's Jira system label Nov 15, 2021
@patrickfreed patrickfreed changed the title Fix: Array relaxed extended json RUST-1107 Fix array relaxed extended json Nov 15, 2021
Copy link
Contributor

@patrickfreed patrickfreed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed a bug, thank you for reporting and fixing it! I just have one small comment, but otherwise looks good to me.

src/bson.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@patrickfreed patrickfreed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks again for your contribution! Tagging in the rest of the team for review

Copy link
Contributor

@abr-egn abr-egn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@isabelatkinson isabelatkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your contribution!

@patrickfreed patrickfreed merged commit 502223f into mongodb:master Nov 16, 2021
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

Successfully merging this pull request may close these issues.

5 participants