Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Preserve user specified event values in Unreal crash reports. ([#4882](https://github.com/getsentry/relay/pull/4882))
- OS name parsing of Unreal crash reports. ([#4854](https://github.com/getsentry/relay/pull/4854))
- Do not overwrite geo information if already set. ([#4888](https://github.com/getsentry/relay/pull/4888))
- The `type` fields of contexts are now enforced to be strings. Non-string values are replaced with the
context's key. ([#4932](https://github.com/getsentry/relay/pull/4932))

**Internal**:

Expand Down
35 changes: 21 additions & 14 deletions relay-event-schema/src/protocol/contexts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl FromValue for Contexts {
for (key, value) in items.iter_mut() {
if let Annotated(Some(Value::Object(items)), _) = value {
// Set the `"type"` if it's empty and overwrite it if it's an empty object or array.
if value_is_empty(items.get("type")) {
if !is_valid_context_type(items.get("type")) {
items.insert(
"type".to_owned(),
Annotated::new(Value::String(key.to_string())),
Expand All @@ -287,17 +287,10 @@ impl FromValue for Contexts {
}
}

/// Returns `true` if `value` is `None`, empty object, or an empty array.
fn value_is_empty(value: Option<&Annotated<Value>>) -> bool {
let Some(value) = value.and_then(|v| v.value()) else {
return true;
};

match value {
Value::Array(values) => values.is_empty(),
Value::Object(values) => values.is_empty(),
_ => false,
}
/// Returns `true` if `value` is a non-empty string, which is the only valid value
/// for the `"type"` field of a context.
fn is_valid_context_type(value: Option<&Annotated<Value>>) -> bool {
matches!(value.and_then(|v| v.value()), Some(Value::String(s)) if !s.is_empty())
}

/// A well-known context in the [`Contexts`] interface.
Expand Down Expand Up @@ -371,14 +364,28 @@ mod tests {
}

#[test]
fn test_context_empty_type_deserialize() {
let json = r#"{"os":{"name":"Linux","type":{}},"runtime":{"name":"rustc","type":[]}}"#;
fn test_context_invalid_type_deserialize() {
let json = r#"{
"monitor":{"name":"Foobar","type":17},
"os":{"name":"Linux","type":{}},
"profile":{"profile_id":"52df9022835246eeb317dbd739ccd059","type":""},
"runtime":{"name":"rustc","type":["invalid"]}
}"#;

let mut map = Contexts::new();
map.add(MonitorContext(
[("name".to_owned(), Value::String("Foobar".to_owned()).into())]
.into_iter()
.collect(),
));
map.add(OsContext {
name: Annotated::new("Linux".to_owned()),
..Default::default()
});
map.add(ProfileContext {
profile_id: Annotated::new("52df9022835246eeb317dbd739ccd059".parse().unwrap()),
..Default::default()
});
map.add(RuntimeContext {
name: Annotated::new("rustc".to_owned()),
..Default::default()
Expand Down
Loading