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

feat(schema): Allow integers as username #3328

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Extract op and description while converting opentelemetry spans to sentry spans. ([#3287](https://github.com/getsentry/relay/pull/3287))
- Drop `event_id` and `remote_addr` from all outcomes. ([#3319](https://github.com/getsentry/relay/pull/3319))
- Support for AI token metrics ([#3250](https://github.com/getsentry/relay/pull/3250))
- Accept integers in `event.user.username`. ([#3328](https://github.com/getsentry/relay/pull/3328))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: floats are also accepted in LenientString

Suggested change
- Accept integers in `event.user.username`. ([#3328](https://github.com/getsentry/relay/pull/3328))
- Accept numbers in `event.user.username`. ([#3328](https://github.com/getsentry/relay/pull/3328))


**Internal**:

Expand Down
2 changes: 1 addition & 1 deletion relay-event-normalization/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2973,7 +2973,7 @@ mod tests {
Annotated::new(map)
});
assert_eq!(user.other, Object::new());
assert_eq!(user.username, Annotated::new("john".to_string()));
assert_eq!(user.username, Annotated::new("john".to_string().into()));
assert_eq!(user.sentry_user, Annotated::new("id:123456".to_string()));
}

Expand Down
4 changes: 2 additions & 2 deletions relay-event-normalization/src/normalize/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ mod tests {
// has to be changed. Though it is probably not a good idea!
let user = User {
id: Annotated::new("ident".to_owned().into()),
username: Annotated::new("username".to_owned()),
username: Annotated::new("username".to_owned().into()),
email: Annotated::new("email".to_owned()),
ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
..User::default()
Expand All @@ -214,7 +214,7 @@ mod tests {
assert_eq!(get_event_user_tag(&user).unwrap(), "id:ident");

let user = User {
username: Annotated::new("username".to_owned()),
username: Annotated::new("username".to_owned().into()),
email: Annotated::new("email".to_owned()),
ip_address: Annotated::new("127.0.0.1".parse().unwrap()),
..User::default()
Expand Down
17 changes: 15 additions & 2 deletions relay-event-schema/src/protocol/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct User {

/// Username of the user.
#[metastructure(pii = "true", max_chars = 128, skip_serialization = "empty")]
pub username: Annotated<String>,
pub username: Annotated<LenientString>,

/// Human readable name of the user.
#[metastructure(pii = "true", max_chars = 128, skip_serialization = "empty")]
Expand Down Expand Up @@ -163,7 +163,7 @@ mod tests {
email: Annotated::new("mail@example.org".to_string()),
ip_address: Annotated::new(IpAddr::auto()),
name: Annotated::new("John Doe".to_string()),
username: Annotated::new("john_doe".to_string()),
username: Annotated::new(LenientString("john_doe".to_owned())),
geo: Annotated::empty(),
segment: Annotated::new("vip".to_string()),
data: {
Expand Down Expand Up @@ -202,6 +202,19 @@ mod tests {
assert_eq!(output, user.to_json().unwrap());
}

#[test]
fn test_user_lenient_username() {
let input = r#"{"username":42}"#;
let output = r#"{"username":"42"}"#;
let user = Annotated::new(User {
username: Annotated::new("42".to_string().into()),
..User::default()
});

assert_eq!(user, Annotated::from_json(input).unwrap());
assert_eq!(output, user.to_json().unwrap());
Comment on lines +209 to +215
Copy link
Member

Choose a reason for hiding this comment

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

imo the round trip is enough

assert_eq!( Annotated::from_json(input).unwrap().to_json().unwrap(), r#"{"username":"42"}"#)

Can also maybe just use insta here to assert the output?

Copy link
Member Author

Choose a reason for hiding this comment

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

I copy-pasted the existing tests around this, would like to keep them as similar as possible.

}

#[test]
fn test_user_invalid_id() {
let json = r#"{"id":[]}"#;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ Event {
),
},
username: Annotated(
"hey man [ip]",
LenientString(
"hey man [ip]",
),
Meta {
remarks: [
Remark {
Expand Down
2 changes: 1 addition & 1 deletion relay-server/src/utils/unreal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn merge_unreal_context(event: &mut Event, context: Unreal4Context) {
.user
.get_or_insert_with(User::default)
.username
.set_value(Some(username));
.set_value(Some(username.into()));
}

let contexts = event.contexts.get_or_insert_with(Contexts::default);
Expand Down
Loading