fastify-swagger icon indicating copy to clipboard operation
fastify-swagger copied to clipboard

`exposeHeadRoute` should include HEAD routes

Open buschco opened this issue 3 years ago • 5 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

setting exposeHeadRoute to true in a fastify route, I expect this route to be included with the original Mehtod and the HEAD method.

Motivation

While adding exposeHeadRoutes in supapase-storage-api we discovered that the new HEAD routes where not exported.

Example

fastify.get<getObjectRequestInterface>(
    '/object/:bucketName/*',
    {
      exposeHeadRoute: true,

->

"/object/{bucketName}/{wildcard}": {
            "get": { ... },
            "head": {  /* same as get but no body */  }

buschco avatar Aug 06 '22 08:08 buschco

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Aug 06 '22 11:08 mcollina

@mcollina I would love to! Can you pre someone else give me a hint where to start? I was looking into the sources and could not find a good point 🧐

buschco avatar Aug 07 '22 06:08 buschco

I'm not sure either, I would start writing a reproducible example /unit test and then work from there. Check out the onRoute hook, it should be triggered for both GET and HEAD routes.

mcollina avatar Aug 07 '22 07:08 mcollina

https://github.com/fastify/fastify-swagger/blob/99a90d425d9a7f903fd7c94eead19d27df060eed/lib/util/common.js#L18 < we are skipping HEAD here.

mcollina avatar Aug 07 '22 07:08 mcollina

Here is the PR -> https://github.com/fastify/fastify-swagger/pull/649

I think I understand now why they where skipped. I tried to improve this, by only skipping routes that would cause a validation problem by using the same operationId multiple times.

see https://swagger.io/docs/specification/paths-and-operations somewhere under operationId

operationId is an optional unique string used to identify an operation. If provided, these IDs must be unique among all operations described in your API.


Imo fastify should not use the same operationId for the automatic generated routes (e.g. ${operationId}-head could be a fix). But also the HEAD route seems to be the same "operation" of the GET but the returned payload is just omited 🤷

buschco avatar Aug 07 '22 13:08 buschco