question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: APIGatewayProxyEventV2Model not deserializing str to custom model

See original GitHub issue

Expected 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:closed
  • Created a year ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
nleipicommented, Sep 16, 2022

I think the correct way to handle this is to use pydantics own Json type as a wrapper for the model.

class MyEventModel(APIGatewayProxyEventV2Model):
    body: Json[OrderCancelationRequest]

That should be definitely highlighted in the documentation.

1reaction
rubenfonsecacommented, Sep 20, 2022

Thank you so much for your input! I’ve opened a PR now to add this use case to our parser docs 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found