Symfony bundle to easily make api, inspired by work of DarwinOnLine
Exemple :
/**
* @Route("/my-path", name="my_path")
* @OA\Tag(name="MyEntity")
*/
class MyEntityCrudController extends AbstractApiController
{
public const entityClass = MyEntity::class;
public const entityCreateTypeClass = MyEntityType::class;
public const entityUpdateTypeClass = MyEntityType::class;
public const serializationGroups = ['my_entity_full'];
public const listSerializationGroups = ['my_entity_light'];
public const filterFields = [];
public const filterSortFields = [];
use CrudControllerTrait;
}
Example:
api_my_path:
resource: "@AppBundle/Controller/MyContext/MyController.php"
type: annotation
easy_api:
authentication: true
user_class: AppBundle\Entity\User\User
user_tracking :
enable: false
connection_history_class: AppBundle\Entity\User\ConnectionHistory
inheritance:
entity: 'App\Entity\AbstractEntity'
entityReferential: 'App\Entity\AbstractReferential'
form: 'App\Form\Type\AbstractCoreType'
repository: 'App\Form\Type\AbstractRepository'
controller: 'App\Controller\AbstractApiController'
serialized_form: 'App\Form\Model\SerializedForm'
tests:
debug: true # true by default
datetime_format: 'Y-m-d H:i:s' # DateTimeInterface::ATOM format by default
You can specify some options in attr field of form fields:
- group : to group fields
- discriminator
- other options you want to pass
You can use all annotations of the nelmio api bundle. The DoctrineAnnorationReader cannot read self and static constants but with this bundle you can use it as strings in two cases as long as the issue isn't closed :
In Nelmio model annotation:
/**
* Get entity.
*
* @OA\Annotations\Response(
* response=200,
* description="Successful operation",
* @OA\Annotations\Schema(
* ref=@Nelmio\ApiDocBundle\Annotation\Model(
* type="static::entityClass",
* groups={"static::serializationGroups"}
* )
* )
* ),
* @param Request $request
*
* @return Response
*/
public function getAction(Request $request)
{
...
}
And in an annotation named "GetFormParameter" which allows you to generate api-doc of form fields as get parameters :
/**
* List entities.
*
* @EasyApiBundle\Annotation\GetFormParameter(type="static::entitySearchTypeClass")
* ...
*/
public function listAction(Request $request)
{
...
}
Define it in php static::$additionalAssessableFunctions
and implement it, with good parameters :
static::$additionalAssessableFunctions = ['assertMyAssertion'];
...
/**
* @param $key The key in response
* @param $parameter The parameter passed (optionnal)
* @param $value The value in response
*/
protected static function assertMyAssertion($key, $parameter, $value): void
{
$parameter = $parameter ?? 'John';
$expected = "hello world $parameter";
$errorMessage = "Invalid value for {$key} field: expected {$expected}, get {$value}";
static::assertTrue($expected === $value, $errorMessage);
}
- Implement the Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface on normalizers (see https://symfony.com/doc/current/serializer/custom_normalizer.html)
- see https://symfony.com/doc/4.4/performance.html