fastify / fastify-swagger-ui

Serve Swagger-UI for Fastify
MIT License
143 stars 41 forks source link

Can't specify host to work correctly with nginx generated subroutes #176

Closed Danko14 closed 1 month ago

Danko14 commented 1 month ago

Prerequisites

Last working version

7.4.0

Stopped working in version

9.0.0

Node.js version

22.9.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

14.0

💥 Regression Report

I'm using fastify with docker and nginx API gateway, so I always need to specify service api url in my swagger configuration, so that it is routed correctly. after update, I couldn't find "host" parameter I used before, and while it all works fine locally, it crashes on server, being unable to fetch lib files, such as swagger-ui-bundle.js. e.g. request goes to https://mydomain/docs/swagger-ui-bundle.js while the correct path is https://mydomain/v1/mock-server/docs/swagger-ui-bundle.js and getting either html or 404 response

routePrefix and servers parameters couldn't help me here

maybe I'm missing an obvious solution, but I couldn't find any in documentation or existing tickets.

sources mismatch: photo_2024-09-24 13 49 58

thanks in advance!

Steps to Reproduce

clone repo https://github.com/Danko14/fastify-swagger

follow readme steps (create .env & run docker-compose) you'll get nginx running on 80 and mock server running on 8080 and available on http://localhost/mock-server/ query http://localhost/mock-server/ping to get { msg: 'ok' } query http://localhost:8080/docs to see swagger docs query http://localhost/mock-server/docs to see where the problem is

swagger config: fastify.register( fastifySwagger, { openapi: { info: { title: 'Mock server', description: 'API documentation', version, }, servers: [ { url:https://${process.env.SERVER_API_URL}, description: 'HTTPS' }, { url:http://${process.env.HTTP_HOST}:${process.env.HTTP_PORT}, description: 'HTTP' }, ], components: { securitySchemes: { actorId: { type: 'apiKey', in: 'header', name: 'x-actor-id', }, actorType: { type: 'apiKey', in: 'header', name: 'x-actor-type', }, }, }, security: [ { ApiKeyAuth: [] }, { ActorTypeAuth: [] }, ], }, }, ) fastify.register(swaggerUi, { routePrefix: '/docs', uiConfig: { docExpansion: 'list', deepLinking: false, }, staticCSP: false, transformSpecification: (swaggerObject, request, reply) => swaggerObject, transformSpecificationClone: true, })

nginx config:

image

Expected Behavior

No response

mcollina commented 1 month ago

Transfering to fastify-swagger-ui, because the problem seems to be there.

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

Danko14 commented 1 month ago

@mcollina created repo and added steps to reproduce to the issue https://github.com/Danko14/fastify-swagger

DavidTanner commented 1 month ago

I had the same issue, and found a fix. I assumed the routePrefix needed to start with a /, but if I change the route to docs or swagger then the route resolves correctly.

I tested it with your example repo @Danko14 and was able to get it to work correctly. I had to also change the port in the Dockerfile, but that shouldn't affect the validity of the change.

DavidTanner commented 1 month ago

Relevant PR #164 for Issue #162

mcollina commented 1 month ago

@DavidTanner Can you send a PR to update the docs for that?

DavidTanner commented 1 month ago

I would, but I don't think that is the right way. I'm trying out some other changes

Danko14 commented 1 month ago

I assumed the routePrefix needed to start with a /, but if I change the route to docs or swagger then the route resolves correctly.

Genius. Just like I imagined it Thank you!