Skip to content

Create fresh response object in default Fastify handler#612

Merged
dahlia merged 1 commit intofedify-dev:2.0-maintenancefrom
moreal:fix-fastify-default-handler
Mar 11, 2026
Merged

Create fresh response object in default Fastify handler#612
dahlia merged 1 commit intofedify-dev:2.0-maintenancefrom
moreal:fix-fastify-default-handler

Conversation

@moreal
Copy link
Contributor

@moreal moreal commented Mar 10, 2026

Summary

This pull request just let @fedify/fastify create a fresh Response object by default not acceptable handler.

When running the examples/fastify project and executing the following curl command twice:

fastify % curl http://localhost:3000/users/someone
Not Acceptable
fastify % curl http://localhost:3000/users/someone
{"statusCode":500,"code":"FST_ERR_REP_RESPONSE_BODY_CONSUMED","error":"Internal Server Error","message":"Response.body is already consumed."}

It returns 500 error because the response stream is already consumed. You can re-check it by running the regression test (in index.test.ts file)

This pull request just make it return a new Response object for each request.

Related issue

There is no related issue.

Changes

List the specific modifications made in this PR.
Focus on what was changed without going into detail about impact.

  • Let default onNotAcceptable handler to create a new Response object.
  • Added a regression test about the above situation.

Benefits

It will make server applications using @fedify/fastify without their onNotAcceptable handler return a valid 406 response (Not Acceptable).

Checklist

  • Did you add a changelog entry to the CHANGES.md?
  • Did you write some relevant docs about this change (if it's a new feature)?
  • Did you write a regression test to reproduce the bug (if it's a bug fix)?
  • Did you write some tests for this change (if it's a new feature)?
  • Did you run mise test on your machine?

@issues-auto-labeler issues-auto-labeler bot added breaking change Breaking change component/federation Federation object related component/integration Web framework integration labels Mar 10, 2026
@moreal moreal force-pushed the fix-fastify-default-handler branch 2 times, most recently from 2a110f9 to c674aef Compare March 10, 2026 14:05
@codecov
Copy link

codecov bot commented Mar 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
packages/fastify/src/index.ts 82.25% <100.00%> (+0.29%) ⬆️
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@moreal moreal marked this pull request as ready for review March 10, 2026 14:19
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the @fedify/fastify package where the default onNotAcceptable handler would return a 500 Internal Server Error on subsequent requests after the initial 406 "Not Acceptable" response. This occurred because the Response object's body was being consumed and the same object was reused. The change ensures that a new Response object is generated for every "Not Acceptable" scenario, guaranteeing consistent and correct 406 responses for applications using this plugin.

Highlights

  • Fix for onNotAcceptable handler: The default onNotAcceptable handler in @fedify/fastify now creates a fresh Response object for each request, resolving an issue where reusing a shared instance led to Response.body is already consumed errors.
  • Regression Test Added: A new test case was introduced to specifically reproduce and verify the fix, ensuring the correct behavior of the 406 "Not Acceptable" response on repeated requests.
Changelog
  • CHANGES.md
    • Added an entry documenting the fix for the onNotAcceptable handler in @fedify/fastify.
  • packages/fastify/src/index.test.ts
    • Added a test case to verify that the onNotAcceptable handler correctly creates a fresh 406 response for each request.
  • packages/fastify/src/index.ts
    • Modified the onNotAcceptable handler to call a function that dynamically creates a new Response object instead of referencing a static, pre-instantiated one.
Activity
  • The author moreal initiated this pull request to resolve a bug in the @fedify/fastify package.
  • The pull request includes a detailed summary, a description of changes, and the benefits of the fix.
  • A changelog entry has been added, and a regression test was written to validate the fix.
  • The author has confirmed running mise test.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 fixes a bug in the @fedify/fastify package where a shared Response object for 406 Not Acceptable errors was causing a 'body already consumed' error on subsequent requests. The fix refactors the default onNotAcceptable handler to create a new Response object for each request. A regression test is included to cover this scenario, and the changelog has been updated to reflect the change. The implementation correctly resolves the issue.

@dahlia dahlia requested a review from Copilot March 11, 2026 02:16
@dahlia
Copy link
Member

dahlia commented Mar 11, 2026

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Bravo.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link

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 a Fastify integration bug where the default onNotAcceptable handler reused a single Response instance, causing subsequent requests to fail due to a consumed response body stream.

Changes:

  • Replaced the shared default 406 Response with a factory function to create a fresh Response per request.
  • Added a regression test that reproduces the “Response.body is already consumed” failure across multiple requests.
  • Added a changelog entry under the upcoming release.

Reviewed changes

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

File Description
packages/fastify/src/index.ts Switches default onNotAcceptable from a singleton Response to a per-request factory.
packages/fastify/src/index.test.ts Adds regression coverage ensuring repeated Not Acceptable requests return 406 consistently.
CHANGES.md Documents the fix for @fedify/fastify in v2.0.4 notes.

@moreal moreal force-pushed the fix-fastify-default-handler branch from c674aef to 3c4a29e Compare March 11, 2026 03:51
@moreal moreal requested a review from dahlia March 11, 2026 03:54
Copy link
Member

@dahlia dahlia left a comment

Choose a reason for hiding this comment

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

Thanks!

@dahlia dahlia merged commit 4b1c5b0 into fedify-dev:2.0-maintenance Mar 11, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Breaking change component/federation Federation object related component/integration Web framework integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants