fix: complete emulator REST proxy events and null-safe AspNetCoreServ… · aws/aws-lambda-dotnet@3261a1a · GitHub
Skip to content

Commit 3261a1a

Browse files
fix: complete emulator REST proxy events and null-safe AspNetCoreServer marshalling (#2567)
API Gateway emulator REST translation omitted RequestContext and could leave Path/HttpMethod null, causing NullReferenceException in Amazon.Lambda.AspNetCoreServer. Fixes #2566 Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 84772ad commit 3261a1a

4 files changed

Lines changed: 72 additions & 13 deletions

File tree

Lines changed: 18 additions & 0 deletions

‎Libraries/src/Amazon.Lambda.AspNetCoreServer/APIGatewayProxyFunction.cs‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ protected override Amazon.Lambda.Core.ResponseStreaming.HttpResponseStreamPrelud
157157
/// <param name="lambdaContext"></param>
158158
protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxyRequest apiGatewayRequest, ILambdaContext lambdaContext)
159159
{
160+
if (apiGatewayRequest == null)
161+
{
162+
_logger.LogError("MarshallRequest: apiGatewayRequest is null");
163+
throw new ArgumentNullException(nameof(apiGatewayRequest));
164+
}
165+
166+
_logger.LogDebug("MarshallRequest: Path={Path}, HttpMethod={HttpMethod}, Resource={Resource}, RequestContext={HasContext}",
167+
apiGatewayRequest.Path ?? "(null)",
168+
apiGatewayRequest.HttpMethod ?? "(null)",
169+
apiGatewayRequest.Resource ?? "(null)",
170+
apiGatewayRequest.RequestContext != null);
171+
160172
{
161173
var authFeatures = (IHttpAuthenticationFeature)features;
162174

@@ -201,7 +213,8 @@ protected override void MarshallRequest(InvokeFeatures features, APIGatewayProxy
201213
var rawQueryString = Utilities.CreateQueryStringParameters(
202214
apiGatewayRequest.QueryStringParameters, apiGatewayRequest.MultiValueQueryStringParameters, true);
203215

204-
requestFeatures.RawTarget = apiGatewayRequest.Path + rawQueryString;
216+
var pathForTarget = apiGatewayRequest.Path ?? "/";
217+
requestFeatures.RawTarget = pathForTarget + rawQueryString;
205218
requestFeatures.QueryString = rawQueryString;
206219
requestFeatures.Path = path;
207220

@@ -365,24 +378,24 @@ protected virtual string ParseHttpPath(APIGatewayProxyRequest apiGatewayRequest)
365378
if (apiGatewayRequest.PathParameters != null && apiGatewayRequest.PathParameters.TryGetValue("proxy", out var proxy) &&
366379
!string.IsNullOrEmpty(apiGatewayRequest.Resource))
367380
{
368-
var proxyPath = proxy;
381+
var proxyPath = proxy ?? "";
369382
path = apiGatewayRequest.Resource.Replace("{proxy+}", proxyPath);
370383

371384
// Adds all the rest of non greedy parameters in apiGateway.Resource to the path
372385
foreach (var pathParameter in apiGatewayRequest.PathParameters.Where(pp => pp.Key != "proxy"))
373386
{
374-
path = path.Replace($"{{{pathParameter.Key}}}", pathParameter.Value);
387+
path = path.Replace($"{{{pathParameter.Key}}}", pathParameter.Value ?? "");
375388
}
376389
}
377390

378391
if (string.IsNullOrEmpty(path))
379392
{
380-
path = apiGatewayRequest.Path;
393+
path = apiGatewayRequest.Path ?? "/";
381394
}
382395

383-
if (!path.StartsWith("/"))
396+
if (string.IsNullOrEmpty(path) || !path.StartsWith("/"))
384397
{
385-
path = "/" + path;
398+
path = "/" + (path ?? "");
386399
}
387400
return path;
388401
}
@@ -394,7 +407,7 @@ protected virtual string ParseHttpPath(APIGatewayProxyRequest apiGatewayRequest)
394407
/// </summary>
395408
protected virtual string ParseHttpMethod(APIGatewayProxyRequest apiGatewayRequest)
396409
{
397-
return apiGatewayRequest.HttpMethod;
410+
return apiGatewayRequest.HttpMethod ?? "GET";
398411
}
399412

400413
/// <summary>

‎Libraries/src/Amazon.Lambda.AspNetCoreServer/Internal/Utilities.cs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,21 @@ internal static void SetHeadersCollection(IHeaderDictionary headers, IDictionary
190190
{
191191
foreach (var kvp in multiValues)
192192
{
193-
headers[kvp.Key] = new StringValues(kvp.Value.ToArray());
193+
var values = kvp.Value?.Where(v => v != null).ToArray() ?? Array.Empty<string>();
194+
if (values.Length > 0)
195+
{
196+
headers[kvp.Key] = new StringValues(values);
197+
}
194198
}
195199
}
196200
else if (singleValues?.Count > 0)
197201
{
198202
foreach (var kvp in singleValues)
199203
{
200-
headers[kvp.Key] = new StringValues(kvp.Value);
204+
if (kvp.Value != null)
205+
{
206+
headers[kvp.Key] = new StringValues(kvp.Value);
207+
}
201208
}
202209
}
203210
}

‎Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Extensions/HttpContextExtensions.cs‎

Lines changed: 25 additions & 4 deletions

0 commit comments

Comments
 (0)