Skip to content

Document the required ClassMetadataFactory #9948

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

Closed
Closed
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ needs three parameters:
in Symfony 3.3.

By default, additional attributes that are not mapped to the denormalized
object will be ignored by the Serializer component. Set the ``allow_extra_attributes``
key of the deserialization context to ``false`` to let the serializer throw
an exception when additional attributes are passed::
object will be ignored by the Serializer component. Include a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide a ``ClassMetadataFactoryInterface`` implementation

ClassMetadataFactory when constructing the normalizer and set the
``allow_extra_attributes`` key of the deserialization context to ``false`` to
let the serializer throw an exception when additional attributes are passed::

$data = <<<EOF
<person>
Expand All @@ -184,6 +185,9 @@ an exception when additional attributes are passed::

// this will throw a Symfony\Component\Serializer\Exception\ExtraAttributesException
// because "city" is not an attribute of the Person class
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$serializer = new Serializer(array($normalizer));
$person = $serializer->deserialize($data, 'Acme\Person', 'xml', array(
'allow_extra_attributes' => false,
));
Expand Down