Skip to content

fix(event_handler): split OpenAPI validation to respect middleware returns - #7050

Merged
leandrodamascena merged 5 commits into
developfrom
fix/avoid-middleware-validation
Jul 30, 2025
Merged

leandrodamascena merged 5 commits into
developfrom
fix/avoid-middleware-validation

Conversation

@leandrodamascena

@leandrodamascena leandrodamascena commented Jul 28, 2025 •

Copy link
Copy Markdown
Contributor

Issue number: #5228
Discussion: #4656

Summary

Fixes middleware validation issues by splitting OpenAPI validation into separate request and response validation middlewares.

Changes

New Middleware Architecture

Split the OpenAPIValidationMiddleware into two separate middlewares:

  1. OpenAPIRequestValidationMiddleware - Validates incoming requests only
  2. OpenAPIResponseValidationMiddleware - Validates outgoing responses only

Execution Order

┌─────────────────────────────────────────────────────────────────┐
│                    Request Flow                                 │
├─────────────────────────────────────────────────────────────────┤
│ 1. Request Validation Middleware                                │
│    ├─ Validates path params, query params, headers, body       │
│    ├─ If invalid → Returns 422, stops execution               │
│    └─ If valid → Continues to next middleware                  │
│                                                                 │
│ 2. User Middlewares (in order of registration)                 │
│    ├─ Auth middleware                                          │
│    ├─ Rate limiting middleware                                 │
│    ├─ Logging middleware                                       │
│    ├─ etc...                                                   │
│    └─ Can return early responses (401, 403, 429, etc.)        │
│                                                                 │
│ 3. Response Validation Middleware                              │
│    ├─ Only executes if reached (no early returns)             │
│    └─ Validates response against OpenAPI schema               │
│                                                                 │
│ 4. Route Handler                                               │
│    └─ The actual endpoint function                             │
└─────────────────────────────────────────────────────────────────┘

User experience

Breaking change consideration

⚠️ Potential Impact: This fix may affect customers who were working around the previous incorrect behavior by catching RequestValidationError exceptions from middleware responses. However, this was unintended behavior that needed to be corrected.

Why this change is necessary:

  • The previous behavior was a bug - middleware responses should not be validated against OpenAPI schemas
  • Middleware responses (401, 403, 429) are control flow responses, not data responses
  • The fix aligns with expected middleware behavior patterns
  • Continuing the incorrect behavior would prevent proper middleware functionality

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

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.