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



参考下面的例子,我想在NamedElementArray的定义中提供一个例子。这将需要显示elements属性的NamedElement数组的示例。

我该怎么做?我在型锻规范中找不到如何做到这一点的细节。

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

您必须在高级示例和低级实例之间进行选择。在Swagger UI中,高级示例位于本地示例之前。

Swagger Hub上提供的完整示例

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

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"
    }
  ]
}

相关内容

  • 没有找到相关文章

最新更新