From a223bedf9ce008f1f8ce594846ae6bd2ee910209 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Fri, 23 Mar 2018 23:57:24 +0100 Subject: [PATCH] feat: Skip some field in user serialization and added test --- src/protocol/v7.rs | 4 ++++ tests/test_protocol_v7.rs | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/protocol/v7.rs b/src/protocol/v7.rs index f83d85fa..4e07712b 100644 --- a/src/protocol/v7.rs +++ b/src/protocol/v7.rs @@ -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, /// The email address of the user. + #[serde(skip_serializing_if = "Option::is_none")] pub email: Option, /// The remote ip address of the user. + #[serde(skip_serializing_if = "Option::is_none")] pub ip_address: Option, /// A human readable username of the user. + #[serde(skip_serializing_if = "Option::is_none")] pub username: Option, /// Additional data that should be send along. #[serde(flatten)] diff --git a/tests/test_protocol_v7.rs b/tests/test_protocol_v7.rs index 039f1077..33408104 100644 --- a/tests/test_protocol_v7.rs +++ b/tests/test_protocol_v7.rs @@ -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("foo@example.invalid".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\":\"foo@example.invalid\",\"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();