-
Notifications
You must be signed in to change notification settings - Fork 469
fix(event-handler): prevent OpenAPI schema bleed when reusing response dictionaries #7952
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
fix(event-handler): prevent OpenAPI schema bleed when reusing response dictionaries #7952
Conversation
…e dictionaries Fixes aws-powertools#7711 When multiple routes shared the same response dictionary object, the OpenAPI schema generator was mutating the shared dictionary by directly modifying it. This caused schema bleeding where one route's return type would incorrectly appear in another route's OpenAPI schema. The fix uses copy.deepcopy() to create independent copies of response dictionaries before mutation, ensuring each route gets its own correct OpenAPI schema based on its return type annotation.
Relates to aws-powertools#7711 Add comprehensive tests to verify that when multiple routes share the same response dictionary, each route gets its own correct OpenAPI schema without bleeding return types between routes. Tests cover: - Different return types (list vs single object) with shared responses - Verification that shared dictionaries are not mutated - Regression testing for standard behavior
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #7952 +/- ##
========================================
Coverage 96.72% 96.72%
========================================
Files 278 278
Lines 13626 13627 +1
Branches 1083 1083
========================================
+ Hits 13180 13181 +1
Misses 327 327
Partials 119 119 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
leandrodamascena
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @oyiz-michael thanks a lot for this PR! Approved.



Issue number: closes #7711
Summary
Changes
This PR fixes a bug where OpenAPI schema return types were bleeding across routes when reusing response dictionaries. The issue occurred when multiple routes shared the same response dictionary object (e.g., via
Responses.combine()), causing the schema generator to mutate the shared dictionary and incorrectly apply one route's return type schema to all routes.Root cause: The
_add_route_to_openapi_routesmethod was directly referencing the shared response dictionary, so modifications made during schema generation affected all routes using that dictionary.Solution: Changed line 670 in
api_gateway.pyto usecopy.deepcopy()to create an independent copy of the response dictionary before any modifications, ensuring each route's schema remains isolated.Files changed:
aws_lambda_powertools/event_handler/api_gateway.py: Addedimport copyand modified response dictionary handling to usedeepcopy()tests/functional/event_handler/_pydantic/test_openapi_shared_response_bleed.py: Added comprehensive regression testsUser experience
Before:
The generated OpenAPI schema would incorrectly show both routes returning the same type (whichever was processed last), instead of list[ExamSummary] and ExamConfig respectively.
After:
Each route correctly maintains its own return type schema in the OpenAPI specification, regardless of shared response dictionaries. The /exams route shows list[ExamSummary] and the /exam/<exam_id>/config route shows ExamConfig as expected.
Testing:
Added 2 new regression tests that verify no schema bleed occurs
Code quality checks pass: ruff format, ruff check, mypy
All 323 existing event handler tests pass
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.