From 383718225b0d4ece4f75191a83ac0cd2eb23c03f Mon Sep 17 00:00:00 2001 From: Manish Ram Date: Mon, 16 Nov 2026 10:27:49 -0800 Subject: [PATCH] fix(parser): accept API Gateway console test events without headers --- .../utilities/parser/models/apigw.py | 6 +++ .../apiGatewayProxyEventConsoleTest.json | 45 +++++++++++++++++++ tests/unit/parser/_pydantic/test_apigw.py | 11 +++++ 3 files changed, 62 insertions(+) create mode 100644 tests/events/apiGatewayProxyEventConsoleTest.json diff --git a/aws_lambda_powertools/utilities/parser/models/apigw.py b/aws_lambda_powertools/utilities/parser/models/apigw.py index 48369a42f71..d8d06aa8ff5 100644 --- a/aws_lambda_powertools/utilities/parser/models/apigw.py +++ b/aws_lambda_powertools/utilities/parser/models/apigw.py @@ -103,6 +103,12 @@ class APIGatewayProxyEventModel(BaseModel): isBase64Encoded: bool | None = None body: str | type[BaseModel] | None = None + @field_validator("headers", "multiValueHeaders", mode="before") + @classmethod + def _null_headers_as_empty(cls, value): + # The Test tab in the API Gateway console sends null headers when the test has none + return {} if value is None else value + class ApiGatewayAuthorizerToken(BaseModel): type: Literal["TOKEN"] diff --git a/tests/events/apiGatewayProxyEventConsoleTest.json b/tests/events/apiGatewayProxyEventConsoleTest.json new file mode 100644 index 00000000000..156c3cc7342 --- /dev/null +++ b/tests/events/apiGatewayProxyEventConsoleTest.json @@ -0,0 +1,45 @@ +{ + "resource": "/no-auth", + "path": "/no-auth", + "httpMethod": "POST", + "headers": null, + "multiValueHeaders": null, + "queryStringParameters": null, + "multiValueQueryStringParameters": null, + "pathParameters": null, + "stageVariables": null, + "requestContext": { + "resourceId": "3nq1cq", + "resourcePath": "/no-auth", + "httpMethod": "POST", + "extendedRequestId": "ZhGnYGtqjoEFyzw=", + "requestTime": "17/Jun/2024:15:08:18 +0000", + "path": "/no-auth", + "accountId": "123456789012", + "protocol": "HTTP/1.1", + "stage": "test-invoke-stage", + "domainPrefix": "testPrefix", + "requestTimeEpoch": 1718636898112, + "requestId": "28422e1a-c975-42da-895e-165f882dafef", + "identity": { + "cognitoIdentityPoolId": null, + "cognitoIdentityId": null, + "apiKey": "test-invoke-api-key", + "principalOrgId": null, + "cognitoAuthenticationType": null, + "userArn": "arn:aws:sts::123456789012:assumed-role/Admin/my-user", + "apiKeyId": "test-invoke-api-key-id", + "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15", + "accountId": "123456789012", + "caller": "ABCAXZWZ5ZDPNIH6C6ODK:my-user", + "sourceIp": "test-invoke-source-ip", + "accessKey": "ABCAXZWZ5ZDPFDRIWYXX", + "cognitoAuthenticationProvider": null, + "user": "ABCAXZWZ5ZDPNIH6C6ODK:my-user" + }, + "domainName": "testPrefix.testDomainName", + "apiId": "puhdx84jy9" + }, + "body": null, + "isBase64Encoded": false +} diff --git a/tests/unit/parser/_pydantic/test_apigw.py b/tests/unit/parser/_pydantic/test_apigw.py index fcd543b087c..0ed0712d160 100644 --- a/tests/unit/parser/_pydantic/test_apigw.py +++ b/tests/unit/parser/_pydantic/test_apigw.py @@ -183,6 +183,17 @@ def test_apigw_event_empty_body(): parse(event=event, model=APIGatewayProxyEventModel) +def test_apigw_event_from_console_test_without_headers(): + # The Test tab in the API Gateway console sends null headers when the test has none + raw_event = load_event("apiGatewayProxyEventConsoleTest.json") + parsed_event: APIGatewayProxyEventModel = parse(event=raw_event, model=APIGatewayProxyEventModel) + + assert parsed_event.headers == {} + assert parsed_event.multiValueHeaders == {} + assert parsed_event.requestContext.stage == "test-invoke-stage" + assert parsed_event.requestContext.identity.sourceIp == "test-invoke-source-ip" + + def test_apigw_event_authorizer_token(): raw_event = load_event("apiGatewayAuthorizerTokenEvent.json") parsed_event: ApiGatewayAuthorizerToken = ApiGatewayAuthorizerToken(**raw_event)