Skip to content

Commit 0e10c0c

Browse files
authored
Merge pull request #791 from gregsinclair42/CheckForValidLambdaContext
fix(logging): check if ILambdaContext is valid in LoggingLambdaContext.Extract
2 parents 08efcb2 + 1dc566e commit 0e10c0c

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Internal/LoggingLambdaContext.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,21 @@ public static bool Extract(AspectEventArgs args)
7474
return false;
7575

7676
var index = Array.FindIndex(args.Method.GetParameters(), p => p.ParameterType == typeof(ILambdaContext));
77-
if (index >= 0)
78-
{
79-
var x = (ILambdaContext)args.Args[index];
80-
81-
Instance = new LoggingLambdaContext
82-
{
83-
AwsRequestId = x.AwsRequestId,
84-
FunctionName = x.FunctionName,
85-
FunctionVersion = x.FunctionVersion,
86-
InvokedFunctionArn = x.InvokedFunctionArn,
87-
LogGroupName = x.LogGroupName,
88-
LogStreamName = x.LogStreamName,
89-
MemoryLimitInMB = x.MemoryLimitInMB
90-
};
91-
return true;
92-
}
77+
if (index < 0 || args.Args[index] == null || args.Args[index] is not ILambdaContext) return false;
78+
79+
var x = (ILambdaContext)args.Args[index];
9380

94-
return false;
81+
Instance = new LoggingLambdaContext
82+
{
83+
AwsRequestId = x.AwsRequestId,
84+
FunctionName = x.FunctionName,
85+
FunctionVersion = x.FunctionVersion,
86+
InvokedFunctionArn = x.InvokedFunctionArn,
87+
LogGroupName = x.LogGroupName,
88+
LogStreamName = x.LogStreamName,
89+
MemoryLimitInMB = x.MemoryLimitInMB
90+
};
91+
return true;
9592
}
9693

9794
/// <summary>

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Context/LambdaContextTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ public void Extract_WhenHasLambdaContextArgument_InitializesLambdaContextInfo()
5656
Assert.Null(LoggingLambdaContext.Instance);
5757
}
5858

59+
[Fact]
60+
public void Extract_When_LambdaContext_Is_Null_But_Not_First_Parameter_Returns_False()
61+
{
62+
// Arrange
63+
ILambdaContext lambdaContext = null;
64+
var args = Substitute.For<AspectEventArgs>();
65+
var method = Substitute.For<MethodInfo>();
66+
var parameter1 = Substitute.For<ParameterInfo>();
67+
var parameter2 = Substitute.For<ParameterInfo>();
68+
69+
// Setup parameters
70+
parameter1.ParameterType.Returns(typeof(string));
71+
parameter2.ParameterType.Returns(typeof(ILambdaContext));
72+
73+
// Setup method
74+
method.GetParameters().Returns(new[] { parameter1, parameter2 });
75+
76+
// Setup args
77+
args.Method = method;
78+
args.Args = new object[] { "requestContext", lambdaContext };
79+
80+
// Act && Assert
81+
LoggingLambdaContext.Clear();
82+
Assert.Null(LoggingLambdaContext.Instance);
83+
Assert.False(LoggingLambdaContext.Extract(args));
84+
}
85+
5986
[Fact]
6087
public void Extract_When_Args_Null_Returns_False()
6188
{

0 commit comments

Comments
 (0)