Skip to content

fix: Middleware Pipeline — Closure Captures Wrong Request#122

Open
s2x wants to merge 3 commits intomasterfrom
fix/middleware-pipeline-closure-captures-wrong-request
Open

fix: Middleware Pipeline — Closure Captures Wrong Request#122
s2x wants to merge 3 commits intomasterfrom
fix/middleware-pipeline-closure-captures-wrong-request

Conversation

@s2x
Copy link
Copy Markdown
Collaborator

@s2x s2x commented Apr 12, 2026

Summary

Fixes a critical bug in the middleware pipeline where middleware modifications to the request object were not propagating to subsequent middleware in the chain.

Problem

In HttpRequestHandler, the closure in the middleware loop was capturing $request from the outer scope instead of using the $input parameter:

// Before (buggy)
$next = fn(Request $input): Http\Response => $middleware($request, $next);

This caused all middleware to receive the original request, ignoring any modifications made by previous middleware.

Solution

Changed the closure to use the $input parameter:

// After (fixed)
$next = fn(Request $input): Http\Response => $middleware($input, $next);

Testing

Added comprehensive tests in MiddlewarePipelineTest.php that verify:

  1. Middleware can modify request headers
  2. Subsequent middleware sees the modified request
  3. The fix works correctly with 3+ middleware in the chain

Breaking Changes

None.

References

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug in Middleware Pipeline — Closure Captures Wrong Request

1 participant