首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用Fastify在Swagger中添加动态查询参数

用Fastify在Swagger中添加动态查询参数
EN

Stack Overflow用户
提问于 2021-04-29 04:27:34
回答 1查看 1.9K关注 0票数 0

是否可以使用Swagger上的Fastify记录动态查询参数,从而允许客户端使用Swaggerv1.0.0在Swagger上传递文本字段中的param值?在我的示例中是输入conversationId参数的动态值。

下面是配置文件夹中的swagger.js文件。

代码语言:javascript
运行
复制
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'], 
    }
  }

+这是我的路线

代码语言:javascript
运行
复制
const healthBotController = require('../controllers/healthBotWrapperController')

const routes = [
  {
    method: 'GET',
    url: '/',
    handler: healthBotController.getEndpoints
  },
]

module.exports = routes;

我试着搜索和阅读文档,但是我还没有找到解决问题的方法。

谢谢你的进阶。

EN

Stack Overflow用户

回答已采纳

发布于 2021-05-05 07:42:41

要在swagger中添加查询参数,需要在路由配置中定义JSON架构:

代码语言:javascript
运行
复制
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中您将看到:

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67311040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档