Navid
Navid

Reputation: 120

Swagger won't generate right json to post from yaml

I am using swagger and want to use a post api. While it shows parameters correctly, it won't produce right command of curl to send data to server.

P.S: back-end is in node.js and express and parses YAML docs via swagger-jsdoc.

here is the 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
 */

and this is generated curl command:

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

and the response:

{
  "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."
    }
  ]
}

Upvotes: 1

Views: 644

Answers (1)

Helen
Helen

Reputation: 97932

The parameter syntax is wrong. If the operation is supposed to consume application/x-www-form-urlencoded, you need to use in: form parameters instead of 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

If it is supposed to consume JSON, you need a single in: body parameter, and individual fields (email, password, etc.) should be the properties of the body object:

 *     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

Upvotes: 1

Related Questions