fix(event-handler): prevent OpenAPI schema bleed when reusing response dictionaries #7952
+176
−1
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.