Bug: APIGatewayProxyEventV2Model not deserializing str to custom model
See original GitHub issueExpected Behaviour
It should be possible to use own model classes for body in APIGatewayProxyEventV2Model
Current Behaviour
When I extend APIGatewayProxyEventV2Model model to include my own model for the body following exception is raised 1 validation error for Event\nbody\n value is not a valid dict (type=type_error.dict)
. I suppose the reason for that is that body of apigw v2 event is a string
Code snippet
from aws_lambda_powertools.utilities.parser import BaseModel
from aws_lambda_powertools.utilities.parser.models import (
APIGatewayProxyEventV2Model
)
class OrderCancelationRequest(BaseModel):
reason: str
orderNo: str
class MyEventModel(APIGatewayProxyEventV2Model):
body: OrderCancelationRequest
Possible Solution
No response
Steps to Reproduce
parse(model=MyEventModel, event=any valid apigw event)
AWS Lambda Powertools for Python version
latest
AWS Lambda function runtime
3.9
Packaging format used
PyPi
Debugging logs
No response
Issue Analytics
- State:
- Created a year ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
Could not cast or convert from System.String to Custom Object ...
I am trying to deserialize JSON text coming from ...
Read more >Error Handling Patterns in Amazon API Gateway and AWS ...
An API Gateway mapping template is defined to deserialize the custom error object and build a custom response based on the fields in...
Read more >API Gateway - Lambda Powertools Python
API Gateway. Event handler for Amazon API Gateway REST/HTTP APIs and Application Loader Balancer (ALB). Key Features¶.
Read more >Resolving “Could not cast or convert from System.String to ...
I faced below error during deserialization - Newtonsoft.Json. ... I had to deserialize as a string and then deserilize again to the model...
Read more >Cannot deserialize the current json object because(e.g.{"name":"
Is not an enumerable of "Root Objects. It contains a list but isn't one. The correct way to deserialize it is: C#. var...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think the correct way to handle this is to use pydantics own Json type as a wrapper for the model.
That should be definitely highlighted in the documentation.
Thank you so much for your input! I’ve opened a PR now to add this use case to our parser docs 😃