Skip to content

Commit a223bed

Browse files
committed
feat: Skip some field in user serialization and added test
1 parent 12411e1 commit a223bed

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/protocol/v7.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,16 @@ impl Default for Breadcrumb {
292292
#[serde(default)]
293293
pub struct User {
294294
/// The ID of the user.
295+
#[serde(skip_serializing_if = "Option::is_none")]
295296
pub id: Option<String>,
296297
/// The email address of the user.
298+
#[serde(skip_serializing_if = "Option::is_none")]
297299
pub email: Option<String>,
298300
/// The remote ip address of the user.
301+
#[serde(skip_serializing_if = "Option::is_none")]
299302
pub ip_address: Option<IpAddr>,
300303
/// A human readable username of the user.
304+
#[serde(skip_serializing_if = "Option::is_none")]
301305
pub username: Option<String>,
302306
/// Additional data that should be send along.
303307
#[serde(flatten)]

tests/test_protocol_v7.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,47 @@ fn test_repos() {
182182
);
183183
}
184184

185+
#[test]
186+
fn test_user() {
187+
let event = v7::Event {
188+
user: Some(v7::User {
189+
id: Some("8fd5a33b-5b0e-45b2-aff2-9e4f067756ba".into()),
190+
email: Some("[email protected]".into()),
191+
ip_address: Some("127.0.0.1".parse().unwrap()),
192+
username: Some("john-doe".into()),
193+
data: {
194+
let mut hm = HashMap::new();
195+
hm.insert("foo".into(), "bar".into());
196+
hm
197+
}
198+
}),
199+
..Default::default()
200+
};
201+
202+
assert_eq!(
203+
serde_json::to_string(&event).unwrap(),
204+
"{\"user\":{\"id\":\"8fd5a33b-5b0e-45b2-aff2-9e4f067756ba\",\
205+
\"email\":\"[email protected]\",\"ip_address\":\"127.0.0.1\",\
206+
\"username\":\"john-doe\",\"foo\":\"bar\"}}"
207+
);
208+
209+
let event = v7::Event {
210+
user: Some(v7::User {
211+
id: Some("8fd5a33b-5b0e-45b2-aff2-9e4f067756ba".into()),
212+
email: None,
213+
ip_address: None,
214+
username: None,
215+
..Default::default()
216+
}),
217+
..Default::default()
218+
};
219+
220+
assert_eq!(
221+
serde_json::to_string(&event).unwrap(),
222+
"{\"user\":{\"id\":\"8fd5a33b-5b0e-45b2-aff2-9e4f067756ba\"}}"
223+
);
224+
}
225+
185226
#[test]
186227
fn test_canonical_exception() {
187228
let mut event: v7::Event = Default::default();

0 commit comments

Comments
 (0)