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

OpenAPI/OAS 3 Server Variable Object url ':' before 'port' variable is invalid

Open lfurzewaddock opened this issue 3 years ago • 2 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.27.0

Plugin version

4.13.1

Node.js version

10.15

Operating system

Linux

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

20.04

Description

According to OAS 3 spec https://swagger.io/specification/#server-object we should be able to use server variables including port, however a 500 error occurs with the following stack trace;

TypeError [ERR_INVALID_URL]: Invalid URL: https://authz.localdomain:{port}/{basePath}
    at onParseError (internal/url.js:241:17)
    at new URL (internal/url.js:319:5)
    at /app/node_modules/fastify-swagger/lib/spec/openapi/utils.js:73:22
    at Array.forEach (<anonymous>)
    at normalizeUrl (/app/node_modules/fastify-swagger/lib/spec/openapi/utils.js:72:11)
    at Object.swagger (/app/node_modules/fastify-swagger/lib/spec/openapi/index.js:40:19)
    at Object.handler (/app/node_modules/fastify-swagger/lib/routes.js:101:26)
    at preHandlerCallback (/app/node_modules/fastify/lib/handleRequest.js:126:28)
    at preValidationCallback (/app/node_modules/fastify/lib/handleRequest.js:109:5)
    at handler (/app/node_modules/fastify/lib/handleRequest.js:72:7) +0ms

Steps to Reproduce

Use the following server object

servers: [
      {
        url: 'https://authz.localdomain:{port}/{basePath}',
        description: 'Development server',
        variables: {
          port: {
            enum: ['443', '4432'],
            default: '4432',
          },
          basePath: {
            default: 'api',
          },
        },
      },
    ],

Expected Behavior

No error should occur and the following url should be computed;

https://authz.localdomain:4432/api

lfurzewaddock avatar Feb 02 '22 12:02 lfurzewaddock

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

mcollina avatar Feb 03 '22 09:02 mcollina

In case someone stumbles upon this, a current workaround is to set stripBasePath: false (at the same level as openapi), as detailed in this other issue: https://github.com/fastify/fastify-swagger/issues/494

As for the code that throws the error (https://github.com/fastify/fastify-swagger/blob/master/lib/spec/openapi/utils.js#L75), I'm not sure in which case it could be useful. Indeed, as I understand it, it's meant to check if the url is relative. However new URL(server.url) will fail if the url is relative, as it would require the base url as second parameter.

So, as suggested in the other ticket, maybe the docs could include a note about this workaround, or normalizeUrl's code could be adapted (haven't figured out what the proper solution would be yet).

Chonne avatar Sep 01 '22 11:09 Chonne