rest - 编码不会在 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"
解决方案
目前无法使用 OpenAPI 定义您的场景(具有爆炸数组的多部分请求),因为explode
andstyle
行为仅定义用于application/x-www-form-urlencoded
但不用于multipart/*
:
style
...如果请求正文媒体类型不是,则应忽略此属性application/x-www-form-urlencoded
。
explode
...如果请求正文媒体类型不是,则应忽略此属性application/x-www-form-urlencoded
。
相关讨论:Swagger UI:在表单数据中提交整数元素数组
您可能希望向OpenAPI 规范提交增强请求。
推荐阅读
- unit-testing - 如何在单元测试中使用具有 .Net 互操作性的相对路径?
- php - 如何在前端操纵 Woocommerce 产品价格?
- python - TensorFlow 2 中的注意力层:我得到“TypeError:‘AdditiveAttention’对象不可迭代”
- java - 使用 Java 中的 Google API 获取 YouTube 频道信息
- javascript - iOS -WKWebView - 从本地主机目录/文件(.js 和资产)加载内容
- json - Rundeck 选项变量显示
- java - if 和 else 语句在 java 中的 onClick() 方法中不起作用
- node.js - 如何在 api 响应中给出自定义错误消息
- reactjs - 如何通过在 reactjs 中使用 react-data-grid 创建所需大小的网格?
- typescript - 在 TypeScript 中将导入的类与命名空间合并