ARR*_*ARR 5 express swagger openapi swagger-jsdocs
我正在尝试将我的 Express api 从 swagger 2.0 迁移到 openapi 3.0.2。swagger 定义和路径是在 swagger-jsdoc 中编写的。这应该不会导致任何问题,因为他们的文档声明它可以与 openapi 3.x 以及 swagger 2.0 一起使用。
将 openapi: 3.0.x 添加到我的 swaggerDefinition 时,视图看起来很好。但是,当尝试通过 swagger 视图执行发布请求时,正文为空。
我的招摇定义如下所示:
const swaggerOptions = {
swaggerDefinition: {
openapi: '3.0.2',
info: {
title: `Channels API`,
description: "Channels API",
version: "v1"
},
servers: [ {
url: "http://127.0.0.1:3000",
description: 'Local server'
}],
},
apis: ['./routes/*.js'],
};
Run Code Online (Sandbox Code Playgroud)
现在我有一个文件channels.js作为路由器:它有以下定义:
class Channel extends BaseRouter {
/**
* @swagger
*
* definitions:
* channel:
* type: object
* tags:
* - Channels
* properties:
* name:
* type: string
* global:
* type: boolean
* required:
* - name
*/
constructor(app, version) {
super(app, new ChannelCtrl(), version);
}
post() {
/**
* @swagger
* /api/v1/channels:
* post:
* tags:
* - Channels
* name: Create
* produces:
* - application/json
* consumes:
* - application/json
* summary: Create new channel object
* parameters:
* - name: channel
* required: true
* in: body
* description: Fields of the channel object to be created
* type: object
* schema:
* $ref: '#/definitions/channel'
* responses:
* '200':
* description: Channel object has been created
* application/json:
* schema:
* $ref: '#/definitions/channel'
*/
this.app.post(`/api/${this.version}/channels`, this.controller.create.bind(this.controller));
return this;
}
Run Code Online (Sandbox Code Playgroud)
用 swagger 2.0 尝试似乎效果很好..我错过了什么?
小智 10
在 openapi 3.0.x 中,他们已用 requestBody 替换了参数,如下所示https://swagger.io/docs/specification/describing-request-body/所以您的文件channels.js 应该如下所示:
class Channel extends BaseRouter {
/**
* @swagger
*
* definitions:
* channel:
* type: object
* tags:
* - Channels
* properties:
* name:
* type: string
* global:
* type: boolean
* required:
* - name
*/
constructor(app, version) {
super(app, new ChannelCtrl(), version);
}
post() {
/**
* @swagger
* /api/v1/channels:
* post:
* tags:
* - Channels
* name: Create
* produces:
* - application/json
* consumes:
* - application/json
* summary: Create new channel object
* requestBody:
* content:
* application/x-www-form-urlencoded:
* schema:
* type: object
* properties:
* field1: # <!--- form field name
* type: string
* field2: # <!--- form field name
* type: string
* responses:
* '200':
* description: Channel object has been created
* application/json:
* schema:
* $ref: '#/definitions/channel'
*/
this.app.post(`/api/${this.version}/channels`, this.controller.create.bind(this.controller));
return this;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13533 次 |
| 最近记录: |