首页 > 解决方案 > 编码不会在 swagger-ui 中的 requestBody 中爆炸属性

问题描述

该参数没有被分解为单独的字段,我无法理解为什么。

这是我的 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'

这是卷曲请求招摇给我(

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"

标签: restswaggerswagger-uiopenapi

解决方案


目前无法使用 OpenAPI 定义您的场景(具有爆炸数组的多部分请求),因为explodeandstyle行为仅定义用于application/x-www-form-urlencoded但不用于multipart/*

style
...如果请求正文媒体类型不是,则应忽略此属性application/x-www-form-urlencoded

explode
...如果请求正文媒体类型不是,则应忽略此属性application/x-www-form-urlencoded

相关讨论:Swagger UI:在表单数据中提交整数元素数组

您可能希望向OpenAPI 规范提交增强请求。


推荐阅读