Skip to content

Commit

Permalink
Fix context merging #1722 (#1724)
Browse files Browse the repository at this point in the history
* Fix context merging

When using the same key in a context hash, the new value should
overwrite the old value. Before, the value having been set first would
always win.

fixes #1722

* Add changelog
  • Loading branch information
thomasjachmann committed Feb 11, 2022
1 parent c65088f commit e960d02
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Unreleased

### Bug Fixes

- Allow overwriting of context values [#1724](https://github.com/getsentry/sentry-ruby/pull/1724)
- Fixes [#1722](https://github.com/getsentry/sentry-ruby/issues/1722)

## 5.1.0

### Features
Expand Down
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def set_tag(key, value)
def set_contexts(contexts_hash)
check_argument_type!(contexts_hash, Hash)
@contexts.merge!(contexts_hash) do |key, old, new|
new.merge(old)
old.merge(new)
end
end

Expand Down
12 changes: 12 additions & 0 deletions sentry-ruby/spec/sentry/scope/setters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
subject.set_contexts({ character: { age: 25 }})
expect(subject.contexts).to include({ character: { name: "John", age: 25 }})
end

it "allows overwriting old values" do
subject.set_contexts({ character: { name: "John" }})
subject.set_contexts({ character: { name: "Jimmy" }})
expect(subject.contexts).to include({ character: { name: "Jimmy" }})
end
end

describe "#set_context" do
Expand Down Expand Up @@ -139,6 +145,12 @@
subject.set_context(:character, { age: 25 })
expect(subject.contexts).to include({ character: { name: "John", age: 25 }})
end

it "allows overwriting old values" do
subject.set_context(:character, { name: "John" })
subject.set_context(:character, { name: "Jimmy" })
expect(subject.contexts).to include({ character: { name: "Jimmy" }})
end
end

describe "#set_tags" do
Expand Down

0 comments on commit e960d02

Please sign in to comment.