Skip to content

Commit 56accda

Browse files
committed
minor #21042 [Serializer] Update the getSupportedTypes() docs of custom serializers (javiereguiluz)
This PR was merged into the 6.4 branch. Discussion ---------- [Serializer] Update the `getSupportedTypes()` docs of custom serializers Fixes #20341. Commits ------- a22eaed [Serializer] Update the getSupportedTypes() docs of custom serializers
2 parents 44abcda + a22eaed commit 56accda

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

serializer/custom_normalizer.rst

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,31 +164,29 @@ Improving Performance of Normalizers/Denormalizers
164164

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

167-
Both :class:`Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface`
168-
and :class:`Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface`
169-
contain a new method ``getSupportedTypes()``. This method allows normalizers or
170-
denormalizers to declare the type of objects they can handle, and whether they
171-
are cacheable. With this info, even if the ``supports*()`` call is not cacheable,
172-
the Serializer can skip a ton of method calls to ``supports*()`` improving
173-
performance substantially in some cases.
167+
Both :class:Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface
168+
and :class:Symfony\\Component\\Serializer\\Normalizer\\DenormalizerInterface
169+
define a ``getSupportedTypes()`` method to declare which types they support and
170+
whether their ``supports*()`` result can be cached.
171+
172+
This **does not** cache the actual normalization or denormalization result. It
173+
only **caches the decision** of whether a normalizer supports a given type, allowing
174+
the Serializer to skip unnecessary ``supports*()`` calls and improve performance.
174175

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

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

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

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

0 commit comments

Comments
 (0)