Context groups
By default all properties of an entity (and all getters marked as virtual properties) are serialized. Usually it is not what you want - for performance reasons (collection does not need bodytext) and for security reasons (fe_ should never leave the server). Serialization groups decide which properties are included in the response of a given operation.
It works in two steps:
- Every operation declares which groups it uses -
normalizationfor the response (serialization) andContext denormalizationfor the request payload (deserialization).Context - Every property declares to which groups it belongs - with
@T3api\annotation orSerializer\ Groups groupskey in YAML metadata.
Only properties which share at least one group with the operation are processed. When operation does not declare any groups, all properties are processed.
use SourceBroker\T3api\Annotation as T3api;
/**
* @T3api\ApiResource(
* collectionOperations={
* "get"={
* "path"="/articles",
* "normalizationContext"={
* "groups"={"api_get_collection_article"}
* },
* },
* "post"={
* "method"="POST",
* "path"="/articles",
* "normalizationContext"={
* "groups"={"api_get_item_article"}
* },
* "denormalizationContext"={
* "groups"={"api_post_item_article"}
* },
* },
* },
* itemOperations={
* "get"={
* "path"="/articles/{id}",
* "normalizationContext"={
* "groups"={"api_get_item_article"}
* },
* },
* },
* )
*/
class Article extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* @T3api\Serializer\Groups({"api_get_collection_article", "api_get_item_article", "api_post_item_article"})
*/
protected string $title = '';
/**
* @T3api\Serializer\Groups({"api_get_item_article", "api_post_item_article"})
*/
protected string $bodytext = '';
/**
* Never serialized and can not be set from request payload.
*/
protected string $internalNote = '';
}
With configuration above:
GET /returns onlyarticles title.GET /returnsarticles/ {id} titleandbodytext.POST /acceptsarticles titleandbodytextfrom payload -internalsent in payload is ignored.Note
Note
uid (and @id if the entity is an API resource with item operation) is always added to serialized entities, regardless of groups (see AbstractEntitySubscriber).
Note
If denormalization is not set, normalization is used also for deserialization. This is a fallback kept for backward compatibility - it is recommended to always set denormalization for write operations, so it is explicit which properties can be changed by the client.
Naming convention
Group names are free strings, but it is good to have a convention. Testing instance (EXT:) uses api_<method>_<collection, e.g. api_ or api_. It makes it easy to see in the model which property is exposed by which endpoint.
Customization
Context created from normalization / denormalization can be changed at runtime with PSR-14 event \Source (see Events). It is dispatched for serialization and deserialization context of every operation. The event gives access to the operation (get), the request (get) and the context (get).
Typical use case is to conditionally expose additional properties, e.g. to return internal only when current frontend user belongs to group editors (uid 3):
/**
* @T3api\Serializer\Groups({"api_editor"})
*/
protected string $internalNote = '';
<?php
namespace Vendor\Extension\EventListener;
use SourceBroker\T3api\Event\AfterCreateContextForOperationEvent;
use TYPO3\CMS\Core\Context\Context;
class AddEditorGroupEventListener
{
public function __construct(private readonly Context $context) {}
public function __invoke(AfterCreateContextForOperationEvent $event): void
{
$serializerContext = $event->getContext();
$userGroupIds = $this->context->getPropertyFromAspect('frontend.user', 'groupIds', []);
if (!in_array(3, $userGroupIds, true) || !$serializerContext->hasAttribute('groups')) {
return;
}
$serializerContext->setGroups(array_merge(
$serializerContext->getAttribute('groups'),
['api_editor']
));
}
}
services:
Vendor\Extension\EventListener\AddEditorGroupEventListener:
tags:
- name: event.listener
event: SourceBroker\T3api\Event\AfterCreateContextForOperationEvent
Important
The event is dispatched for both directions. Group added as above is used also for deserialization of POST / PUT / PATCH payload, so editors would be able to write internal too. If you want to change only the response, check the type of context: $serializer.
Important
Responses of GET endpoints can be cached by response cache. If the response depends on the current user, skip the cache for such requests with read / write (see Conditions).