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

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

Open Danko14 opened this issue 1 year ago • 2 comments

Prerequisites

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

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

Danko14 avatar Sep 24 '24 06:09 Danko14

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.

mcollina avatar Sep 26 '24 17:09 mcollina

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

Danko14 avatar Oct 04 '24 10:10 Danko14

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 avatar Oct 11 '24 18:10 DavidTanner

Relevant PR #164 for Issue #162

DavidTanner avatar Oct 11 '24 18:10 DavidTanner

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

mcollina avatar Oct 12 '24 09:10 mcollina

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

DavidTanner avatar Oct 12 '24 14:10 DavidTanner

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!

Danko14 avatar Oct 14 '24 10:10 Danko14