使用 Fastify 在 Swagger 中添加动态查询参数
Add dynamic query parameter in Swagger with Fastify
是否可以在 Swagger 上使用 Fastify 记录动态查询参数,允许客户端使用 Swagger v1.0.0 在 Swagger UI 上的文本字段内传递参数值?在我的例子中是输入动态conversationId
参数的值。
这是我在配置文件夹中的 swagger.js 文件。
exports.options = {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Wrapper API',
description: 'Building a wrapper api',
version: '1.0.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost:3000',
schemes: [
'http',
'https'
],
consumes: ['application/json'],
produces: ['application/json'],
}
}
+ 这是我的路线
const healthBotController = require('../controllers/healthBotWrapperController')
const routes = [
{
method: 'GET',
url: '/',
handler: healthBotController.getEndpoints
},
]
module.exports = routes;
我尝试搜索和阅读文档,但仍找不到解决问题的方法。
先谢谢你了。
要在swagger中添加查询参数,需要在路由配置中定义一个JSON Schema:
const fastify = require('fastify')()
fastify.register(require('fastify-swagger'), {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Wrapper API',
description: 'Building a wrapper api',
version: '1.0.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost:3000',
schemes: [
'http',
'https'
],
consumes: ['application/json'],
produces: ['application/json'],
securityDefinitions: {
ApiToken: {
description: 'Authorization header token, sample: "Bearer #TOKEN#"',
type: 'apiKey',
name: 'Authorization',
in: 'header'
},
Oauth2Token: {
description: 'OAUTH2',
type: 'oauth2',
flow: 'accessCode',
authorizationUrl: 'https://example.com/oauth/authorize',
tokenUrl: 'https://example.com/oauth/token',
scopes: {
read: 'Grants read access',
foo: 'Grants foao scope'
}
}
}
}
})
fastify.route({
method: 'GET',
url: '/',
schema: {
security: [{ ApiToken: [], Oauth2Token: [] }],
querystring: {
foo: { type: 'number' },
bar: { type: 'string' }
}
},
handler: function foo () {}
})
fastify.listen(8080)
然后在http://localhost:8080/documentation你会看到:
是否可以在 Swagger 上使用 Fastify 记录动态查询参数,允许客户端使用 Swagger v1.0.0 在 Swagger UI 上的文本字段内传递参数值?在我的例子中是输入动态conversationId
参数的值。
这是我在配置文件夹中的 swagger.js 文件。
exports.options = {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Wrapper API',
description: 'Building a wrapper api',
version: '1.0.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost:3000',
schemes: [
'http',
'https'
],
consumes: ['application/json'],
produces: ['application/json'],
}
}
+ 这是我的路线
const healthBotController = require('../controllers/healthBotWrapperController')
const routes = [
{
method: 'GET',
url: '/',
handler: healthBotController.getEndpoints
},
]
module.exports = routes;
我尝试搜索和阅读文档,但仍找不到解决问题的方法。
先谢谢你了。
要在swagger中添加查询参数,需要在路由配置中定义一个JSON Schema:
const fastify = require('fastify')()
fastify.register(require('fastify-swagger'), {
routePrefix: '/documentation',
exposeRoute: true,
swagger: {
info: {
title: 'Wrapper API',
description: 'Building a wrapper api',
version: '1.0.0'
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
},
host: 'localhost:3000',
schemes: [
'http',
'https'
],
consumes: ['application/json'],
produces: ['application/json'],
securityDefinitions: {
ApiToken: {
description: 'Authorization header token, sample: "Bearer #TOKEN#"',
type: 'apiKey',
name: 'Authorization',
in: 'header'
},
Oauth2Token: {
description: 'OAUTH2',
type: 'oauth2',
flow: 'accessCode',
authorizationUrl: 'https://example.com/oauth/authorize',
tokenUrl: 'https://example.com/oauth/token',
scopes: {
read: 'Grants read access',
foo: 'Grants foao scope'
}
}
}
}
})
fastify.route({
method: 'GET',
url: '/',
schema: {
security: [{ ApiToken: [], Oauth2Token: [] }],
querystring: {
foo: { type: 'number' },
bar: { type: 'string' }
}
},
handler: function foo () {}
})
fastify.listen(8080)
然后在http://localhost:8080/documentation你会看到: