Skip to content

Instantly share code, notes, and snippets.

@jgauthi
Last active April 7, 2021 17:17
Show Gist options
  • Save jgauthi/8e967e40afb51fa37ff7889cf8ab1493 to your computer and use it in GitHub Desktop.
Save jgauthi/8e967e40afb51fa37ff7889cf8ab1493 to your computer and use it in GitHub Desktop.
[Symfony] Nelmio Apidoc v4, OpenApi 3 example
<?php
namespace App\Controller\Api;
use App\Collection\CinemaCollection;
use FOS\RestBundle\Controller\{AbstractFOSRestController, Annotations as Rest};
use FOS\RestBundle\View\View;
use Nelmio\ApiDocBundle\Annotation\{Model, Security};
use OpenApi\Annotations as OA;
use Symfony\Component\HttpFoundation\Request;
/**
* @Rest\Route("/cinema")
* @OA\Tag(name="Cinema")
*/
class CinemaController extends AbstractFOSRestController
{
/**
* @Rest\Get("")
* @Rest\View(serializerGroups={"Cinema", "Pagination"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="Authorization", @OA\Schema(type="string", default="Bearer TOKEN")),
* @OA\Parameter(name="page", in="query", description="Current page (Pagination)", required=false, @OA\Schema(type="integer", default=1)),
* @OA\Parameter(name="per_page", in="query", description="Number cinemas by page (Pagination)", required=false, @OA\Schema(type="integer", default=CinemaCollection::ELEMENTS_PER_PAGE)),
* @OA\Response(
* response=200,
* description="List of cinemas",
* @OA\JsonContent(ref=@Model(type=CinemaCollection::class, groups={"Cinema", "Pagination"}))
* ),
* @OA\Response(response=401, description="Error: JWT Token invalid, expired or not found"),
* @Security(name="Bearer")
* @throws \Exception
*/
public function getAll(Request $request): View
{
//...
}
/**
* @Rest\Get("/{id}", requirements={"id"="\d+"})
* @Rest\View(serializerGroups={"Cinema", "CinemaDetails", "CinemaGroup", "Group"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="Authorization", @OA\Schema(type="string", default="Bearer TOKEN")),
* @OA\Response(
* response=200,
* description="get Cinema by ID",
* @OA\JsonContent(ref=@Model(type=App\Entity\Cinema::class, groups={"Cinema", "CinemaDetails"}))
* ),
* @OA\Response(response=401, description="Error: JWT Token invalid, expired or not found"),
* @OA\Response(response=404, description="Cinema not found")
* @Security(name="Bearer")
*/
public function getById(int $id): View
{
//...
}
/**
* @Rest\Post("")
* @Rest\View(serializerGroups={"Cinema", "CinemaDetails"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="Authorization", @OA\Schema(type="string", default="Bearer TOKEN")),
* @OA\RequestBody(
* description="Json BODY fields",
* @OA\JsonContent(ref=@Model(type=App\Entity\Cinema::class, groups={"Form"})),
* ),
* @OA\Response(
* response=201,
* description="Success",
* @OA\JsonContent(ref=@Model(type=App\Entity\Cinema::class, groups={"Cinema", "CinemaDetails"}))
* ),
* @OA\Response(response=401, description="Error: JWT Token invalid, expired or not found")
* @Security(name="Bearer")
*/
public function postOne(Request $request): View
{
//...
}
/**
* @Rest\Put("/{id}", requirements={"id"="\d+"})
* @Rest\View(serializerGroups={"Cinema", "CinemaDetails"})
* @OA\Parameter(name="id", in="query", description="Cinema ID", required=true, @OA\Schema(type="integer", default=1)),
* @OA\RequestBody(
* description="Json BODY fields",
* @OA\JsonContent(ref=@Model(type=App\Entity\Cinema::class, groups={"Form"})),
* ),
* @OA\Response(
* response=201,
* description="Success",
* @OA\JsonContent(ref=@Model(type=App\Entity\Cinema::class, groups={"Cinema", "CinemaDetails"}))
* ),
* @OA\Response(response=401, description="Error: JWT Token invalid, expired or not found"),
* @OA\Response(response=404, description="Cinema not found")
* @Security(name="Bearer")
*/
public function putOne(int $id, Request $request): View
{
//...
}
/**
* @Rest\Patch("/{id}", requirements={"id"="\d+"})
* @Rest\View(serializerGroups={"Cinema", "CinemaDetails"})
* @OA\Parameter(name="id", in="query", description="Cinema ID", required=true, @OA\Schema(type="integer", default=1)),
* @OA\RequestBody(
* description="Json BODY fields",
* @OA\JsonContent(ref=@Model(type=App\Entity\Cinema::class, groups={"Form"})),
* ),
* @OA\Response(
* response=201,
* description="Success",
* @OA\JsonContent(ref=@Model(type=App\Entity\Cinema::class, groups={"Cinema", "CinemaDetails"}))
* ),
* @OA\Response(response=401, description="Error: JWT Token invalid, expired or not found"),
* @OA\Response(response=404, description="Cinema not found")
* @Security(name="Bearer")
*/
public function patchOne(int $id, Request $request): View
{
//...
}
/**
* @Rest\Delete("/{id}", requirements={"id"="\d+"})
* @OA\Parameter(name="id", in="query", description="Cinema ID", required=true, @OA\Schema(type="integer", default=1)),
* @OA\Response(response=204, description="Cinema deleted"),
* @OA\Response(response=401, description="Error: JWT Token invalid, expired or not found"),
* @OA\Response(response=404, description="Cinema not found")
* @Security(name="Bearer")
*/
public function deleteOne(int $id): View
{
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment