fastify-swagger
fastify-swagger copied to clipboard
fastify-static plugin as an option
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the issue has not already been raised
Issue
Currently we are embedding fastify-static to serve the swagger-ui. But what about the case when people want to use only the /json feature? I'm asking this because i sometimes have dependabots notifications like so:
This can be solved by removing fastify-static from dependencies and add it as peerDependencies but i'm not a fan of it. What about adding it as an option?
I don't understand, is there a vulnerability somewhere I didn't receive a report on?
It's an old one, but was stalling in one of my repositories. But in this project i don't use any feature involving the need of fastify-static that's why I'm bringing the point.
Solving this is quite complex and require a lot of rework.
Just adding an option to pass in fastify-static does not work from my point of view. peerDependencies are not standardized across package managers and better avoid them. More importantly, they'll likely still be installed, causing your problem of having a dependency you do not need.
The root cause of your problem is that fastify-swagger does too much. It has 4 responsibilities:
- generate the swagger definition from the routes
- generate the openapi definition from the routes
- expose the swagger endpoints
- serve the swagger UI
I think the only solution is to move this to a monorepo/workspaces setup and split it in multiple modules. In the meanwhile we can also move it to the @fastify scope.
This is quite some work.
This is a good analysis of the current situation. A good roadmap would be to migrate to @fastify scope and refactor it afterward?
It really depends if somebody has time to do the refactoring. It's not a big issue for most people.
If it will be a rewritten version. I recommend to provide a plugin system (plugin registration order does matter). Allow the user to customize all the behavior as their needs.
We only provide the basic one, like openapi and swagger.
And they can create an plugin like typebox, joi for extra support.
The below one is what I did to allow all behavior customization and support multiple document based on constraints / what you needs.
https://github.com/climba03003/fastify-openapi
As an alternative, I use Elements to provide the document. It is more light weight and do not need fastify-static.
https://github.com/climba03003/fastify-openapi/blob/6429e36b688ab7a0083116847ba2d3b3df4d8e12/lib/utils/routes.ts#L31-L57
POC for my last comment in here https://github.com/climba03003/fastify-openapi/tree/next
import { TypeboxPlugin, OpenAPIPlugin, FastifyOpenAPI } from '@kakang/fastify-openapi'
import Fastify from 'fastify'
const fastify = Fastify()
fastify.register(FastifyOpenAPI, {
// default: [OpenAPIPlugin]
plugins: [TypeboxPlugin, OpenAPIPlugin]
})
await fastify.ready()
// documents calculated from first get
fastify.documents
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Want to throw a +1 in for this, in my projects fastify-swagger contributes 5-10% to the size of node_modules primarily because of fastify-static and the UI (which isn't used as I use Redoc to provide the frontend for the OpenAPI documentation).
I agree, however we need somebody to champion this.
How about we fork this repo in the fastify org and name it @fastify/openapi, and remove all the swagger-ui code. Maybe rename this package to @fastify/swagger-ui to make clear it adds only swagger-ui and requires @fastify/openapi-spec.
I personally prefer rapidodc of @mrin9 and I also would not need swagger-ui.
I think we should:
- create a new package
@fastify/swagger-uithat just adds the Swagger UI. - remove everything
@fastify/swagger-uifrom this repo, pointing to the new repo for the docs - release a new major
This looks a good starting step, wdyt?
If you give green light, I would take care of it.
go for it
The fastify.swagger() function is useful in my case (using exposeRoute = true). I don't want fastify-swagger to register any routes for me because I just need the fastify.swagger() function...
The
fastify.swagger()function is useful in my case (usingexposeRoute = true). I don't wantfastify-swaggerto register any routes for me because I just need thefastify.swagger()function...
Please open a separate issue or submit a PR for your use-case.
I already created a swagger-ui implementation in https://github.com/fastify/fastify-swagger-ui
I was waiting for feedback of you guys. It currently fails because cspNonce is already decorated to fastify.
https://github.com/fastify/fastify-swagger-ui/pull/1
Solved with release of v 8.0.0