【问题标题】:Swagger openapi 3.0.x empty bodySwagger openapi 3.0.x 空体
【发布时间】:2020-02-26 07:04:54
【问题描述】:

我正在尝试将我的 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'],
};

现在我有一个文件 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;
}

用 swagger 2.0 尝试它似乎工作正常.. 我错过了什么?

【问题讨论】:

  • 您找到解决方案了吗? @ARR
  • @OrkunOzen,不,现在我切换回 swagger 2.0,但是我的其他项目没有这个问题。

标签: express swagger openapi swagger-jsdocs


【解决方案1】:

在 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;
}

【讨论】: