4
votes

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.

1
It might be completely unrelated but for me I had to downgrade the swagger-express-mw node package back to 0.1.0 to temporarily resolve this issue.badsyntax
I did downgrade the package but for other reasons and still the same.. I opened an issue on github.camille
can you post the link to the github issue?badsyntax
you can find the opened issue here but i'm afraid the answer is not very helpfulcamille

1 Answers

0
votes

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.