@@ -350,9 +350,10 @@ protected function instantiateObject(array &$data, string $class, array &$contex
350
350
$ constructorParameters = $ constructor ->getParameters ();
351
351
$ missingConstructorArguments = [];
352
352
$ params = [];
353
+ $ constructorParamKeys = [];
353
354
foreach ($ constructorParameters as $ constructorParameter ) {
354
355
$ paramName = $ constructorParameter ->name ;
355
- $ key = $ this ->nameConverter ? $ this ->nameConverter ->normalize ($ paramName , $ class , $ format , $ context ) : $ paramName ;
356
+ $ constructorParamKeys [] = $ key = $ this ->nameConverter ? $ this ->nameConverter ->normalize ($ paramName , $ class , $ format , $ context ) : $ paramName ;
356
357
357
358
$ allowed = false === $ allowedAttributes || \in_array ($ paramName , $ allowedAttributes );
358
359
$ ignored = !$ this ->isAllowedAttribute ($ class , $ paramName , $ format , $ context );
@@ -368,18 +369,14 @@ protected function instantiateObject(array &$data, string $class, array &$contex
368
369
}
369
370
370
371
$ params = array_merge ($ params , $ variadicParameters );
371
- unset($ data [$ key ]);
372
372
}
373
373
} elseif ($ allowed && !$ ignored && (isset ($ data [$ key ]) || \array_key_exists ($ key , $ data ))) {
374
374
$ parameterData = $ data [$ key ];
375
375
if (null === $ parameterData && $ constructorParameter ->allowsNull ()) {
376
376
$ params [] = null ;
377
- // Don't run set for a parameter passed to the constructor
378
- unset($ data [$ key ]);
379
377
continue ;
380
378
}
381
379
382
- // Don't run set for a parameter passed to the constructor
383
380
try {
384
381
$ params [] = $ this ->denormalizeParameter ($ reflectionClass , $ constructorParameter , $ paramName , $ parameterData , $ context , $ format );
385
382
} catch (NotNormalizableValueException $ exception ) {
@@ -390,7 +387,6 @@ protected function instantiateObject(array &$data, string $class, array &$contex
390
387
$ context ['not_normalizable_value_exceptions ' ][] = $ exception ;
391
388
$ params [] = $ parameterData ;
392
389
}
393
- unset($ data [$ key ]);
394
390
} elseif (\array_key_exists ($ key , $ context [static ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ] ?? [])) {
395
391
$ params [] = $ context [static ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ][$ key ];
396
392
} elseif (\array_key_exists ($ key , $ this ->defaultContext [self ::DEFAULT_CONSTRUCTOR_ARGUMENTS ][$ class ] ?? [])) {
@@ -421,11 +417,25 @@ protected function instantiateObject(array &$data, string $class, array &$contex
421
417
}
422
418
423
419
if (!$ constructor ->isConstructor ()) {
424
- return $ constructor ->invokeArgs (null , $ params );
420
+ $ instance = $ constructor ->invokeArgs (null , $ params );
421
+
422
+ // do not set a parameter that has been set in the constructor
423
+ foreach ($ constructorParamKeys as $ key ) {
424
+ unset($ data [$ key ]);
425
+ }
426
+
427
+ return $ instance ;
425
428
}
426
429
427
430
try {
428
- return $ reflectionClass ->newInstanceArgs ($ params );
431
+ $ instance = $ reflectionClass ->newInstanceArgs ($ params );
432
+
433
+ // do not set a parameter that has been set in the constructor
434
+ foreach ($ constructorParamKeys as $ key ) {
435
+ unset($ data [$ key ]);
436
+ }
437
+
438
+ return $ instance ;
429
439
} catch (\TypeError $ e ) {
430
440
if (!isset ($ context ['not_normalizable_value_exceptions ' ])) {
431
441
throw $ e ;
0 commit comments