laravel - 如何在 swagger-php 数组中上传文件
问题描述
我想在 json requestBody 的 swagger-php 中上传一个文件 如何在 swagger anonations 的帮助下上传
尝试了很多时间,但运气不佳,如何在 application/json 数组中发送和归档,如果有任何关于此的信息,您能否提供帮助,那么我将解决我的问题,我对此一无所知
当此代码在终端中生成时也没有任何错误并且未显示在 swagger ui 的请求正文中
/**
* @OA\Post(
* path="/products/save",
* tags={"Product"},
* summary="Post bulk products",
* description="Return bulk products",
* @OA\RequestBody(
* required=true,
* description="Bulk products Body",
* @OA\JsonContent(
* @OA\Property(
* property="products",
* @OA\Items(
* @OA\Property(property="first_name", type="string"),
* @OA\Property(property="last_name", type="string"),
* @OA\Property(property="email", type="string"),
* @OA\Property(property="phone", type="string"),
* @OA\Property(property="resume", type="string", format="base64"),
* ),
* )
* )
* ),
* )
*/
我想要这种类型的 swagger-ui 正文,以便用户可以填写属性并以 base64 格式添加简历
{
"products": [
{
"first_name": "string",
"last_name": "string",
"email": "string",
"phone": "string",
"resume": "string" ==> here i will send base64 format of resume file
}
]
}
``
解决方案
您可以使用@OA\Property(property="file", type="string", format="binary"),
来定义文件属性:
/**
* @OA\Schema(
* schema="ProductRequest",
* required={"products"},
* @OA\Property(
* property="products",
* type="array",
* @OA\Items(
* @OA\Property(property="first_name", type="string"),
* @OA\Property(property="last_name", type="string"),
* @OA\Property(property="email", type="string"),
* @OA\Property(property="phone", type="string"),
* @OA\Property(property="resume", type="string", format="binary"),
* ),
* )
* )
*/
然后,您必须在RequestBody
using上设置媒体类型@OA\MediaType
:
/**
* @OA\RequestBody(
* request="Product",
* required=true,
* description="Bulk products Body",
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(ref="#/components/schemas/ProductRequest")
* )
* )
*/
最后在你的@OA\Post
:
/**
* @OA\Post(
* path="/products/save",
* tags={"Product"},
* summary="Post bulk products",
* description="Return bulk products",
* @OA\RequestBody(ref="#/components/requestBodies/Product"),
* @OA\Response(response=200, ref="#/components/responses/Product")
* )
*/
另请参阅有关文件数据类型和文件上传的 Swagger 文档以获取更多信息。
更新:如果您不想要单独的声明,只需像这样合并它们:
/**
* @OA\Post(
* path="/products/save",
* tags={"Product"},
* summary="Post bulk products",
* description="Return bulk products",
* @OA\RequestBody(
* required=true,
* description="Bulk products Body",
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(
* property="products",
* type="array",
* @OA\Items(
* @OA\Property(property="first_name", type="string"),
* @OA\Property(property="last_name", type="string"),
* @OA\Property(property="email", type="string"),
* @OA\Property(property="phone", type="string"),
* @OA\Property(property="resume", type="string", format="binary"),
* )
* )
* )
* )
* )
* )
*/
推荐阅读
- ag-grid - 在 ag-grid 中,服务器端模型是否支持状态栏?
- python - 数据存储客户端 query.fetch() 抛出错误
- sql - Oracle中的数据透视表?
- java - Math.pow(-27.0,1.0/3) 返回 NaN
- google-analytics - Google Analytics 是否衡量 Foxtel 广告的流量?
- javascript - 用于位置许可警报的 Hybrid Cordova App 回调
- django - 在 Django 框架下不使用 CSS 无法找到静态图像
- firebase - 除非重新部署,否则用于 Firestore 触发器的 Firebase Cloud Functions 不起作用
- ide - 当我粘贴到 CDN 中时,WebStorm 没有导入
- css - 溢出属性如何与 ul 元素一起使用?