Skip to content

Commit

Permalink
OpenApi: Fix params mapping on relative URL
Browse files Browse the repository at this point in the history
  • Loading branch information
fatal10110 committed Dec 27, 2021
1 parent 60d9d72 commit ec2ba26
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
36 changes: 36 additions & 0 deletions examples/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,41 @@ const openapiOption = {
}
}

const openapiRelaiveOptions = {
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: {
Expand Down Expand Up @@ -250,6 +285,7 @@ const schemaOperationId = {

module.exports = {
openapiOption,
openapiRelaiveOptions,
swaggerOption,
schemaQuerystring,
schemaBody,
Expand Down
2 changes: 1 addition & 1 deletion lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions test/spec/openapi/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const yaml = require('js-yaml')
const fastifySwagger = require('../../../index')
const {
openapiOption,
openapiRelaiveOptions,
schemaBody,
schemaConsumes,
schemaCookies,
Expand Down Expand Up @@ -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, openapiRelaiveOptions)

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)
})
})
})

0 comments on commit ec2ba26

Please sign in to comment.