Skip to content

Conversation

@oyiz-michael
Copy link
Contributor

@oyiz-michael oyiz-michael commented Jan 19, 2026

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_routes method 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.py to use copy.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: Added import copy and modified response dictionary handling to use deepcopy()
  • tests/functional/event_handler/_pydantic/test_openapi_shared_response_bleed.py: Added comprehensive regression tests

User experience

Before:

from aws_lambda_powertools.event_handler import APIGatewayRestResolver

responses = Responses.combine(
    Responses.success,
    Responses.not_found
)

@app.get("/exams", responses=responses)
def list_exams() -> list[ExamSummary]:
    ...

@app.get("/exam/<exam_id>/config", responses=responses)
def get_exam_config(exam_id: str) -> ExamConfig:
    ...

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.

…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
@oyiz-michael oyiz-michael requested a review from a team as a code owner January 19, 2026 00:14
@oyiz-michael oyiz-michael requested a review from sdangol January 19, 2026 00:14
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 19, 2026
@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event_handlers size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: OpenAPI schema return types bleed across routes when reusing response dictionaries

1 participant