Skip to content

Commit f416d15

Browse files
authored
Replace BaseScope with IScope (#590)
1 parent bea2086 commit f416d15

File tree

22 files changed

+759
-741
lines changed

22 files changed

+759
-741
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## vNext
44

5+
* Replaced `BaseScope` with `IScope`. (#590) @Tyrrrz
56
* Removed code coverage report from the test folder. (#592) @lucas-zimerman
67
* Add target framework NET5.0 on Sentry.csproj. Change the type of `Extra` where value parameter become nullable. @lucas-zimerman
78
* Implement envelope caching. (#576) @Tyrrrz

src/Sentry.AspNetCore.Grpc/ScopeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void Populate<TRequest>(this Scope scope, ServerCallContext contex
3535
}
3636
}
3737

38-
private static void SetBody<TRequest>(BaseScope scope, ServerCallContext context, TRequest request,
38+
private static void SetBody<TRequest>(IScope scope, ServerCallContext context, TRequest request,
3939
SentryAspNetCoreOptions options) where TRequest : class, IMessage
4040
{
4141
var httpContext = context.GetHttpContext();

src/Sentry.AspNetCore/ScopeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private static void SetEnv(Scope scope, HttpContext context, SentryAspNetCoreOpt
147147
}
148148
}
149149

150-
private static void SetBody(BaseScope scope, HttpContext context, SentryAspNetCoreOptions options)
150+
private static void SetBody(IScope scope, HttpContext context, SentryAspNetCoreOptions options)
151151
{
152152
var extractors = context.RequestServices.GetService<IEnumerable<IRequestPayloadExtractor>>();
153153
if (extractors == null)

src/Sentry/Internal/Extensions/CollectionsExtensions.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,28 @@ namespace Sentry.Internal.Extensions
66
{
77
internal static class CollectionsExtensions
88
{
9+
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items)
10+
{
11+
foreach (var i in items)
12+
{
13+
collection.Add(i);
14+
}
15+
}
16+
917
public static TValue GetOrCreate<TValue>(
1018
this ConcurrentDictionary<string, object> dictionary,
1119
string key)
1220
where TValue : class, new()
1321
=> (TValue) dictionary.GetOrAdd(key, _ => new TValue());
1422

15-
public static ConcurrentQueue<T> EnqueueAll<T>(this ConcurrentQueue<T> target, IEnumerable<T> values)
16-
{
17-
foreach (var value in values)
18-
{
19-
target.Enqueue(value);
20-
}
21-
22-
return target;
23-
}
24-
2523
public static void TryCopyTo<TKey, TValue>(this IDictionary<TKey, TValue> from, IDictionary<TKey, TValue> to)
2624
where TKey : notnull
2725
{
28-
foreach (var kv in from)
26+
foreach (var (key, value) in from)
2927
{
30-
if (!to.ContainsKey(kv.Key))
28+
if (!to.ContainsKey(key))
3129
{
32-
to[kv.Key] = kv.Value;
30+
to[key] = value;
3331
}
3432
}
3533
}

src/Sentry/Internal/Json.cs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
1+
using System;
2+
using System.Collections;
13
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
26
using System.Text;
37
using System.Threading;
48
using System.Threading.Tasks;
59
using Newtonsoft.Json;
610
using Newtonsoft.Json.Converters;
11+
using Newtonsoft.Json.Serialization;
712

813
namespace Sentry.Internal
914
{
15+
[AttributeUsage(AttributeTargets.Property)]
16+
internal class DontSerializeEmptyAttribute : Attribute {}
17+
1018
internal static class Json
1119
{
20+
private class ContractResolver : DefaultContractResolver
21+
{
22+
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
23+
{
24+
var jsonProperty = base.CreateProperty(member, memberSerialization);
25+
var property = jsonProperty.DeclaringType.GetProperty(jsonProperty.UnderlyingName);
26+
27+
// DontSerializeEmpty
28+
if (jsonProperty.ShouldSerialize is null &&
29+
property?.GetCustomAttribute<DontSerializeEmptyAttribute>() is {})
30+
{
31+
// Collections
32+
if (property.PropertyType.GetInterfaces().Any(i => i == typeof(IEnumerable)))
33+
{
34+
jsonProperty.ShouldSerialize = o =>
35+
{
36+
if (property.GetValue(o) is IEnumerable value)
37+
{
38+
return !value.Cast<object>().Any();
39+
}
40+
41+
return true;
42+
};
43+
}
44+
}
45+
46+
return jsonProperty;
47+
}
48+
}
49+
1250
private static readonly Encoding Encoding = new UTF8Encoding(false, true);
1351
private static readonly StringEnumConverter StringEnumConverter = new StringEnumConverter();
1452

@@ -19,7 +57,8 @@ internal static class Json
1957
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
2058
Formatting = Formatting.None,
2159
Converters = {StringEnumConverter},
22-
DateFormatHandling = DateFormatHandling.IsoDateFormat
60+
DateFormatHandling = DateFormatHandling.IsoDateFormat,
61+
ContractResolver = new ContractResolver()
2362
};
2463

2564
private static JsonTextWriter CreateWriter(Stream stream) => new JsonTextWriter(

0 commit comments

Comments
 (0)