Skip to content

Commit 15efb4c

Browse files
authored
Merge pull request #572 from hjgraca/fix-tracing-duplicate-generic-method-decorator
fix: Error when decorating duplicate generic method - Tracing
2 parents 02db37e + bdb02b7 commit 15efb4c

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

libraries/src/AWS.Lambda.Powertools.Common/Aspects/UniversalWrapperAspect.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public object Handle(
8383
};
8484

8585
var wrappers = triggers.OfType<UniversalWrapperAttribute>().ToArray();
86-
var handler = GetMethodHandler(method, returnType, wrappers);
86+
// Target.Method is more precise for cases when decorating generic methods
87+
var handler = GetMethodHandler(target.Method, returnType, wrappers);
8788
return handler(target, args, eventArgs);
8889
}
8990

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Globalization;
2+
using System.Threading.Tasks;
3+
4+
namespace AWS.Lambda.Powertools.Tracing.Tests.Handlers;
5+
6+
public class FunctionHandlerForGeneric
7+
{
8+
[Tracing(CaptureMode = TracingCaptureMode.ResponseAndError)]
9+
public async Task<string> Handle(string input)
10+
{
11+
GenericMethod<int>(1);
12+
GenericMethod<double>(2);
13+
14+
GenericMethod<int>(1);
15+
16+
GenericMethod<int>();
17+
GenericMethod<double>();
18+
19+
GenericMethod2<int>(1);
20+
GenericMethod2<double>(2);
21+
22+
GenericMethod<int>();
23+
GenericMethod<double>();
24+
25+
await Task.Delay(1);
26+
27+
return input.ToUpper(CultureInfo.InvariantCulture);
28+
}
29+
30+
[Tracing]
31+
private T GenericMethod<T>(T x)
32+
{
33+
return default;
34+
}
35+
36+
[Tracing]
37+
private T GenericMethod<T>()
38+
{
39+
return default;
40+
}
41+
42+
[Tracing]
43+
private void GenericMethod2<T>(T x)
44+
{
45+
}
46+
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace AWS.Lambda.Powertools.Tracing.Tests.Handlers;
66

7-
public sealed class ExceptionFunctionHandlerTests
7+
public sealed class HandlerTests
88
{
99
[Fact]
1010
public async Task Stack_Trace_Included_When_Decorator_Present()
@@ -20,4 +20,17 @@ public async Task Stack_Trace_Included_When_Decorator_Present()
2020
Assert.StartsWith("at AWS.Lambda.Powertools.Tracing.Tests.Handlers.ExceptionFunctionHandler.ThisThrows()", tracedException.StackTrace?.TrimStart());
2121

2222
}
23+
24+
[Fact]
25+
public async Task When_Decorator_Present_In_Generic_Method_Should_Not_Throw_When_Type_Changes()
26+
{
27+
// Arrange
28+
var handler = new FunctionHandlerForGeneric();
29+
30+
// Act
31+
await handler.Handle("whatever");
32+
33+
// Assert
34+
35+
}
2336
}

0 commit comments

Comments
 (0)