Skip to content

fix: accept method arrays on websocket routes - #384

Open
lprnmns wants to merge 1 commit into
fastify:mainfrom
lprnmns:fix/websocket-route-method-array
Open

lprnmns wants to merge 1 commit into
fastify:mainfrom
lprnmns:fix/websocket-route-method-array

Conversation

@lprnmns

@lprnmns lprnmns commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Fastify accepts method as a string or as an array, but the onRoute hook compares it to 'GET' directly. A websocket route declared with an array is rejected, even when it only lists GET:

fastify.route({
  method: ['GET'], // or ['GET', 'HEAD']
  url: '/echo',
  websocket: true,
  handler: (socket) => {}
})
// Error: websocket handler can only be declared in GET method

The hook now normalizes method to an array:

  • only HEAD keeps returning early, as it does for the string form;
  • any method other than GET or HEAD still throws the same error;
  • ['GET'] and ['GET', 'HEAD'] are accepted and upgrade like method: 'GET'.

Tests cover both accepted arrays end to end with injectWS, and ['GET', 'POST'] still throwing. npm test passes, including 100% coverage and the type tests, and so does eslint.

Checklist

This branch has not been deployed

No deployments
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.

1 participant