Swagger POST Json Body参数模式YAML

时间:2016-01-05 08:50:30

标签: json post yaml swagger swagger-editor

我正在使用swagger-api和swagger-editor处理RESTful API。 我无法弄清楚为什么我通过身体发送的JSON,从未到达我的控制器。 这是我的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"}

如果我将参数“name”设置为不需要,我刚刚收到这样的空响应     {param1:        {path:['paths','/ projects','post','parameters','0'],          模式:           {name:'param1',             in:'body',             描述:'描述',             必需:false,             schema:[Object]},          originalValue:{},          价值:{}}} 我不知道为什么其他格式如header,path或formdata工作正常。 我总是收到一个空物。 req.swagger.params没有任何价值。 我尝试了几种模式,但即便是最简单的也不起作用。 我可以从标题中看出'content-type':'application / json'。 因此,设置了内容类型,模式验证名为“name”的简单字符串参数。一切都应该没问题。但仍然没有。

1 个答案:

答案 0 :(得分:0)

此问题已得到修复。 这并不是徒劳的。 我用nodeJs构建了一个API,我意识到我没有处理body参数的中间件。 因为我在启用swagger中间件之前错过了一步,我无法对body参数做任何事情。

相关问题