【问题标题】:Swagger POST Json Body Parameter Schema YAMLSwagger POST Json 正文参数模式 YAML
【发布时间】:2016-04-09 00:46:30
【问题描述】:

我正在使用 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”设置为不需要,我只会收到这样的空响应 {参数1: {路径:['路径','/项目','发布','参数','0'], 架构: {名称:'param1', 在:“身体”, 描述:'描述', 必需:假, 架构:[对象]}, 原始值:{}, 价值: {} } } 我不知道为什么其他格式(例如标题、路径或表单数据)可以正常工作。 我总是收到一个空对象。 req.swagger.params 没有价值。 我尝试了几种模式,但即使是最简单的也不起作用。 我可以从标题中看出“内容类型”:“应用程序/json”。 因此设置了内容类型,模式验证了一个名为“name”的简单字符串参数。一切都应该没问题。但仍然没有。

【问题讨论】:

  • 这可能完全不相关,但对我来说,我不得不将 swagger-express-mw 节点包降级回 0.1.0 以暂时解决此问题。
  • 我确实降级了软件包,但由于其他原因,仍然一样。我在 github 上打开了一个问题。
  • 你能把github问题的链接贴出来吗?
  • 你可以找到打开的问题here 但恐怕答案不是很有帮助

标签: json post yaml swagger swagger-editor


【解决方案1】:

此问题已修复。 这与招摇无关。 我用 nodeJs 构建了一个 API,我意识到我没有一个中间件来处理 body 参数。 所以因为我在启用 swagger 中间件之前错过了一步,所以我无法对 body 参数做任何事情。

【讨论】:

    【解决方案2】:

    在向 API 后端发送 json 数据时获得空值的主要原因是您在大多数情况下提供的参数路径以及您为参数提供的命名。

    您还必须明确声明您期望的架构类型

    您必须将参数名称设置为 body 并设置 in: body 以便它选择正文对象作为 JSON

    这里有一个例子。你可以试试看

    /auth/register:
        post:
          tags:
            - Auth
          parameters:
            - in: body
              name: user
              description: Create a new user.
              schema:
                type: object
                required:
                  - firstName
                  - lastName
                  - email
                  - password
                  - confirmPassword
                properties:
                  firstName:
                    type: string
                  lastName:
                    type: string
                  email:
                    type: string
                  password:
                    type: string
                  confirmPassword:
                    type: string
                example:
                  firstName: Jane
                  lastName: Doe
                  email: janedoe@gmail.com
                  password: pass
                  confirmPassword: pass
          responses:
            "200":
              description: OK
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多