Skip to content

feat(event_handler): enable support for custom deserializer to parse the request body #6601

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

LucasCMFBraga
Copy link

Issue number: #6600

Summary

This feature allows to pass a custom deserializer to ensure accurate handling of floating-point precision.

Changes

Added support for passing a custom deserializer instance to ApiGatewayResolver to handle JSON numeric values as Decimal, ensuring correct floating-point precision.

`
from tests.functional.utils import load_even
import json
from decimal import Decimal

app = ApiGatewayResolver(deserializer=partial(json.loads, parse_float=Decimal))
`

User experience

Please share what the user experience looks like before and after this change

`
app = ApiGatewayResolver(deserializer=partial(json.loads, parse_float=Decimal))

@app.post("/my/path")
def test_handler():
    return app.current_event.json_body

# WHEN calling the event handler
event = {}
event.update(LOAD_GW_EVENT)
event["body"] = '{"amount": 2.2999999999999998}'
event["httpMethod"] = "POST"

result = app(event, {})
# THEN process event correctly
assert result["statusCode"] == 200
assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON]
assert result["body"] == '{"amount":"2.2999999999999998"}'

`

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@LucasCMFBraga LucasCMFBraga requested a review from a team as a code owner May 2, 2025 17:56
@LucasCMFBraga LucasCMFBraga requested a review from anafalcao May 2, 2025 17:56
Copy link

boring-cyborg bot commented May 2, 2025

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 2, 2025
@leandrodamascena leandrodamascena changed the title feat: Enabled support for a custom deserializer to parse the request body feat(event_handler): enable support for custom deserializer to parse the request body May 12, 2025
@leandrodamascena leandrodamascena linked an issue May 12, 2025 that may be closed by this pull request
2 tasks
@github-actions github-actions bot added the feature New feature or functionality label May 12, 2025
@leandrodamascena
Copy link
Contributor

Hi @LucasCMFBraga, thanks for working on this PR! I've reviewed it and to me it makes sense for us to add these new parameter to propagate it to DataClasses, but I'm wondering if we're parsing float incorrectly and if it would make sense to make it default here: https://github.com/aws-powertools/powertools-lambda-python/blob/develop/aws_lambda_powertools/utilities/data_classes/common.py#L89

WDYT?

Copy link

codecov bot commented May 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.12%. Comparing base (d00c6a1) to head (9448f97).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6601   +/-   ##
========================================
  Coverage    96.12%   96.12%           
========================================
  Files          253      253           
  Lines        12104    12105    +1     
  Branches       902      902           
========================================
+ Hits         11635    11636    +1     
  Misses         369      369           
  Partials       100      100           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature New feature or functionality size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Enable to change the deserializer body
2 participants