编码不在 swagger-ui 的 requestBody 中爆炸 属性

Encoding not exploding a property in requestBody in swagger-ui

参数没有分解成单独的字段,我想不通为什么。

这是我的 yaml,使用 OpenApi 3.0

paths:
  /match/started:
    post:
      tags:
        - match
      summary: 'Callback for when a game has started.'
      operationId: 'App\Http\Controllers\Api\V1\MatchController::started'
      requestBody:
        description: 'Something something batman!'
        required: true
        content:
          multipart/form-data:
            schema:
              required:
                - match_uuid
              properties:
                game_uuid:
                  type: string
                player_uuids:
                  type: array
                  items:
                    type: string
              type: object
            encoding:
              player_uuids:
                style: form
                explode: true
      responses:
        200:
          description: 'success response'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Api_V1_Match_Started'

这是 swagger 给我的 curl 请求 (BAD)

curl -X POST "https://editor.swagger.io/api/v1/match/started" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "game_uuid=test" -F "player_uuids=aaa,bbb,ccc"

你可以看到最后一个参数是-F "player_uuids=aaa,bbb,ccc",应该是-F "player_uuids=aaa" -F "player_uuids=bbb" -F "player_uuids=ccc"

所以完整的请求应该是这样的:

curl -X POST "https://editor.swagger.io/api/v1/match/started" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "game_uuid=test" -F "player_uuids=aaa" -F "player_uuids=bbb" -F "player_uuids=ccc"

目前无法使用 OpenAPI 定义您的场景(带有分解数组的多部分请求),因为 explodestyle 行为仅针对 application/x-www-form-urlencoded 定义,但未定义对于 multipart/*:

style
... This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded.

explode
... This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded.

相关讨论:Swagger UI: Submit An Array of Integer Elements In formdata

您可能希望 file an enhancement request 使用 OpenAPI 规范。