【问题标题】:NelmioApiDocBundle set Response content type as fileNelmioApiDocBundle 将响应内容类型设置为文件
【发布时间】:2021-04-20 02:42:46
【问题描述】:

我正在使用 NelmioApiDocBundle 4.0 来生成我的 API 文档。当我尝试将响应类型设置为文件时,问题就开始了。我有一个从数据库中获取文件并将其作为 StreamedResponse 返回的方法:

return new StreamedResponse(function () use ($consent) {
                    fpassthru($consent);
                    exit();
                }, 200, [
                    'Content-Transfer-Encoding', 'binary',
                    'Content-Type' => 'application/pdf',
                    'Content-Disposition' => sprintf('attachment; filename="%s.pdf"', 'filename'),
                    'Content-Length' => fstat($consent)['size'],
                ]);

此代码运行良好并返回请求的文件,但我无法在文档中设置适当的响应类型。 我试过this way,所以mi注解是这样的:

     * @OA\Response(
     *     response=200,
     *     description="My description",
     *     content="application/pdf",
     *     @OA\Schema(
     *      type="string",
     *      format="binary"
     *    )
     * )

但响应是:

Warning: Invalid argument supplied for foreach()

问题从我设置 content 属性开始,但没有它 this is the result

【问题讨论】:

  • 看起来您需要将@OA\Schema 放入@OA\MediaType - 请参阅this example。这行得通吗?
  • 我试过了,它成功了,谢谢,把你的评论作为回复,所以我可以像正确答案一样接受它

标签: symfony swagger php-7 symfony5 nelmioapidocbundle


【解决方案1】:

你需要把@OA\Schema放在@OA\MediaType里面:

     * @OA\Response(
     *     response=200,
     *     description="My description",
     *     @OA\MediaType(
     *         mediaType="application/pdf",
     *         @OA\Schema(
     *            type="string",
     *            format="binary"
     *         )
     *     )
     * )

【讨论】:

    猜你喜欢
    • 2015-03-10
    • 1970-01-01
    • 2011-07-26
    • 2015-10-09
    • 2023-03-06
    • 2019-03-03
    • 2012-04-20
    • 1970-01-01
    • 2012-02-28
    相关资源
    最近更新 更多