Swagger openapi 3.0.x 空体

Swagger openapi 3.0.x empty body

我正在尝试将我的 swagger 2.0 迁移到 openapi 3.0.2 以用于我的 express api。 swagger 定义和路径是用 swagger-jsdoc 编写的。这应该不会造成任何问题,因为他们的文档说明它可以与 openapi 3.x 以及 swagger 2.0.

一起使用

将 openapi: 3.0.x 添加到我的 swaggerDefinition 时,视图看起来不错。 但是,当尝试通过 swagger 视图执行 post 请求时,正文是空的。

我的 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 试试好像没问题.. 我错过了什么?

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