1515use Symfony \Component \PropertyAccess \Exception \NoSuchPropertyException ;
1616use Symfony \Component \PropertyInfo \PropertyTypeExtractorInterface ;
1717use Symfony \Component \PropertyInfo \Type ;
18+ use Symfony \Component \Serializer \Context \ObjectChildContextTrait ;
1819use Symfony \Component \Serializer \Encoder \CsvEncoder ;
1920use Symfony \Component \Serializer \Encoder \JsonEncoder ;
2021use Symfony \Component \Serializer \Encoder \XmlEncoder ;
2122use Symfony \Component \Serializer \Exception \ExtraAttributesException ;
2223use Symfony \Component \Serializer \Exception \LogicException ;
24+ use Symfony \Component \Serializer \Exception \MissingConstructorArgumentsException ;
2325use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
24- use Symfony \Component \Serializer \Exception \ RuntimeException ;
26+ use Symfony \Component \Serializer \Instantiator \ InstantiatorInterface ;
2527use Symfony \Component \Serializer \Mapping \AttributeMetadataInterface ;
2628use Symfony \Component \Serializer \Mapping \ClassDiscriminatorFromClassMetadata ;
2729use Symfony \Component \Serializer \Mapping \ClassDiscriminatorResolverInterface ;
3335 *
3436 * @author Kévin Dunglas <dunglas@gmail.com>
3537 */
36- abstract class AbstractObjectNormalizer extends AbstractNormalizer
38+ abstract class AbstractObjectNormalizer extends AbstractNormalizer implements DenormalizerAwareInterface
3739{
40+ use DenormalizerAwareTrait;
41+ use ObjectChildContextTrait;
42+
3843 /**
3944 * Set to true to respect the max depth metadata on fields.
4045 */
@@ -93,6 +98,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
9398 public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects ' ;
9499
95100 private $ propertyTypeExtractor ;
101+ private $ instantiator ;
96102 private $ typesCache = [];
97103 private $ attributesCache = [];
98104
@@ -103,7 +109,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
103109 */
104110 protected $ classDiscriminatorResolver ;
105111
106- public function __construct (ClassMetadataFactoryInterface $ classMetadataFactory = null , NameConverterInterface $ nameConverter = null , PropertyTypeExtractorInterface $ propertyTypeExtractor = null , ClassDiscriminatorResolverInterface $ classDiscriminatorResolver = null , callable $ objectClassResolver = null , array $ defaultContext = [])
112+ public function __construct (ClassMetadataFactoryInterface $ classMetadataFactory = null , NameConverterInterface $ nameConverter = null , PropertyTypeExtractorInterface $ propertyTypeExtractor = null , ClassDiscriminatorResolverInterface $ classDiscriminatorResolver = null , callable $ objectClassResolver = null , array $ defaultContext = [], InstantiatorInterface $ instantiator = null )
107113 {
108114 parent ::__construct ($ classMetadataFactory , $ nameConverter , $ defaultContext );
109115
@@ -120,6 +126,11 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
120126 }
121127 $ this ->classDiscriminatorResolver = $ classDiscriminatorResolver ;
122128 $ this ->objectClassResolver = $ objectClassResolver ;
129+
130+ if (null === $ instantiator ) {
131+ throw new InvalidArgumentException (sprintf ('Instantiator parameter is required, please use "%s" to instantiate your Normalizer. ' , get_parent_class ($ this ).'Factory ' ));
132+ }
133+ $ this ->instantiator = $ instantiator ;
123134 }
124135
125136 /**
@@ -210,29 +221,6 @@ public function normalize($object, string $format = null, array $context = [])
210221 return $ data ;
211222 }
212223
213- /**
214- * {@inheritdoc}
215- */
216- protected function instantiateObject (array &$ data , string $ class , array &$ context , \ReflectionClass $ reflectionClass , $ allowedAttributes , string $ format = null )
217- {
218- if ($ this ->classDiscriminatorResolver && $ mapping = $ this ->classDiscriminatorResolver ->getMappingForClass ($ class )) {
219- if (!isset ($ data [$ mapping ->getTypeProperty ()])) {
220- throw new RuntimeException (sprintf ('Type property "%s" not found for the abstract object "%s". ' , $ mapping ->getTypeProperty (), $ class ));
221- }
222-
223- $ type = $ data [$ mapping ->getTypeProperty ()];
224- if (null === ($ mappedClass = $ mapping ->getClassForType ($ type ))) {
225- throw new RuntimeException (sprintf ('The type "%s" has no mapped class for the abstract object "%s". ' , $ type , $ class ));
226- }
227-
228- if ($ mappedClass !== $ class ) {
229- return $ this ->instantiateObject ($ data , $ mappedClass , $ context , new \ReflectionClass ($ mappedClass ), $ allowedAttributes , $ format );
230- }
231- }
232-
233- return parent ::instantiateObject ($ data , $ class , $ context , $ reflectionClass , $ allowedAttributes , $ format );
234- }
235-
236224 /**
237225 * Gets and caches attributes for the given object, format and context.
238226 *
@@ -307,8 +295,14 @@ public function denormalize($data, string $type, string $format = null, array $c
307295 $ normalizedData = $ this ->prepareForDenormalization ($ data );
308296 $ extraAttributes = [];
309297
310- $ reflectionClass = new \ReflectionClass ($ type );
311- $ object = $ this ->instantiateObject ($ normalizedData , $ type , $ context , $ reflectionClass , $ allowedAttributes , $ format );
298+ $ instantiatorResult = $ this ->instantiator ->instantiate ($ type , $ normalizedData , $ context , $ format );
299+ if ($ instantiatorResult ->hasFailed ()) {
300+ throw new MissingConstructorArgumentsException ($ instantiatorResult ->getError ());
301+ }
302+ $ object = $ instantiatorResult ->getObject ();
303+ $ normalizedData = $ instantiatorResult ->getUnusedData ();
304+ $ context = $ instantiatorResult ->getUnusedContext ();
305+
312306 $ resolvedClass = $ this ->objectClassResolver ? ($ this ->objectClassResolver )($ object ) : \get_class ($ object );
313307
314308 foreach ($ normalizedData as $ attribute => $ value ) {
@@ -595,23 +589,6 @@ private function isMaxDepthReached(array $attributesMetadata, string $class, str
595589 return false ;
596590 }
597591
598- /**
599- * Overwritten to update the cache key for the child.
600- *
601- * We must not mix up the attribute cache between parent and children.
602- *
603- * {@inheritdoc}
604- *
605- * @internal
606- */
607- protected function createChildContext (array $ parentContext , string $ attribute , ?string $ format ): array
608- {
609- $ context = parent ::createChildContext ($ parentContext , $ attribute , $ format );
610- $ context ['cache_key ' ] = $ this ->getCacheKey ($ format , $ context );
611-
612- return $ context ;
613- }
614-
615592 /**
616593 * Builds the cache key for the attributes cache.
617594 *
0 commit comments