NelmioApiDocBundle 将响应内容类型设置为文件

NelmioApiDocBundle set Response content type as file

我正在使用 NelmioApiDocBundle 4.0 生成我的 API 的文档。当我尝试将响应类型设置为文件时,问题就开始了。我有一种方法可以从数据库中获取文件并将其 return 作为 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'],
                ]);

此代码运行良好,return 请求的文件,但我无法在文档中设置适当的响应类型。 我试过 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里面:

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