简体   繁体   English

带有multipart / form-data和@ApiImplicitBody的Swagger文档

[英]Swagger documentation with multipart/form-data and @ApiImplicitBody

I'm trying to create a controller action, which has to upload some files and saves them with some strings into the database. 我正在尝试创建一个控制器动作,该动作必须上传一些文件并将它们与一些字符串一起保存到数据库中。 For upload files I use FileFieldsInterceptor and for other data strings DTO. 对于上传文件,我使用FileFieldsInterceptor ,对于其他数据字符串DTO。 It works. 有用。 But I want document this endpoint by swagger and it doesn't work. 但是我想用草率的文档记录此端点,但它不起作用。 For files, I can use @ApiImplicitFile with @ApiConsumes('multipart/form-data') , but for other body parameters I try use @ApiImplicitBody decorator and the app crashes with an error in api-parameters.explorer.js . 对于文件,我可以将@ApiImplicitFile@ApiConsumes('multipart/form-data') ,但对于其他主体参数,我尝试使用@ApiImplicitBody装饰器,并且该应用程序崩溃,并在api-parameters.explorer.js出现错误。

How can I document a body parameter (other than file) into multipart/form-data by @nestjs/swagger module ? 如何通过@nestjs/swagger模块将主体参数(文件除外)记录到multipart/form-data

@Post()
@ApiOperation({ ...config.api.post })
@ApiConsumes('multipart/form-data')
@ApiImplicitFile({ name: 'file', required: true, description: 'Infographic file' })
@ApiImplicitFile({ name: 'file_preview', required: true, description: 'Infographic preview file' })
@ApiImplicitBody({ name: 'name', required: true, description: 'Infographic title', type: 'string' })
@UseInterceptors(FileFieldsInterceptor([
        { name: 'file', maxCount: 1 },
        { name: 'file_preview', maxCount: 1 },
    ],
    {
        storage: storageUpload('infographics'),
        limits: {
            fileSize: 20971520, // 20Mb
        },
        fileFilter: (req, file, cb) => {
            const mimeTypeList = ['image/png', 'image/jpeg', 'application/pdf'];

            return mimeTypeList.some(item => item === file.mimetype)
                ? cb(null, true)
                : cb(null, false);
        },
    },
  ), new FilesValidationInterceptor())
upload(@UploadedFiles() files, @Body() createDto: CreateInfographicsDto) {
    return this.infographicsService.create(files, createDto);
}

Error 错误

You have to use String instead of 'string' for the type: 您必须使用String而不是'string'作为类型:

@ApiImplicitBody({ name: 'name', description: 'Infographic title', type: String })
                                                                         ^^^^^^


Form Data 表格数据

Documenting form-data parameters is not yet supported by @nestjs/swagger according to this github issue . 根据此github问题 ,@ nestjs / swagger尚不支持记录表单数据参数。 There already exists a pull request that would add this feature but it is not merges yet. 已经存在将添加此功能的拉取请求 ,但尚未合并。 You can subscribe to it to get notified when the status updates. 您可以订阅它以在状态更新时得到通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM