diff --git a/components/serializer.rst b/components/serializer.rst index 164a29edc0b..c4137e36a73 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -169,10 +169,11 @@ needs three parameters: Support for the ``allow_extra_attributes`` key in the context was introduced 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:: +By default, additional attributes that are not mapped to the denormalized object +will be ignored by the Serializer component. If you prefer to throw an exception +when this happens, set the ``allow_extra_attributes`` context option to +``false`` and provide an object that implements ``ClassMetadataFactoryInterface` +when constructing the normalizer:: $data = << @@ -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, ));