Skip to content

[Serializer] Add COLLECT_EXTRA_ATTRIBUTES_ERRORS doc #16872

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.2
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
33 changes: 31 additions & 2 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ when constructing the normalizer::
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
]);

.. tip::

If your object contains nested objects, an ``ExtraAttributesException`` will be thrown at first nested object containing
extra attributes. When this happens, you can use :ref:`Attributes Groups section <component-serializer-collecting-extra-attributes-errors-while-denormalizing>`

Deserializing in an Existing Object
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -1266,8 +1271,8 @@ Collecting Type Errors While Denormalizing
------------------------------------------

When denormalizing a payload to an object with typed properties, you'll get an
exception if the payload contains properties that don't have the same type as
the object.
exception ``NotNormalizableValueException`` if the payload contains properties that don't have the same type as
the object, at first incorrect type.

In those situations, use the ``COLLECT_DENORMALIZATION_ERRORS`` option to
collect all exceptions at once, and to get the object partially denormalized::
Expand All @@ -1291,6 +1296,30 @@ collect all exceptions at once, and to get the object partially denormalized::
return $this->json($violations, 400);
}

Collecting Extra Attributes Errors While Denormalizing
------------------------------------------

When denormalizing a payload with ``ALLOW_EXTRA_ATTRIBUTES`` set to false to an object with nested objects, you'll get an
exception at first ``ExtraAttributesException`` in nested objects.

In those situations, use the ``COLLECT_EXTRA_ATTRIBUTES_ERRORS`` option to
collect all exceptions at once, and to get the object partially denormalized::

try {
$dto = $serializer->deserialize($request->getContent(), MyDto::class, 'json', [
DenormalizerInterface::COLLECT_EXTRA_ATTRIBUTES_ERRORS => true,
]);
} catch (PartialDenormalizationException $e) {
$violations = new ConstraintViolationList();
if (null !== $extraAttributesException = $e->getExtraAttributesError()) {
foreach ($extraAttributesException->getExtraAttributes() as $extraAttribute) {
$violations->add(new ConstraintViolation('This attribute is not allowed.', '', [], null, $extraAttribute, null));
};
}

return $this->json($violations, 400);
}

Handling Circular References
----------------------------

Expand Down