如何在swagger中提供一个对象定义的示例,该对象定义包含一个数组作为其属性之一

3npbholx  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(203)

参考下面的例子,我想在其定义中提供一个NamedElementArray的例子。这需要为elements属性显示一个NamedElement数组的例子。
我怎么做呢?我在swagger规范中找不到如何做的细节。

swagger: '2.0'

info:
  version: "0.0.0"
  title: Example

definitions:
  Identifier:
    type: string
    format: uuid
  NamedElement:
    type: object
    properties:
      name:
        type: string
      identifier:
        $ref: "#/definitions/Identifier"
    required:
    - name
    - identifier
    example:
      name: Identifier1
      identifier: ab804529-11d0-4781-a49a-3bbbc40243df
  NamedElementArray:
    type: object
    properties:
      name: 
        type: string
      elements:
        type: array
        minLength: 0
        items:
          $ref: "#/definitions/NamedElement"
    required:
    - name
    - elements
    example:
      name: Fred
      elements:

paths:
  /elements/{name}:
    get:
      description: |
        Gets `NamedElement` objects, based on the **name** query param.
      parameters:
        -
          name: name
          in: path
          description: Name of element array to return
          required: true
          type: string
      responses:
        200:
          description: Returns a named element array
          schema:
            $ref: "#/definitions/NamedElementArray"
        default:
          description: Return nothing

字符串

cs7cruho

cs7cruho1#

你必须在 * 高级 * 示例和 * 低级 * 示例之间进行选择。在Swagger UI中,高级示例在本地示例之前。

您可以在每个属性上定义一个示例(低级别):

Identifier:
    type: string
    format: uuid
    example: Local UUID example

NamedElement:
    type: object
    properties:
        name:
            type: string
            example: Local identifier example
        identifier:
            $ref: "#/definitions/Identifier"
        required:
            - name
            - identifier

NamedElementArray:
    type: object
    properties:
      name: 
          type: string
          example: Local name example
      elements:
          type: array
          minLength: 0
          items:
              $ref: "#/definitions/NamedElement"
    required:
        - name
        - elements

字符串
在这种情况下,Swagger UI中的示例如下所示:

{
  "name": "Local name example",
  "elements": [
    {
      "name": "Local identifier example",
      "identifier": "Local UUID example"
    }
  ]
}


但是你也可以给予一个完整的高层次的例子,就像你在NamedElement上的例子一样:

NamedElementArray:
    type: object
    properties:
        name: 
            type: string
        elements:
            type: array
            minLength: 0
            items:
                $ref: "#/definitions/NamedElement"
    required:
        - name
        - elements
    example:
        name: Fred
        elements:
            - name: Identifier1
              identifier: ab804529-11d0-4781-a49a-3bbbc40243df
            - name: Identifier2
              identifier: zzz4529-11d0-4781-a49a-3bbbc40243df


在这种情况下,Swagger UI中的示例如下所示:

{
  "name": "Fred",
  "elements": [
    {
      "name": "Identifier1",
      "identifier": "ab804529-11d0-4781-a49a-3bbbc40243df"
    },
    {
      "name": "Identifier2",
      "identifier": "zzz4529-11d0-4781-a49a-3bbbc40243df"
    }
  ]
}

相关问题