fix(event-handler): prevent OpenAPI schema bleed when reusing response dictionaries#7952
Merged
leandrodamascena merged 4 commits intoaws-powertools:developfrom Jan 19, 2026
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
approved these changes
Jan 19, 2026
Contributor
leandrodamascena
left a comment
There was a problem hiding this comment.
Hey @oyiz-michael thanks a lot for this PR! Approved.
hjgraca
approved these changes
Jan 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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.