Closed
Description
Using parser() to parse Event for API Gateway Lambda.
APIGatewayProxyEventModel has version as mandatory attribute
APIGatewayEventRequestContext has authorizer as mandatory attribute
These are not present in ALL Lambda events from API Gateway via Lambda Proxy integration.
What were you trying to accomplish?
Trying to parse payload as a Model according to documentation on parser
Expected Behavior
To correctly parse the API Gateway Event passed to Lambda without throwing an exception
Current Behavior
Validation exception is thrown for missing fields.
Possible Solution
class APIGatewayEventRequestContext(BaseModel):
...
authorizer: Optional[APIGatewayEventAuthorizer]
...
class APIGatewayProxyEventModel(BaseModel):
version: Optional[str]
...
Steps to Reproduce (for bugs)
import json
from aws_lambda_powertools.utilities.parser import BaseModel, envelopes
from aws_lambda_powertools.logging import Logger
from aws_lambda_powertools.utilities.parser.parser import parse
# Specify the API payload Model expected
class UserModel(BaseModel):
username: str
password1: str
password2: str
logger = Logger()
def test(event, context):
parsed_event = parse(event=event, model=UserModel, envelope=envelopes.ApiGatewayEnvelope)
response = {
"statusCode": 200,
"body": json.dumps(event.dict())
}
return response
Environment
- Powertools version used: 1.17.1
- Packaging format (Layers, PyPi): PyPi
- AWS Lambda function runtime: 3.8
- Debugging logs
[ERROR] ValidationError: 2 validation errors for APIGatewayProxyEventModel
version
field required (type=value_error.missing)
requestContext -> authorizer
field required (type=value_error.missing)
Traceback (most recent call last):
File "/var/task/handler.py", line 34, in test
parsed_event = parse(event=event, model=UserModel, envelope=envelopes.ApiGatewayEnvelope)
File "/var/task/aws_lambda_powertools/utilities/parser/parser.py", line 148, in parse
return envelope().parse(data=event, model=model)
File "/var/task/aws_lambda_powertools/utilities/parser/envelopes/apigw.py", line 30, in parse
parsed_envelope = APIGatewayProxyEventModel.parse_obj(data)
File "/var/task/pydantic/main.py", line 578, in parse_obj
return cls(**obj)
File "/var/task/pydantic/main.py", line 406, in __init__
raise validation_error
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
boring-cyborg commentedon Jul 17, 2021
Thanks for opening your first issue here! We'll come back to you as soon as we can.
heitorlessa commentedon Jul 17, 2021
Hey @walmsles, thanks for flagging it!
Could you give an example of an event that this isn't true so we can flag the official aws docs too?
Happy to accept a PR to make this optional, or work on this right after a Mypy fix I'm working
walmsles commentedon Jul 17, 2021
Hey have submitted a PR for this (just now :-)).
My PR is consistent with sample events here
The event samples (above and below) are also consistent with output from: sam local generate-event apigateway aws-proxy
Event from API GW:
heitorlessa commentedon Jul 17, 2021
Staged for next week's release - Friday the latest. Let me know if this is impacting production and I can bring it forward
walmsles commentedon Jul 17, 2021
All good, not urgent at all. Thanks 👍
heitorlessa commentedon Jul 20, 2021
hey @walmsles - This is now fixed in 1.18.0
More details in the release notes: https://github.com/awslabs/aws-lambda-powertools-python/releases/tag/v1.18.0