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

Relative URL gives error

Open JamesRamm opened this issue 2 years ago • 4 comments

Prerequisites

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

Fastify version

3.22.0

Plugin version

4.x.x

Node.js version

14.x

Operating system

Linux

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

20.10

Description

According to the OpenAPI docs it should be possible to use a relative URL in the servers array. When I do this, I get the following error on startup:

node:internal/url:552
  throw new ERR_INVALID_URL(input);
  ^

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:371:5)
    at onParseError (node:internal/url:552:9)
    at new URL (node:internal/url:628:5)
    at /opt/deploy/node_modules/fastify-swagger/lib/spec/openapi/utils.js:73:22
    at Array.forEach (<anonymous>)
    at normalizeUrl (/opt/deploy/node_modules/fastify-swagger/lib/spec/openapi/utils.js:72:11)
    at Object.swagger (/opt/deploy/node_modules/fastify-swagger/lib/spec/openapi/index.js:40:19)
    at /opt/deploy/index.js:42:11
    at Server.wrap (/opt/deploy/node_modules/fastify/lib/server.js:86:9)
    at Object.onceWrapper (node:events:509:28) {
  input: 'api/results',
  code: 'ERR_INVALID_URL'
}

My config looks like this

{
   routePrefix: '/docs',
   openapi: {
      info: {
         title: 'Results',
         description: 'C2ST Results API',
         version: package.version,
      },
      servers: [{
         url: '/api/results',
         description: 'API Server'
      }],
      consumes: ['application/json'],
      produces: ['application/json'],
      definitions: {},
      components: {
         securitySchemes: {
            openId: {
               type: 'openIdConnect',
               openIdConnectUrl: '/auth/realms/master/.well-known/openid-configuration'
            },
         },
      },
      security: [{
         openId: ['openid', 'profile'],
      }]
   },
   uiConfig: {
      docExpansion: 'list',
      deepLinking: false,
   },
   exposeRoute: true,
}

I am running locally - if I change the URL to http://localhost/api/results all works fine, but I need to be able to support on-premise installations (different hostnames).

Steps to Reproduce

Use a relative url in the openapi.servers[].url field

Expected Behavior

A relative url should be resolved against the host serving the swagger docs.

JamesRamm avatar Oct 26 '21 14:10 JamesRamm

After a little digging:

  1. This is an issue with this library (rather than any upstream swagger libs)
  2. specifiying 'stripBasePath' to false, may be a workaround - needs validating

JamesRamm avatar Oct 26 '21 14:10 JamesRamm

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

mcollina avatar Oct 26 '21 14:10 mcollina

@mcollina I'm having a dig around - if specifying stripBasePath is a workaround, is it better to document this as required if using a relative URL, or to fix the underlying issue (as far as I can see it is a case of determining whether the url is relative before calling URL)

JamesRamm avatar Oct 26 '21 14:10 JamesRamm

@mcollina @JamesRamm the problem with stripBasePath or checking url.startsWith('/') is that you are missing the formatParamUrl and return the URL as is. I think this should work: if (!stripBasePath || url.startsWith('/')) return formatParamUrl(url)

meir-ma avatar Nov 21 '21 13:11 meir-ma

Thank you for this, I was having an issue with Invalid URL and didn't know you could use relative urls.

nickolasdeluca avatar Jul 17 '23 02:07 nickolasdeluca