Repository navigation
fix: undo changes of failed middlewares - #318
Merged
Merged
Conversation
Alternatives of a middleware step shared one mutable request: an alternative that changed the JSON body and then raised left its partial changes for the next alternative. The handler now copies the body before each step and restores it, in place, after each failed alternative, so every alternative starts from the body as it was before the step. The middleware interface is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Take the snapshot of the body only for steps with more than one alternative, and skip the restore after the last one. The docs say that only the JSON body is restored, not other request state or a replaced body. Refs #313 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideMiddleware alternatives now receive an isolated JSON body state: the handler snapshots object bodies for multi-alternative steps and restores mutations in place after each failed non-final attempt, while documentation and tests define and verify the supported scope. Sequence diagram for restoring middleware request bodiessequenceDiagram
participant Handler as MiddlewareHandler
participant Request as flask.Request
participant Alternative as Middleware alternative
Handler->>Request: deepcopy(request.json)
loop Alternatives in middleware step
Handler->>Alternative: apply(request)
alt middleware succeeds
Alternative-->>Handler: return
else middleware fails and another alternative remains
Alternative-->>Handler: raise exception
Handler->>Handler: _restore_body(request, snapshot)
Handler->>Request: clear body and update snapshot
else final middleware fails
Alternative-->>Handler: raise exception
Handler-->>Handler: raise MiddlewareException
end
end
Flow diagram for middleware alternative body restorationflowchart TD
A[Start middleware step] --> B{More than one alternative?}
B -- Yes --> C[Snapshot request.json]
B -- No --> D[No snapshot]
C --> E[Try middleware alternative]
D --> E
E --> F{Applied successfully?}
F -- Yes --> G[Continue to next step]
F -- No, another alternative --> H[_restore_body]
H --> I[Try next alternative]
I --> F
F -- No, final alternative --> J[Raise MiddlewareException]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
uniqueg
marked this pull request as ready for review
October 24, 2026 23:06
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #318 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 29 29
Lines 2469 2479 +10
=========================================
+ Hits 2469 2479 +10 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Alternatives of a middleware step share one request. An alternative that changed the JSON body and then failed left its partial changes for the next alternative. The handler now takes a snapshot of the body for steps with more than one alternative and restores it in place after each failed alternative (not after the last one, which raises anyway). The docs say plainly that only the JSON body is restored: not headers, environ or request attributes, and not a body that was replaced instead of changed. #315 replaces this mechanism with a copy of the task per alternative.
Verification
Run locally:
mkdocs build --strict: passedReviewed with the adversarial and quality reviewer subagents: no high or medium findings. Both low findings are fixed (snapshot only when there are alternatives; docs on what is restored).
Not run locally: the Docker Compose smoke test and the MongoDB 7.0 CI entry.
Open questions
None.
Closes #313
🤖 Generated with Claude Code
Summary by Sourcery
Isolate middleware alternatives by restoring the request JSON body after failed attempts.
Bug Fixes:
Enhancements:
Documentation:
Tests: