Skip to content

[Serializer] Update the getSupportedTypes() docs of custom serializers #21042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 6.4
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions serializer/custom_normalizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,31 +164,29 @@ Improving Performance of Normalizers/Denormalizers

The ``getSupportedTypes()`` method was introduced in Symfony 6.3.

Both :class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface`
and :class:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface`
contain a new method ``getSupportedTypes()``. This method allows normalizers or
denormalizers to declare the type of objects they can handle, and whether they
are cacheable. With this info, even if the ``supports*()`` call is not cacheable,
the Serializer can skip a ton of method calls to ``supports*()`` improving
performance substantially in some cases.
Both :class:Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface
and :class:Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface
define a ``getSupportedTypes()`` method to declare which types they support and
whether their ``supports*()`` result can be cached.

This **does not** cache the actual normalization or denormalization result. It
only **caches the decision** of whether a normalizer supports a given type, allowing
the Serializer to skip unnecessary ``supports*()`` calls and improve performance.

The ``getSupportedTypes()`` method should return an array where the keys
represent the supported types, and the values indicate whether the result of
the ``supports*()`` method call can be cached or not. The format of the
returned array is as follows:
represent the supported types, and the values indicate whether the result of the
corresponding ``supports*()`` call can be cached. The array format is as follows:

#. The special key ``object`` can be used to indicate that the normalizer or
denormalizer supports any classes or interfaces.
#. The special key ``*`` can be used to indicate that the normalizer or
denormalizer might support any types.
#. The other keys in the array should correspond to specific types that the
normalizer or denormalizer supports.
#. The values associated with each type should be a boolean indicating if the
result of the ``supports*()`` method call for that type can be cached or not.
A value of ``true`` means that the result is cacheable, while ``false`` means
that the result is not cacheable.
#. A ``null`` value for a type means that the normalizer or denormalizer does
not support that type.
denormalizer might support any type.
#. Other keys should correspond to specific types that the normalizer or
denormalizer supports.
#. The values should be booleans indicating whether the result of the
``supports*()`` call for that type is cacheable. Use ``true`` if the result
can be cached, ``false`` if it cannot.
#. A ``null`` value means the normalizer or denormalizer does not support that type.

Here is an example of how to use the ``getSupportedTypes()`` method::

Expand All @@ -201,9 +199,9 @@ Here is an example of how to use the ``getSupportedTypes()`` method::
public function getSupportedTypes(?string $format): array
{
return [
'object' => null, // Doesn't support any classes or interfaces
'*' => false, // Supports any other types, but the result is not cacheable
MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable
'object' => null, // doesn't support any classes or interfaces
'*' => false, // supports any other types, but the decision is not cacheable
MyCustomClass::class => true, // supports MyCustomClass and decision is cacheable
];
}
}
Expand Down