-
Notifications
You must be signed in to change notification settings - Fork 432
Bug: Pydantic objects always serialized using alias #6728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
I think I may have found the root cause: powertools-lambda-python/aws_lambda_powertools/event_handler/middlewares/openapi_validation.py Line 165 in d89f1f9
When |
Hi @dehanjl, thanks for opening this issue. Yes, the default (and unchangeable so far) behavior is to serialize by alias, because when working with HTTP requests, fields can contain potentially characters like While I don't know the whole impact of allowing the customer to serialize by field instead of alias, I would consider thinking about a way for the customer to change this behavior, if it makes sense. Thanka |
Hi @leandrodamascena, and thanks for getting back to me. That perspective makes sense. So my exact use-case is that we have some Pydantic models that define our existing API contract ( If I can add Unfortunately, I can't do this because now the legacy model would be serialized by the snake case aliases, breaking the API contract. So I guess my main concerns are as followings:
However, points 1 and 4 may just be skill issues and that I missed something. So my guesses (options) for a solution/way forwarded would be:
I would also be happy with a documentation update that makes it explicit that when using a validator, serialization is done using an alias. |
Uh oh!
There was an error while loading. Please reload this page.
Expected Behaviour
The default behavior of Pydantic is to model dump using
by_alias=False
. Lambda powertools seems to override this somehow.Expected return from code snippet below:
Note: When I remove the alias generator, and set the fields manually, the issue does not occur; hence why I suspect this has something to do with aliases.
Current Behaviour
Currently, when creating a Pydantic object that has an alias generator, and model dumping it explicitly using
by_alias=False
before returning it from an API Gateway Rest Resolver endpoint, it is always return by the alias, even when it should not be.Actually returned from code snippet below:
Code snippet
Possible Solution
No response
Steps to Reproduce
I have created a sample repository where I isolated the issue: https://github.com/dehanjl/lambda-powertools-serialization-test.
I've investigated the serializer and the layers it goes into; but nowhere that I can see is
by_alias=True
passed.powertools-lambda-python/aws_lambda_powertools/event_handler/api_gateway.py
Line 1623 in f106e36
https://github.com/aws-powertools/powertools-lambda-python/blob/f106e368cf760b3585e36ebf7dc0f65a62c632b8/aws_lambda_powertools/shared/json_encoder.py
powertools-lambda-python/aws_lambda_powertools/shared/functions.py
Line 239 in f106e36
powertools-lambda-python/aws_lambda_powertools/event_handler/openapi/compat.py
Line 230 in f106e36
Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.12
Packaging format used
Lambda Layers
Debugging logs
The text was updated successfully, but these errors were encountered: