3

我正在尝试将我的 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 尝试似乎效果很好。我错过了什么?

4

1 回答 1

7

在 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;
}
于 2020-04-16T22:59:19.767 回答