Description
First time user of AWS Lambda PowerTools and I would just like to say this looks like an amazing library.
I recently came across what I think might be a bug, but not sure. When creating a simple lambda with a APIGatewayProxyEventModel being parsed it throws an error unless I put a request body in my payload. This is due to the body object being None while the model expects a "str".
This issue affects me because it makes it a body a requirement for every request.
Expected Behavior
It should execute as expected with body being an object that can be independently parsed.
Current Behavior
When invoking the Steps to Reproduce code I get the following in CloudWatch. As such the current behavior is that if body is a None object it will fail validation.
[ERROR] ValidationError: 1 validation error for APIGatewayProxyEventModel
body
none is not an allowed value (type=type_error.none.not_allowed)
Traceback (most recent call last):
File "/opt/python/aws_lambda_powertools/middleware_factory/factory.py", line 133, in wrapper
response = middleware()
File "/var/task/get_preference.py", line 18, in api_handler
response = handler(event, context)
File "/opt/python/aws_lambda_powertools/middleware_factory/factory.py", line 133, in wrapper
response = middleware()
File "/opt/python/aws_lambda_powertools/utilities/parser/parser.py", line 84, in event_parser
parsed_event = parse(event=event, model=model, envelope=envelope)
File "/opt/python/aws_lambda_powertools/utilities/parser/parser.py", line 157, in parse
return model.parse_obj(event)
File "/opt/python/pydantic/main.py", line 578, in parse_obj
return cls(**obj)
File "/opt/python/pydantic/main.py", line 406, in __init__
raise validation_error
Possible Solution
Make body an optional object.
Steps to Reproduce (for bugs)
Simple Code should help you reproduce it:
import json
from aws_lambda_powertools import Logger, Tracer, Metrics
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.utilities.parser import event_parser
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger()
tracer = Tracer()
metrics = Metrics()
@lambda_handler_decorator
def api_handler(handler, event, context, status_code):
response = handler(event, context)
return {
"statusCode": status_code,
"body": json.dumps(response)
}
@api_handler(status_code=200)
@event_parser(model=APIGatewayProxyEventModel)
def handler(event: APIGatewayProxyEventModel, _context=LambdaContext()):
return event
Environment
- Powertools version used: 1.21.1
- Packaging format (Layers, PyPi): PyPi
- AWS Lambda function runtime: 3.9 -> ARM
- Debugging logs