You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PHP Fatal error: Uncaught Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException: Cannot create an instance of "SubAPropertyConstructor" from serialized data because its constructor requires parameter "toto" to be present. in /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Normalizer/AbstractNormalizer.php:403
Stack trace:
#0 /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php(280): Symfony\Component\Serializer\Normalizer\AbstractNormalizer->instantiateObject()
#1 /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php(358): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->instantiateObject()
#2 /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Serializer.php(238): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->denormalize()
#3 /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php(556): Symfony\Component\Serializer\Serializer->denormalize()
#4 /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php(387): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->validateAndDenormalize()
#5 /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Serializer.php(238): Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->denormalize()
#6 /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Serializer.php(151): Symfony\Component\Serializer\Serializer->denormalize()
#7 /home/thibault/projets/bugreporter-serializer/index.php(52): Symfony\Component\Serializer\Serializer->deserialize()
#8 {main}
thrown in /home/thibault/projets/bugreporter-serializer/vendor/symfony/serializer/Normalizer/AbstractNormalizer.php on line 403
/** * Validates the submitted data and denormalizes it. * * @param Type[] $types * @param mixed $data * * @return mixed * * @throws NotNormalizableValueException * @throws LogicException */privatefunctionvalidateAndDenormalize(array$types, string$currentClass, string$attribute, $data, ?string$format, array$context)
{
$expectedTypes = [];
$isUnionType = \count($types) > 1;
foreach ($typesas$type) {
...// This try-catch should cover all NotNormalizableValueException (and all return branches after the first// exception) so we could try denormalizing all types of an union type. If the target type is not an union// type, we will just re-throw the catched exception.// In the case of no denormalization succeeds with an union type, it will fall back to the default exception// with the acceptable types list.try {
...
} catch (NotNormalizableValueException$e) {
if (!$isUnionType) {
throw$e;
}
}
}
...
}
Try / catch block on foreach union-types beacause it catches only NotNormalizableValueException .
The error comes from the fact that in the try a MissingConstructorArgumentsException is thrown by Symfony\Component\Serializer\Normalize\AbstractNormalizer.
…tor (Gwemox)
This PR was merged into the 4.4 branch.
Discussion
----------
[Serializer] Fix denormalization union types with constructor
| Q | A
| ------------- | ---
| Branch? | 4.4
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | Fix#46396
| License | MIT
Fix bug when deserialize union types with constructor. Before that, `MissingConstructorArgumentsException` was thrown even if another type matched.
Is similar to #45861
Commits
-------
33fb153 [Serializer] Fix denormalization union types with constructor
Symfony version(s) affected
4.4 - 5.4 - 6.1
Description
It's not possible to deserialize an object with a union-type property when Normalizer throw an other exception than
NotNormalizableValueException
.For examples :
How to reproduce
composer.json
index.php
Deserialize A does not work:
Deserialize B does not work:
Possible Solution
Symfony\Component\Serializer\Normalize\AbstractObjectNormalizer :
Try / catch block on foreach union-types beacause it catches only
NotNormalizableValueException
.The error comes from the fact that in the try a
MissingConstructorArgumentsException
is thrown bySymfony\Component\Serializer\Normalize\AbstractNormalizer
.Possible solution:
Additional Context
No response
The text was updated successfully, but these errors were encountered: