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_users.password should never leave the server). Serialization groups decide which properties are included in the response of a given operation.

It works in two steps:

  1. Every operation declares which groups it uses - normalizationContext for the response (serialization) and denormalizationContext for the request payload (deserialization).
  2. Every property declares to which groups it belongs - with @T3api\Serializer\Groups annotation or groups key 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 = '';
}
Copied!

With configuration above:

  • GET /articles returns only title.
  • GET /articles/{id} returns title and bodytext.
  • POST /articles accepts title and bodytext from payload - internalNote sent in payload is ignored.

Naming convention 

Group names are free strings, but it is good to have a convention. Testing instance (EXT:t3apinews) uses api_<method>_<collection|item>_<extension>_<model>, e.g. api_get_collection_t3apinews_news or api_patch_item_t3apinews_news. It makes it easy to see in the model which property is exposed by which endpoint.

Customization 

Context created from normalizationContext / denormalizationContext can be changed at runtime with PSR-14 event \SourceBroker\T3api\Event\AfterCreateContextForOperationEvent (see Events). It is dispatched for serialization and deserialization context of every operation. The event gives access to the operation (getOperation()), the request (getRequest()) and the context (getContext()).

Typical use case is to conditionally expose additional properties, e.g. to return internalNote only when current frontend user belongs to group editors (uid 3):

/**
 * @T3api\Serializer\Groups({"api_editor"})
 */
protected string $internalNote = '';
Copied!
<?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']
        ));
    }
}
Copied!
Configuration/Services.yaml
services:
  Vendor\Extension\EventListener\AddEditorGroupEventListener:
    tags:
      - name: event.listener
        event: SourceBroker\T3api\Event\AfterCreateContextForOperationEvent
Copied!