1

Swagger を使用して、オブジェクト配列パラメーターを持つ API POST 呼び出しを文書化しようとしています。しかし、Swagger UI でテストしようとすると、 ではexplode: true無視されているようencoding:filtersです。これは私のコードです:

openapi: 3.0.2
info:
  description: >-
    My API
  version: 1.0.0
  title: My API
tags:
  - name: myApi
    description: my API
paths:
  /myApi/getList:
    post:
      tags:
        - myApi
      summary: gets a list
      description: gets a list
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                sourceId:
                  type: integer
                  description: the source id
               filters:
                  type: array
                  items:
                    $ref: '#/components/schemas/Filter'
            encoding:
              filters:
                contentType: application/json
                explode: true
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
        '500':
          description: error
components:
  schemas:
    Filter: 
      type: object
      properties:
        field:
          type: string
          description: the name of the field for this filter
        selection:
          type: array
          items:
            type: string
            description: the name of a selected value of the filter field
      required: [attribUniqueName, selection]

たとえば、パラメーターとして使用する場合

sourceId: 1

filters: [
  {
    "field": "product",
    "selection": ["Prod A", "Prod B"]
  },
  {
    "field": "country",
    "selection": ["USA", "France"]
  }
]

次に、Swagger UI は以下を使用して呼び出しを生成します (読みやすくするために URL エンコーディングを省略した場合)。

sourceId=1&filters={"field":"product","selection":["Prod A","Prod B"]},{"field":"country","selection":["USA","France"]}

どうすれば生産できるようになりますか

sourceId=1&filters={"field":"product","selection":["Prod A","Prod B"]}&filters={"field":"country","selection":["USA","France"]}

代わりは?

「エンコーディング オブジェクト」のOpenAPI 3.0.2 ドキュメントには、「explodeリクエスト ボディのメディア タイプが application/x-www-form-urlencoded でない場合、プロパティを無視する必要がある」と記載されています。ただし、ここでは application/x-www-form-urlencoded を使用しています。それともドキュメントが間違っていて、「リクエストボディのメディアタイプ」ではなく「現在のオブジェクトのコンテンツタイプ」と記載する必要がありますか? しかし、その後、パラメータ値として実際の配列を取得すると想定していました。つまり、

sourceId=1&filters=[{"field":"product","selection":["Prod A","Prod B"]},{"field":"country","selection":["USA","France"]}]

問題がある場合: Swagger UI バージョン 3.24.3 を使用しています。

4

1 に答える 1