fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
910 stars 200 forks source link

Disable HEAD routes #722

Closed Xhale1 closed 1 year ago

Xhale1 commented 1 year ago

Prerequisites

🚀 Feature Proposal

Add an option to automatically hide HEAD routes.

Motivation

With release 8.4.0 HEAD routes are now supported (well done everyone!). For some use cases HEAD routes might not be needed in the swagger ui and their introduction could provide more clutter than utility.

Example

No response

ymhmd commented 1 year ago

I'd upvote for this issue 👍

bacloud23 commented 1 year ago

I also upvote for this. I believe this is somwhere in the Swagger libraries itself https://stackoverflow.com/a/45282119/15313684

-- probably

bacloud23 commented 1 year ago

hello @Xhale1 I don't know how you do, but a workaround would be to have an explicit head route, with an empty handler on top of the route in question, without tags and the otherone with tags:

This for instance:

    // just to disable the route in autogenerated swagger UI
    fastify.head('/meta', {
        schema: {},
        preHandler: rejectForeigners,
        handler: async (request, reply) => {
            return
        },
    })

    fastify.get('/meta', {
        schema: {
            tags: ['meta'],
        },
        preHandler: rejectForeigners,
        handler: async (request, reply) => {
            return await api.meta()
        },
    }) 
Matheusbafutto commented 1 year ago

Hey folks!

Looking at the PR that introduced support for HEAD routes on fastify swagger, I see there is an option to hide them available in the Fastify instance constructor

https://github.com/fastify/fastify-swagger/blob/master/test/spec/swagger/schema.js#L486

Hope this helps!

ymhmd commented 1 year ago

Thanks @Matheusbafutto for sharing this. It works 🎉

That's how I initialized Fastify instance to remove HEAD routes

const app = fastify({ exposeHeadRoutes: false });
dolsup commented 1 year ago

It's cool that HEAD routes are now visible in swagger. But we need to allow opt-out for those who don't want it.

mcollina commented 1 year ago

@dolsup I noticed and I've been fighting this. #724 should do the trick, take a look.