Skip to content

Commit 4f9d9f2

Browse files
committed
minor #18391 [Serializer] Document Serializer getSupportedTypes method (ndench)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Serializer] Document Serializer getSupportedTypes method Closes #18042 Documented mainly from `@nicolas`-grekas comment here #18042 (comment) and tweaked the wording to match the [New in Symfony 6.3 post](https://symfony.com/blog/new-in-symfony-6-3-performance-improvements). ![2023-06-09_15-23](https://github.com/symfony/symfony-docs/assets/2062388/7cc00bcc-bb0c-49c5-9bea-680edb318ad6) Commits ------- 524c05d [Serializer] Document Serializer getSupportedTypes method
2 parents c7578c6 + 524c05d commit 4f9d9f2

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

serializer/custom_normalizer.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,52 @@ to customize the normalized data. To do that, leverage the ``ObjectNormalizer``:
4848
}
4949
}
5050

51+
Caching the result of supportsNormalization
52+
-------------------------------------------
53+
54+
.. versionadded:: 6.3
55+
56+
The ``getSupportedTypes()`` method was introduced in Symfony 6.3.
57+
58+
Both :class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface` and
59+
:class:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface` contain a new method ``getSupportedTypes()``.
60+
This method allows normalizers or denormalizers to declare the type of objects they can handle, and whether they are
61+
cacheable. With this info, even if the ``supports*()`` call is not cacheable, the Serializer can skip a ton of method calls
62+
to ``supports*()`` improving performance substantially in some cases.
63+
64+
The ``getSupportedTypes()`` method should return an array where the keys represent the supported types, and the values
65+
indicate whether the result of the ``supports*()`` method call can be cached or not. The format of the returned array is as
66+
follows:
67+
68+
- The special key ``object`` can be used to indicate that the normalizer or denormalizer supports any classes or
69+
interfaces.
70+
- The special key ``*`` can be used to indicate that the normalizer or denormalizer might support any types.
71+
- The other keys in the array should correspond to specific types that the normalizer or denormalizer supports.
72+
- The values associated with each type should be a boolean indicating if the result of the ``supports*()`` method call for
73+
that type can be cached or not. A value of ``true`` means that the result is cacheable, while ``false`` means that the
74+
result is not cacheable.
75+
- A ``null`` value for a type means that the normalizer or denormalizer does not support that type.
76+
77+
Here is an example of how to use the ``getSupportedTypes()`` method::
78+
79+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
80+
81+
class MyNormalizer implements NormalizerInterface
82+
{
83+
// ...
84+
85+
public function getSupportedTypes(?string $format): array
86+
{
87+
return [
88+
'object' => null, // Doesn't supports any classes or interfaces
89+
'*' => false, // Supports any other types, but the result is not cacheable
90+
MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable
91+
];
92+
}
93+
}
94+
95+
Note that ``supports*()`` method implementations should not assume that ``getSupportedTypes()`` has been called before.
96+
5197
Registering it in your Application
5298
----------------------------------
5399

0 commit comments

Comments
 (0)