phpsymfonyswaggernelmioapidocbundle

Symfony: Output example image in nelmio api docs?


I am using the Nelmio Api Doc to generate documentation for my API. I have just added a new endpoint which returns image/png files based on an id parameter. How am I best representing this response in my api docs? I would ideally like to display an example image in the example response section of the documentation for this endpoint. But can I do this with nelmio? Please see below:

/**
 *
 * ### Example Response ###
 *     [
 *         {TELL NELIMO TO OUTPUT EXAMPLE IMAGE?}
 *     ]
 *
 * @Route("/image/{id}", name="image_get", requirements={"id": "\d+"})
 * @Method("GET")
 *
 * @ApiDoc(
 *  section="image",
 *  description="Fetch image.",
 *  headers={
 *     {
 *          "name" : "api-key",
 *          "description"="Token the client needs to provide when making API calls.",
 *          "required"="true"
 *     }
 *  },
 *  requirements={
 *      {
 *          "name"="id",
 *          "dataType"="integer",
 *          "requirement"="\d+",
 *          "description"="ID of the image you wish to retrieve."
 *      }
 *  },
 *  parameters={},
 *  filters={},
 *  statusCodes={
 *      200="Returned when successful",
 *      400={
 *        "Returned when bad request",
 *      },
 *      401={
 *        "Returned when unauthorized",
 *      },
 *      404={
 *        "Returned when not found",
 *      }
 *   }
 * )
 */
public function getAction($id, Request $request)
{
    /** @var ImageRepository $imageRepository */
    $imageRepository = $this->get('api.repository.image');

    /** @var Image $image */
    $image = $imageRepository->fetchById($id);

    if(empty($image->getId())){
        $problem = new ApiProblem("Image not found", "E_NOT_FOUND");
        $problem->setDetail("A image could not be found.");
        $problem->setInstance($request->getUri());
        return new Response($problem->asJson(), Response::HTTP_NOT_FOUND);
    }

    /** @var string $file */
    $file = file_get_contents(__DIR__ . '/../../../../app/Resources/img/' . $flag->getImg());

    return new Response($file, 200, [
        'Content-Type' => 'image/png',
        'Content-Disposition' => 'inline; filename="'.$image->getImg().'"'
    ]);
}

Solution

  • Use markdown within the comment like so ![alt text](https://some_image.png) which will output it in the nelmio api doc.