Skip to content

🐛 bug: preserve idempotency replay protection for oversized responses#4287

Open
gaby wants to merge 1 commit into
mainfrom
fix-idempotency-for-large-responses
Open

🐛 bug: preserve idempotency replay protection for oversized responses#4287
gaby wants to merge 1 commit into
mainfrom
fix-idempotency-for-large-responses

Conversation

@gaby
Copy link
Copy Markdown
Member

@gaby gaby commented May 16, 2026

Motivation

  • Fix a regression where the idempotency middleware skipped persisting responses larger than BodyLimit after the unsafe handler ran, allowing retries with the same idempotency key to re-execute side effects.
  • Restore the middleware guarantee that duplicate requests with the same idempotency key do not trigger the same server action twice.

Description

  • Removed the BodyLimit-based early return in middleware/idempotency/idempotency.go so successful handler responses are always marshaled and stored for idempotency replay protection.
  • Updated the regression test Test_New_SkipCache_WhenBodyTooLargeTest_New_Cache_WhenBodyTooLarge in middleware/idempotency/idempotency_test.go to assert oversized responses are cached and replayed (asserts count == 1, s.setCount == 1, and WasPutToCache is true for the initial request).
  • The change is minimal and preserves existing behavior for handler errors and lock/storage error paths.

Copilot AI review requested due to automatic review settings May 16, 2026 13:40
@gaby gaby requested a review from a team as a code owner May 16, 2026 13:40
@gaby gaby requested review from ReneWerner87, efectn and sixcolors May 16, 2026 13:40
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 16, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 3bf50d13-04d6-4717-8529-489bf9b1c75e

📥 Commits

Reviewing files that changed from the base of the PR and between 7e4493e and a4fe81a.

📒 Files selected for processing (2)
  • middleware/idempotency/idempotency.go
  • middleware/idempotency/idempotency_test.go
💤 Files with no reviewable changes (1)
  • middleware/idempotency/idempotency.go

Walkthrough

The idempotency middleware no longer gates caching decisions on the application BodyLimit configuration. A prior guard that skipped storing responses when body size exceeded the limit is removed, and the corresponding test is updated to verify that caching now succeeds for oversized bodies.

Changes

Idempotency Caching for Large Bodies

Layer / File(s) Summary
Remove BodyLimit check and update caching test
middleware/idempotency/idempotency.go, middleware/idempotency/idempotency_test.go
The middleware removes the BodyLimit-based guard (lines 164–167) that previously prevented caching responses exceeding the configured limit. The test function Test_New_SkipCache_WhenBodyTooLarge is renamed to Test_New_Cache_WhenBodyTooLarge and assertions updated to expect a single handler execution, single cache write, and successful cache insertion even when response body is oversized.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • gofiber/fiber#4018: Previously added the BodyLimit-based skip logic that this PR now removes from the same idempotency caching path.
  • gofiber/fiber#3829: Related changes to how response bodies are marshaled and stored in the idempotency middleware.
  • gofiber/fiber#3521: Earlier updates to idempotency cache-behavior test cases and assertions.

Suggested labels

☢️ Bug, v3

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • efectn

Poem

🐰 Large bodies once feared the cache's keen eye,
But now they're embraced, no size too high!
The limit guard falls, responses take flight—
All bodies cached, everything's tight! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: preserving idempotency replay protection for oversized responses by removing a BodyLimit-based early return.
Description check ✅ Passed The description covers motivation, implementation details, and testing changes. However, it lacks completion of template sections like type of change, checklist items, and changelog entries.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-idempotency-for-large-responses

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an idempotency middleware regression where successful responses exceeding the app's BodyLimit were not cached, allowing duplicate requests with the same idempotency key to re-execute handler side effects. With the early return removed, all successful responses (regardless of size) are marshaled and stored so replays are served from the cache.

Changes:

  • Remove the BodyLimit size check that skipped caching of oversized successful responses in middleware/idempotency/idempotency.go.
  • Rename and invert the regression test (Test_New_SkipCache_WhenBodyTooLargeTest_New_Cache_WhenBodyTooLarge) to assert oversized responses are cached and replayed.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
middleware/idempotency/idempotency.go Removes the BodyLimit early return so oversized successful responses are still persisted for replay.
middleware/idempotency/idempotency_test.go Updates the regression test to assert that the oversized response is cached once and replayed on retry.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.26%. Comparing base (c48bbc2) to head (a4fe81a).
⚠️ Report is 52 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4287      +/-   ##
==========================================
+ Coverage   91.21%   91.26%   +0.05%     
==========================================
  Files         130      130              
  Lines       12760    12793      +33     
==========================================
+ Hits        11639    11676      +37     
+ Misses        709      705       -4     
  Partials      412      412              
Flag Coverage Δ
unittests 91.26% <ø> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request removes the logic in the idempotency middleware that previously skipped caching for response bodies exceeding the application's configured body limit. The associated unit tests have been updated to verify that oversized responses are now correctly cached. I have no feedback to provide.

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants