Skip to content

fix: warn when addHttpMethod overrides an existing HTTP method - #6608

Closed
Vansh0204 wants to merge 3 commits into
fastify:mainfrom
Vansh0204:fix/add-http-method-override-warning
Closed

Vansh0204 wants to merge 3 commits into
fastify:mainfrom
Vansh0204:fix/add-http-method-override-warning

Conversation

@Vansh0204

Copy link
Copy Markdown

Summary

Closes #6284

addHttpMethod could silently reconfigure built-in HTTP methods (e.g. GET, POST) without any indication to the developer. The function name implies adding new methods, not overriding existing ones.

Changes

This PR introduces a new overrideExisting option (default: false) to addHttpMethod.

  • When a method that already exists in kSupportedHTTPMethods is passed without overrideExisting: true, a deprecation warning (FSTWRN005) is emitted.
  • The actual reconfiguration still proceeds — fully non-breaking in v5.
  • In Fastify v6, calling addHttpMethod on an existing method without { overrideExisting: true } will throw an error.

Usage

// v5 — emits FSTWRN005 deprecation warning, still works
fastify.addHttpMethod('GET', { hasBody: false })

// v5 — no warning, override is explicit
fastify.addHttpMethod('GET', { overrideExisting: true })

// Unchanged — adding a brand new method works as before
fastify.addHttpMethod('SEARCH', { hasBody: true })

Files Changed

File Change
lib/warnings.js Added FSTWRN005 warning
fastify.js Added overrideExisting option to addHttpMethod
types/instance.d.ts Updated TypeScript signature
test/http-methods/custom-http-methods.test.js 3 new tests
test/types/instance.test-d.ts 2 new type tests

Closes fastify#6284

addHttpMethod could silently reconfigure built-in HTTP methods (e.g.
GET, POST) without any indication to the developer. The name 'add'
implies adding new methods, not overriding existing ones.

This commit introduces a new  option (default: false).
When a method that already exists in  is passed
without , a deprecation warning (FSTWRN005) is
emitted. The actual reconfiguration still proceeds so this is fully
non-breaking in v5.

In Fastify v6, calling addHttpMethod on an existing method without
{ overrideExisting: true } will throw an error.

Changes:
- lib/warnings.js: add FSTWRN005 warning
- fastify.js: add overrideExisting option and emit FSTWRN005 when needed
- types/instance.d.ts: update addHttpMethod signature
- test/http-methods/custom-http-methods.test.js: add 3 new tests
- test/types/instance.test-d.ts: add type tests for the new option
@github-actions github-actions Bot added the typescript TypeScript related label Mar 24, 2026
@Eomm

Eomm commented Jul 5, 2026

Copy link
Copy Markdown
Member

We are late to the party.

I think the PR is good, but could you resolve the conflicts?

@Eomm Eomm added the semver-minor Issue or PR that should land as semver minor label Jul 5, 2026

@jean-michelet jean-michelet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would still prefer add a new setHttpMethod and remove addHttpMethod in future major.

But if other contributors agree with this, it's already better then the current misleading implementation.

Comment thread lib/warnings.js
const FSTWRN005 = createWarning({
name: 'FastifyWarning',
code: 'FSTWRN005',
message: 'addHttpMethod was called for method "%s" which already exists. Pass { overrideExisting: true } to suppress this warning. In Fastify v6, calling addHttpMethod on an existing method without this option will throw an error.',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we agree about this behavior in next major @fastify/core?

@Vansh0204

Copy link
Copy Markdown
Author

Hi @jean-michelet @Eomm,

Thanks for the feedback!

I've resolved the merge conflicts with main and updated the warning codes to align with the latest changes. I also went ahead and removed the trivial comment in test/http-methods/custom-http-methods.test.js as requested.

Let me know if there's anything else needed to get this merged!

@metcoder95 metcoder95 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@jean-michelet jean-michelet left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, but let's remove useless coments plz.

}
process.on('warning', onWarning)

// POST is a built-in bodywith method — with the flag, no warning should fire

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// POST is a built-in bodywith method — with the flag, no warning should fire

t.plan(2)
const fastify = Fastify()

// GET is bodyless by default; override it to accept a body

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// GET is bodyless by default; override it to accept a body

t.plan(2)
const fastify = Fastify()

// Attach listener BEFORE calling addHttpMethod — process.emitWarning is async (next tick)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// Attach listener BEFORE calling addHttpMethod — process.emitWarning is async (next tick)

@jean-michelet

Copy link
Copy Markdown
Member

@Vansh0204 can you plz fix the conflicts and remove comments plz?
Or I will push a PR for this feature.

@jean-michelet

Copy link
Copy Markdown
Member

Sorry, I let you 4 days to fix but we now must ship that.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

semver-minor Issue or PR that should land as semver minor typescript TypeScript related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

addHttpMethod override existing http standard methods

4 participants