Skip to content

Conversation

@oyiz-michael
Copy link
Contributor

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

Issue number: closes #6983

Summary

Changes
This PR adds per-route enable_validation control to Event Handler, allowing routes to override the resolver-level validation setting. This enables incremental adoption of OpenAPI validation in large monolithic Lambda functions.

Implementation:

Added enable_validation parameter (bool | None) to the Route class
Updated all route decorators to accept enable_validation
Modified middleware stack builder to check route-level setting before resolver-level setting
Extended support to BedrockAgentResolver and async middleware chains
Route-level setting overrides resolver-level when explicitly set; None inherits from resolver
Fully backwards compatible - existing code works without changes

User experience
Before:

# Validation was all-or-nothing at the resolver level
app = APIGatewayRestResolver(enable_validation=True)

# ALL routes must have valid OpenAPI schemas
# No way to incrementally adopt validation in a large monolithic Lambda
@app.get("/new-endpoint")  # Must be validated
def new_handler(): ...

@app.get("/legacy-endpoint")  # Also must be validated - breaks if not ready!
def legacy_handler(): ...

Users with large monolithic Lambdas couldn't adopt validation incrementally without migrating all endpoints at once, which is time-consuming and risky.

After:

# Enable validation globally
app = APIGatewayRestResolver(enable_validation=True)

# New/critical endpoints use validation (inherit from resolver)
@app.get("/new-endpoint")
def new_handler() -> NewResponse:
    return NewResponse(...)

# Legacy endpoints can opt-out during migration
@app.get("/legacy-endpoint", enable_validation=False)
def legacy_handler():
    return {"status": "ok"}  # No validation required

# Gradually migrate routes over time without breaking existing functionality

This enables teams to:
Enable validation globally on the resolver
Selectively disable it for legacy routes not yet ready
Migrate routes incrementally at their own pace
Avoid breaking changes during gradual adoption

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.

…nd decorators

- Add enable_validation parameter to Route class constructor
- Update all route decorators to accept enable_validation parameter
- Modify _build_middleware_stack to check route-level validation setting
- Route-level setting overrides resolver-level when explicitly set
- Maintains backwards compatibility (None inherits from resolver)

Addresses aws-powertools#6983
…olver

- Update all HTTP method decorators to accept enable_validation parameter
- Pass enable_validation to parent class methods
- Ensures type safety and consistency across all resolvers

Addresses aws-powertools#6983
…ware chain

- Update _run_middleware_chain_async to check route-level validation
- Conditionally add validation middlewares based on effective setting
- Supports async/await pattern with per-route validation control

Addresses aws-powertools#6983
- Test explicit route-level enable_validation=True
- Test disabling validation on specific routes when globally enabled
- Test request body and response validation with per-route settings
- Test inheritance behavior and mixed validation scenarios
- Test Pydantic v2 compatibility
- All tests passing with full coverage

Addresses aws-powertools#6983
- Demonstrate incremental validation adoption for monolithic Lambda
- Show validated routes inheriting global setting
- Show legacy routes with enable_validation=False
- Practical example for migration scenarios

Addresses aws-powertools#6983
@oyiz-michael oyiz-michael requested a review from a team as a code owner January 23, 2026 20:31
@oyiz-michael oyiz-michael requested a review from hjgraca January 23, 2026 20:31
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 23, 2026
oyiz-michael and others added 2 commits January 23, 2026 20:31
- Move type: ignore comment to function definition line
- Properly suppress return type mismatch warning in test
@oyiz-michael oyiz-michael changed the title Feat/per route validation feat(event-handler): add per-route validation support Jan 23, 2026
Use same pattern as other validation tests in codebase
Use typing.cast instead of type: ignore comment to properly handle
intentional type mismatch in validation error test. This satisfies
both mypy and SonarCloud while maintaining test functionality.
@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.

Feature request: Add enable_validation per route

1 participant