Yaml metadata
Serialization of your own models is usually configured with t3api annotations (@T3api\ etc.) directly in the model. It is not possible for classes you do not own - e.g. models of 3rd party extensions (EXT:) or TYPO3 core. For such classes serialization metadata can be defined in YAML files.
YAML files are read from directories registered in $GLOBALS. Register your directory in ext_:
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['serializerMetadataDirs'] = array_merge(
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3api']['serializerMetadataDirs'] ?? [],
[
'GeorgRinger\News' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('my_ext')
. 'Resources/Private/Serializer/GeorgRinger.News',
]
);
Important
Use array_ - do not overwrite the whole array. t3api registers its own directory there (EXT:) with metadata for core classes like File or Object.
All *. files from registered directories are loaded (subdirectories are not scanned) and merged. Name of the file does not matter, but the convention is to use class name with dots, e.g. Domain.. Top level key of the file is the fully qualified class name. Syntax is the JMS serializer YAML reference.
Example from testing instance (EXT:) which exposes properties of \Georg - class extended by API resource \Source:
GeorgRinger\News\Domain\Model\News:
properties:
title:
groups: [api_get_collection_t3apinews_news, api_get_item_t3apinews_news]
teaser:
groups: [api_get_collection_t3apinews_news, api_get_item_t3apinews_news]
bodytext:
groups: [api_get_item_t3apinews_news]
datetime:
groups: [api_get_collection_t3apinews_news, api_get_item_t3apinews_news]
categories:
groups: [api_get_collection_t3apinews_news, api_get_item_t3apinews_news]
type: 'TYPO3\CMS\Extbase\Persistence\ObjectStorage<SourceBroker\T3apinews\Domain\Model\Category>'
With such configuration:
bodytextis returned only for item operation (/news/), not in collection.news/ {id} categoriesare serialized as\Source(the API resource class), becauseBroker\ T3apinews\ Domain\ Model\ Category typepoints to the extended class instead of\Georg.Ringer\ News\ Domain\ Model\ Category
Metadata merging
YAML metadata are merged with annotations of the class - values from YAML win. If the same key is set in more than one YAML file, files from directories registered later win (array_). It means that you can change metadata delivered by t3api or by other extensions - e.g. remove a property from serialization:
GeorgRinger\News\Domain\Model\News:
properties:
teaser:
exclude: true
Note
Merged metadata is generated once and cached in var/ folder. After changing YAML files flush system caches ("Flush all caches" in backend). In Development application context metadata is regenerated on every request.
Real examples. Run "ddev restart && ddev ci 14" and try those links below.
- List of news -
bodytextis not included (not inapi_group):get_ collection_ t3apinews_ news
- Single news -
bodytextis included (api_group):get_ item_ t3apinews_ news