Swagger POST Json Body Parameter Schema YAML

camille picture camille · Jan 5, 2016 · Viewed 8.5k times · Source

i'm working on a RESTful API using swagger-api and swagger-editor for routes. I can't figure out why the JSON i am sending through body, never reaches my controller. here is my YAML

  schemes:
  - http
  - https

produces: [application/json, multipart/form-data, application/x-www-form-urlencoded]

paths:
 /projects:
    x-swagger-router-controller: project
    post:
      description: create a new project
      operationId: postProjects
      consumes:
        - application/json
      parameters:
        - name: param1
          in: body
          description: description
          required: false
          schema:
            $ref: "#/definitions/Project" 
      responses:
        "200":
          description: Success
          schema:
            $ref: "#/definitions/Project" 
        default:
          description: Error
          schema: 
            $ref: "#/definitions/ErrorResponse"

definitions:
  Project:
    properties:
      name:
       type: string
    required:
      - name

an example of the post request i'm sending.

curl -v -X POST -H "Content-Type: application/json" -d '{"name":"test"}' http://127.0.0.1:10010/projects

and the response

{"message":"Request validation failed: Parameter (param1) failed schema validation","code":"SCHEMA_VALIDATION_FAILED","failedValidation":true,"results":{"errors":[{"code":"OBJECT_MISSING_REQUIRED_PROPERTY","message":"Missing required property: name","path":[]}],"warnings":[]},"path":["paths","/projects","post","parameters","0"],"paramName":"param1"}

If i set the parameter "name" as not required, i just received an empty response like this { param1: { path: [ 'paths', '/projects', 'post', 'parameters', '0' ], schema: { name: 'param1', in: 'body', description: 'description', required: false, schema: [Object] }, originalValue: {}, value: {} } } I have no clue why since other format such as header, path or formdata works fine. I always receive an empty object. req.swagger.params has no value. I tried several schema but even the simplest is not working. i can tell from the header that 'content-type': 'application/json'. So the content type is set, the schema validates a simple string argument named "name". Everything should be ok. but still not.

Answer

camille picture camille · Sep 23, 2016

This issue has been fixed. It wasn't swagger related. I built an API with nodeJs and i realized i didn't have a middleware to handle body parameter. So because i missed a step before enabled the swagger middleware, i couldn't do anything with body parameter.