如何在 swagger body 参数中定义平面对象结构

How to define flat object structure in swagger body parameter

我正在使用 swagger 2.0,需要定义一个 post 请求来创建一个帐户对象。对象数据作为平面对象结构在正文中传递:

正文数据示例:

{
  first_name: "Sherlock",
  last_name: "Holmes",
  address: "Bakerstreet 221b",
  # tax_id: not set, # optional
}

当我在 yaml 中创建请求文档时:

definitions:
  new_account:
    properties:
      first_name:
        type: string
      last_name:
        type: string
      address:
        type: string
      tax_id:
        type: string
    required:
      - first_name
      - last_name
      - address
paths:
  /accounts:
    post:
      summary: Create account
      parameters:
        - name: account
          in: body
          schema:
            $ref: "#/definitions/new_account"

文档描述了一个只有一个元素的主体:account 它本身包含一些字段。但是我的结构是扁平的,没有顶部节点 account.

如果我省略 name 属性,它基本上是相同的,只是结果文档中的名称列是空的,整个结构是必需的还是不需要的。

我目前的解决方法是列出所有参数并设置 in: query 但这显然是错误的。

这个怎么定义?

account 只是 body 参数的名称。一种用法是 Swagger Codegen 生成的 API 客户端中的方法签名。换句话说,account 不是顶级节点(仅供参考,在 Swagger 规范 1.2 中,主体参数必须命名为 body

您上面的定义对于您提供的示例正文数据是正确的。