Skip to content

Commit

Permalink
feat: Skip some field in user serialization and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Mar 23, 2018
1 parent 12411e1 commit a223bed
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/protocol/v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,16 @@ impl Default for Breadcrumb {
#[serde(default)]
pub struct User {
/// The ID of the user.
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
/// The email address of the user.
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
/// The remote ip address of the user.
#[serde(skip_serializing_if = "Option::is_none")]
pub ip_address: Option<IpAddr>,
/// A human readable username of the user.
#[serde(skip_serializing_if = "Option::is_none")]
pub username: Option<String>,
/// Additional data that should be send along.
#[serde(flatten)]
Expand Down
41 changes: 41 additions & 0 deletions tests/test_protocol_v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,47 @@ fn test_repos() {
);
}

#[test]
fn test_user() {
let event = v7::Event {
user: Some(v7::User {
id: Some("8fd5a33b-5b0e-45b2-aff2-9e4f067756ba".into()),
email: Some("[email protected]".into()),
ip_address: Some("127.0.0.1".parse().unwrap()),
username: Some("john-doe".into()),
data: {
let mut hm = HashMap::new();
hm.insert("foo".into(), "bar".into());
hm
}
}),
..Default::default()
};

assert_eq!(
serde_json::to_string(&event).unwrap(),
"{\"user\":{\"id\":\"8fd5a33b-5b0e-45b2-aff2-9e4f067756ba\",\
\"email\":\"[email protected]\",\"ip_address\":\"127.0.0.1\",\
\"username\":\"john-doe\",\"foo\":\"bar\"}}"
);

let event = v7::Event {
user: Some(v7::User {
id: Some("8fd5a33b-5b0e-45b2-aff2-9e4f067756ba".into()),
email: None,
ip_address: None,
username: None,
..Default::default()
}),
..Default::default()
};

assert_eq!(
serde_json::to_string(&event).unwrap(),
"{\"user\":{\"id\":\"8fd5a33b-5b0e-45b2-aff2-9e4f067756ba\"}}"
);
}

#[test]
fn test_canonical_exception() {
let mut event: v7::Event = Default::default();
Expand Down

0 comments on commit a223bed

Please sign in to comment.