Swagger不会生成正确的json从yaml发布

时间:2017-09-12 06:46:57

标签: json node.js curl yaml swagger

我正在使用招摇,想要使用后api。虽然它正确显示参数,但它不会产生正确的curl命令来将数据发送到服务器。

P.S:后端在node.js中,通过swagger-jsdoc表达并解析YAML文档。

这是YAML:

/**
 * @swagger
 * /register:
 *   post:
 *     tags:
 *       - report
 *     description: Returns info for panel api
 *     consumes:
 *       - application/x-www-form-urlencoded
 *     produces:
 *       - application/json
 *     parameters:
 *     -  name: email
 *        in: body
 *        description: Email
 *        required: true
 *        type: string
 *     -  name: password
 *        in: body
 *        description: password
 *        required: true
 *        type: string
 *     -  name: fullName
 *        in: body
 *        description: full name
 *        required: true
 *        type: string
 *     responses:
 *       200:
 *         description: An info for panel api
 *       401:
 *         description: If user didn't authenticate
 */

这是生成curl命令:

curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' -d 'max mcgrey' 'http://localhost:5200/register'

和回复:

{
  "status": "fail",
  "message": "Validation failed, check your inputs.",
  "errors": [
    {
      "param": "fullName",
      "msg": "Full name is required."
    },
    {
      "param": "fullName",
      "msg": "Full name must be between 1 and 50 characters long."
    },
    {
      "param": "email",
      "msg": "Email is required."
    },
    {
      "param": "email",
      "msg": "Email is required."
    },
    {
      "param": "password",
      "msg": "Password is required."
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

参数语法错误。如果操作应该使用application/x-www-form-urlencoded,则需要使用in: form参数而不是in: body

 *     consumes:
 *       - application/x-www-form-urlencoded
 *     ...
 *     parameters:
 *     -  name: email
 *        in: form              <-----------
 *        description: Email
 *        required: true
 *        type: string
 *     -  name: password
 *        in: form              <-----------
 *        description: password
 *        required: true
 *        type: string
 *     -  name: fullName
 *        in: form              <-----------
 *        description: full name
 *        required: true
 *        type: string

如果它应该使用JSON,则需要一个in: body参数,并且各个字段(emailpassword等)应该是正文对象的属性:

 *     consumes:
 *       - application/json
 *     ...
 *     parameters:
 *     -  name: body
 *        in: body
 *        required: true
 *        schema:
 *          type: object
 *          required: [email, password, fullName]
 *          properties:
 *            email:
 *              type: string
 *              format: email
 *              description: Email
 *            password:
 *              type: string
 *              format: password
 *              description: password
 *            fullName:
 *              type: string
 *              description: full name