首页 文章

如何在Swagger中指定GET参数的示例?

提问于
浏览
12

我正在使用在线Swagger Editor为我的API创建一个Swagger规范 .

我的API有一个GET请求 endpoints ,我使用以下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参数时设置示例?

1 回答

  • 22

    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   # <-----
    

    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
    

相关问题