2016-01-05 68 views
3

我正在使用swagger-api和swagger-editor編輯路由的RESTful API。 我想不通爲什麼我通過身體發送的JSON,從來沒有達到我的控制器。Swagger POST Json正文參數架構YAML

這裏是我的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 

發佈請求我送的個例。

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

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

如果我設置參數「名稱」爲不用,我剛收到這樣

{ param1: 
    { path: [ 'paths', '/projects', 'post', 'parameters', '0' ], 
    schema: 
     { name: 'param1', 
     in: 'body', 
     description: 'description', 
     required: false, 
     schema: [Object] }, 
    originalValue: {}, 
    value: {} } } 

空響應的迴應,我不知道爲什麼,因爲其他格式,如標題,路徑或formdata工作正常。 我總是收到一個空的對象。 req.swagger.params沒有價值。 我嘗試了幾種模式,但即使最簡單也沒有。 我可以從頭文件中看出'content-type':'application/json'。 因此,內容類型已設置,模式將驗證名爲「name」的簡單字符串參數。一切都應該沒問題,但仍然沒有。

+0

這可能是完全不相關的,但對我來說,我必須將'swagger-express-mw'節點包降級回0.1.0來臨時解決這個問題。 – badsyntax

+0

我沒有降級軟件包,但由於其他原因,仍然是相同的。 我在github上打開了一個問題。 – camille

+0

你可以發佈到github問題的鏈接嗎? – badsyntax

回答

0

此問題已得到解決。 這不是招搖的相關。 我用nodeJs構建了一個API,並且我意識到我沒有處理身體參數的中間件。 所以,因爲我在啓用swagger中間件之前錯過了一個步驟,所以我無法對身體參數做任何事情。