幸运的是,一些错误引发了有关如何处理此用例的讨论
看看这两个讨论
https://github.com/nestjs/swagger/issues/167
https://github.com/nestjs/swagger/issues/417
我能够将以下内容放在一起
我使用 DTO 添加了注释:
两个关键部分是:
在 DTO 中添加
@ApiProperty({
type: 'file',
properties: {
file: {
type: 'string',
format: 'binary',
},
},
})
public readonly file: any;
@IsString()
public readonly user_id: string;
在控制器中添加
@ApiConsumes('multipart/form-data')
这让我有一个工作端点
还有这个 OpenAPI Json
{
"/users/files":{
"post":{
"operationId":"UsersController_addPrivateFile",
"summary":"...",
"parameters":[
],
"requestBody":{
"required":true,
"content":{
"multipart/form-data":{
"schema":{
"$ref":"#/components/schemas/UploadFileDto"
}
}
}
}
}
}
}
...
{
"UploadFileDto":{
"type":"object",
"properties":{
"file":{
"type":"file",
"properties":{
"file":{
"type":"string",
"format":"binary"
}
},
"description":"...",
"example":"'file': <any-kind-of-binary-file>"
},
"user_id":{
"type":"string",
"description":"...",
"example":"cus_IPqRS333voIGbS"
}
},
"required":[
"file",
"user_id"
]
}
}