Skip to content

Commit 579d0db

Browse files
committed
Merge branch '6.3' into 6.4
* 6.3: [Serializer] Document Serializer getSupportedTypes method
2 parents 21e222c + 9596db6 commit 579d0db

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

serializer/custom_normalizer.rst

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,58 @@ is called.
8282
All built-in :ref:`normalizers and denormalizers <component-serializer-normalizers>`
8383
as well the ones included in `API Platform`_ natively implement this interface.
8484

85-
.. _`API Platform`: https://api-platform.com
85+
Improving Performance of Normalizers/Denormalizers
86+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87+
88+
.. versionadded:: 6.3
89+
90+
The ``getSupportedTypes()`` method was introduced in Symfony 6.3.
91+
92+
Both :class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface`
93+
and :class:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface`
94+
contain a new method ``getSupportedTypes()``. This method allows normalizers or
95+
denormalizers to declare the type of objects they can handle, and whether they
96+
are cacheable. With this info, even if the ``supports*()`` call is not cacheable,
97+
the Serializer can skip a ton of method calls to ``supports*()`` improving
98+
performance substantially in some cases.
99+
100+
The ``getSupportedTypes()`` method should return an array where the keys
101+
represent the supported types, and the values indicate whether the result of
102+
the ``supports*()`` method call can be cached or not. The format of the
103+
returned array is as follows:
104+
105+
#. The special key ``object`` can be used to indicate that the normalizer or
106+
denormalizer supports any classes or interfaces.
107+
#. The special key ``*`` can be used to indicate that the normalizer or
108+
denormalizer might support any types.
109+
#. The other keys in the array should correspond to specific types that the
110+
normalizer or denormalizer supports.
111+
#. The values associated with each type should be a boolean indicating if the
112+
result of the ``supports*()`` method call for that type can be cached or not.
113+
A value of ``true`` means that the result is cacheable, while ``false`` means
114+
that the result is not cacheable.
115+
#. A ``null`` value for a type means that the normalizer or denormalizer does
116+
not support that type.
117+
118+
Here is an example of how to use the ``getSupportedTypes()`` method::
119+
120+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
86121

122+
class MyNormalizer implements NormalizerInterface
123+
{
124+
// ...
125+
126+
public function getSupportedTypes(?string $format): array
127+
{
128+
return [
129+
'object' => null, // Doesn't support any classes or interfaces
130+
'*' => false, // Supports any other types, but the result is not cacheable
131+
MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable
132+
];
133+
}
134+
}
135+
136+
Note that ``supports*()`` method implementations should not assume that
137+
``getSupportedTypes()`` has been called before.
138+
139+
.. _`API Platform`: https://api-platform.com

0 commit comments

Comments
 (0)