19

我正在使用在线Swagger Editor为我的 API 创建 Swagger 规范。

我的 API 有一个 GET 请求端点,我使用以下 YAML 代码来描述输入参数:

paths:
  /fooBar:
    get:
      tags:
        - foobar
      summary: ''
      description: ''
      operationId: foobar
      consumes:
        - application/x-www-form-urlencoded
      produces:
        - application/json
      parameters:
        - name: address
          in: query
          description: Address to be foobared
          required: true
          type: string
          example: 123, FakeStreet
        - name: city
          in: query
          description: City of the Address
          required: true
          type: string
          example: New York

如果我放入example标签,我会收到一条错误消息:

不完全是 <#/definitions/parameter>,<#/definitions/jsonReference> 之一

在 Swagger 中编写 GET 参数时如何设置示例?

4

1 回答 1

32

开放API 2.0

OpenAPI/Swagger 2.0 没有example非正文参数的关键字。您可以在参数中指定示例description。Swagger UI v2、v3.12+ 和 Dredd 等一些工具也x-example为此目的支持扩展属性:

      parameters:
        - name: address
          in: query
          description: Address to be foobared. Example: `123, FakeStreet`.  # <-----
          required: true
          type: string
          x-example: 123, FakeStreet   # <-----

开放API 3.0

OpenAPI 3.0 原生支持参数示例:

      parameters:
        - name: address
          in: query
          description: Address to be foobared
          required: true
          schema:
            type: string
            example: 123, FakeStreet   # <----
          example: 456, AnotherStreet  # Overrides schema-level example
于 2017-05-12T09:51:22.107 回答