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

时间:2016-04-02 22:09:22

标签: swagger definitions

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

我该怎么做?我无法在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

1 个答案:

答案 0 :(得分:9)

您必须在高级示例和低级示例之间进行选择。 在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"
    }
  ]
}