Swagger で GET パラメータの例を指定するにはどうすればいいですか? 質問する

Swagger で GET パラメータの例を指定するにはどうすればいいですか? 質問する

私はオンラインを使用していますSwagger エディターAPI 用の Swagger 仕様を作成します。

私の API には 1 つの 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 パラメータを記述するときに例を設定するにはどうすればよいですか?

ベストアンサー1

オープンAPI2.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   # <-----

オープンAPI3.x

パラメータの例は OpenAPI 3.x でサポートされています。

      parameters:
        - name: address
          in: query
          description: Address to be foobared
          required: true
          schema:
            type: string
            example: 123, FakeStreet   # <----
          example: 456, AnotherStreet  # Overrides the schema-level example

おすすめ記事