Use annoations for serialization

This commit is contained in:
2021-05-11 19:49:41 +02:00
parent 3f64db3b3a
commit a4989f99d6
5 changed files with 48 additions and 67 deletions

View File

@@ -138,7 +138,12 @@ class ApiController extends AbstractCRUDController
return $response;
}
return $this->json($entity);
return $this->json(
$entity,
Response::HTTP_OK,
[],
$this->getContextForSerializationPostAlter($action, $request, $_format, $entity)
);
}
protected function onAfterValidation(string $action, Request $request, string $_format, $entity, ConstraintViolationListInterface $errors): ?Response
@@ -276,7 +281,26 @@ class ApiController extends AbstractCRUDController
protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array
{
return [];
switch ($request->getMethod()) {
case Request::METHOD_GET:
return [ 'groups' => [ 'read' ]];
case Request::METHOD_PUT:
case Request::METHOD_PATCH:
return [ 'groups' => [ 'write' ]];
default:
throw new \LogicException("get context for serialization is not implemented for this method");
}
}
/**
* Get the context for serialization post alter query (in case of
* PATCH, PUT, or POST method)
*
* This is called **after** the entity was altered.
*/
protected function getContextForSerializationPostAlter(string $action, Request $request, string $_format, $entity): array
{
return [ 'groups' => [ 'read' ]];
}
/**