diff --git a/examples/options.js b/examples/options.js index ea31100a..a398e15d 100644 --- a/examples/options.js +++ b/examples/options.js @@ -63,6 +63,41 @@ const openapiOption = { } } +const openapiRelativeOptions = { + openapi: { + info: { + title: 'Test swagger', + description: 'testing the fastify swagger api', + version: '0.1.0' + }, + servers: [ + { + url: '/test' + } + ], + tags: [ + { name: 'tag' } + ], + components: { + securitySchemes: { + apiKey: { + type: 'apiKey', + name: 'apiKey', + in: 'header' + } + } + }, + security: [{ + apiKey: [] + }], + externalDocs: { + description: 'Find more info here', + url: 'https://swagger.io' + } + }, + stripBasePath: false +} + const schemaQuerystring = { schema: { response: { @@ -250,6 +285,7 @@ const schemaOperationId = { module.exports = { openapiOption, + openapiRelativeOptions, swaggerOption, schemaQuerystring, schemaBody, diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js index 8ba7d30a..f7734d3c 100644 --- a/lib/spec/openapi/utils.js +++ b/lib/spec/openapi/utils.js @@ -67,7 +67,7 @@ function prepareOpenapiObject (opts) { } function normalizeUrl (url, servers, stripBasePath) { - if (!stripBasePath) return url + if (!stripBasePath) return formatParamUrl(url) servers = Array.isArray(servers) ? servers : [] servers.forEach(function (server) { const basePath = new URL(server.url).pathname diff --git a/test/spec/openapi/route.js b/test/spec/openapi/route.js index 54a10925..bd2b022b 100644 --- a/test/spec/openapi/route.js +++ b/test/spec/openapi/route.js @@ -7,6 +7,7 @@ const yaml = require('js-yaml') const fastifySwagger = require('../../../index') const { openapiOption, + openapiRelativeOptions, schemaBody, schemaConsumes, schemaCookies, @@ -912,3 +913,46 @@ test('security cookies ignored when declared in security and securityScheme', t }) }) }) + +test('path params on relative url', t => { + t.plan(3) + const fastify = Fastify() + + fastify.register(fastifySwagger, openapiRelativeOptions) + + const schemaParams = { + schema: { + params: { + type: 'object', + properties: { + id: { type: 'string' } + } + } + } + } + fastify.get('/parameters/:id', schemaParams, () => {}) + + fastify.ready(err => { + t.error(err) + + const openapiObject = fastify.swagger() + Swagger.validate(openapiObject) + .then(function (api) { + const paramPath = api.paths['/parameters/{id}'].get + t.ok(paramPath) + t.same(paramPath.parameters, [ + { + required: true, + in: 'path', + name: 'id', + schema: { + type: 'string' + } + } + ]) + }) + .catch(function (err) { + t.fail(err) + }) + }) +})