Skip to content

Commit

Permalink
fix(parser): body/QS can be null or omitted in apigw v1/v2 (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
heitorlessa authored Nov 13, 2021
1 parent e34469a commit 0728aa2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion aws_lambda_powertools/utilities/parser/models/apigw.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ class APIGatewayProxyEventModel(BaseModel):
pathParameters: Optional[Dict[str, str]]
stageVariables: Optional[Dict[str, str]]
isBase64Encoded: bool
body: str
body: Optional[str]
4 changes: 2 additions & 2 deletions aws_lambda_powertools/utilities/parser/models/apigwv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class APIGatewayProxyEventV2Model(BaseModel):
rawQueryString: str
cookies: Optional[List[str]]
headers: Dict[str, str]
queryStringParameters: Dict[str, str]
queryStringParameters: Optional[Dict[str, str]]
pathParameters: Optional[Dict[str, str]]
stageVariables: Optional[Dict[str, str]]
requestContext: RequestContextV2
body: str
body: Optional[str]
isBase64Encoded: bool
8 changes: 7 additions & 1 deletion tests/functional/parser/test_apigw.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from pydantic import ValidationError

from aws_lambda_powertools.utilities.parser import envelopes, event_parser
from aws_lambda_powertools.utilities.parser import envelopes, event_parser, parse
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
from aws_lambda_powertools.utilities.typing import LambdaContext
from tests.functional.parser.schemas import MyApiGatewayBusiness
Expand Down Expand Up @@ -144,3 +144,9 @@ def test_apigw_event_with_invalid_websocket_request():
expected_msg = "messageId is available only when the `eventType` is `MESSAGE`"
assert errors[0]["msg"] == expected_msg
assert expected_msg in str(err.value)


def test_apigw_event_empty_body():
event = load_event("apiGatewayProxyEvent.json")
event["body"] = None
parse(event=event, model=APIGatewayProxyEventModel)
15 changes: 14 additions & 1 deletion tests/functional/parser/test_apigwv2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from aws_lambda_powertools.utilities.parser import envelopes, event_parser
from aws_lambda_powertools.utilities.parser import envelopes, event_parser, parse
from aws_lambda_powertools.utilities.parser.models import (
APIGatewayProxyEventV2Model,
RequestContextV2,
Expand Down Expand Up @@ -90,3 +90,16 @@ def test_api_gateway_proxy_v2_event_iam_authorizer():
assert iam.principalOrgId == "AwsOrgId"
assert iam.userArn == "arn:aws:iam::1234567890:user/Admin"
assert iam.userId == "AROA2ZJZYVRE7Y3TUXHH6"


def test_apigw_event_empty_body():
event = load_event("apiGatewayProxyV2Event.json")
event.pop("body") # API GW v2 removes certain keys when no data is passed
parse(event=event, model=APIGatewayProxyEventV2Model)


def test_apigw_event_empty_query_strings():
event = load_event("apiGatewayProxyV2Event.json")
event["rawQueryString"] = ""
event.pop("queryStringParameters") # API GW v2 removes certain keys when no data is passed
parse(event=event, model=APIGatewayProxyEventV2Model)

0 comments on commit 0728aa2

Please sign in to comment.