Skip to content

Add type checking in contexts try get value #1700

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

Merged
merged 4 commits into from
Jun 7, 2022
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 @@
- NOTE: This can affect grouping. You can keep the original behavior by setting the option `KeepAggregateException` to `true`.
- Serialize stack frame addresses as strings. ([#1692](https://github.com/getsentry/sentry-dotnet/pull/1692))
- Improve serialization perf and fix memory leak in `SentryEvent` ([#1693](https://github.com/getsentry/sentry-dotnet/pull/1693))
- Add type checking in contexts TryGetValue ([#1700](https://github.com/getsentry/sentry-dotnet/pull/1700))

### Features

Expand Down
11 changes: 10 additions & 1 deletion src/Sentry/Internal/Extensions/CollectionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ public static TValue GetOrCreate<TValue>(
this ConcurrentDictionary<string, object> dictionary,
string key)
where TValue : class, new()
=> (TValue)dictionary.GetOrAdd(key, _ => new TValue());
{
var value = dictionary.GetOrAdd(key, _ => new TValue());

if (value is TValue casted)
{
return casted;
}

throw new($"Expected a type of {typeof(TValue)} to exist for the key '{key}'. Instead found a {value.GetType()}. The likely cause of this is that the value for '{key}' has been incorrectly set to an instance of a different type.");
}

public static void TryCopyTo<TKey, TValue>(this IDictionary<TKey, TValue> from, IDictionary<TKey, TValue> to)
where TKey : notnull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Type: Exception,
Message: Expected a type of CollectionExtensionsTests+Value to exist for the key 'key'. Instead found a System.Int32. The likely cause of this is that the value for 'key' has been incorrectly set to an instance of a different type.
}
16 changes: 16 additions & 0 deletions test/Sentry.Tests/Internals/CollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Concurrent;

[UsesVerify]
public class CollectionExtensionsTests
{
[Fact]
public Task GetOrCreate_invalid_type()
{
var dictionary = new ConcurrentDictionary<string, object> {["key"] = 1};
return Throws(() => dictionary.GetOrCreate<Value>("key")).IgnoreStackTrack();
}

class Value
{
}
}