symfonyswaggerphp-7symfony5nelmioapidocbundle

NelmioApiDocBundle set Response content type as file


I am using NelmioApiDocBundle 4.0 to generate the documentation of my API. The problem started when i tried to set a response type as a file. I have a method that gets a file from database and returns it as 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 code works well and return the requested file but i can't set appropriate response type in the documentation. I tried this way, so mi annotations look like this:

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

but the response is:

Warning: Invalid argument supplied for foreach()

The problems starts when i set the content property, but without it this is the result


Solution

  • You need to put @OA\Schema inside @OA\MediaType:

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