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

问题描述 投票:14回答:1

我正在使用在线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参数时设置示例?

swagger swagger-2.0 swagger-editor openapi
1个回答
23
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.