如何在Open API 3.0中的命名参数下嵌套JSON有效负载

时间:2019-10-04 13:14:22

标签: api swagger openapi

我正在尝试描述一个API端点,该端点接收具有以下JSON负载的PUT请求:

{
  "product": {
    "name: "foo",
    "brand": "bar"
  }
}

以下是定义的示例:

components:
  schemas:
    Product:
      type: object
      required:
        - name
        - brand
      properties:
        name:
          type: string
        brand:
          type: string
paths:
  /products/{id}:
    parameters:
      - name: id
        in: path
        schema:
          type: integer
        required: true
    put:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Product'

但是,这会产生以下JSON有效负载(缺少“ product”键):

{
  "name: "foo",
  "brand": "bar"
}

我尝试过:

      parameters:
        - name: product
          in: body
          schema:
            type: object
            properties:
              product:
                type: object
                schema:
                  $ref: '#/components/schemas/Product'

但这不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的第二个示例几乎是正确的,只需更改此部分

          schema:
            type: object
            properties:
              product:
                type: object
                schema:
                  $ref: '#/components/schemas/Product'

          schema:
            type: object
            properties:
              product:
                $ref: '#/components/schemas/Product'

请注意,$ref直接在属性名称下使用。