Description
Symfony version(s) affected
6.1
Description
When using the serializer to normalize an object that implements an interface with a DisciminatorMap and also extends from an abstract class, the ObjectSerializer doesn't use the DiscriminatorMap and makes the denormalize process failed.
The abstract class seems to have some sort of prority over the interfaces.
I couldn't find this behavior documented anywhere.
How to reproduce
I have created a repository to reproduce the problem easily: https://github.com/yurplan/serializer-bug-example with a composition example.
Here is a minimal example:
#[DiscriminatorMap(
typeProperty: 'type',
mapping: [
'cat' => Cat::class,
'dog' => Dog::class,
],
)]
interface Animal {}
abstract class AbstractEntity {}
class Cat extends AbstractEntity implements Animal {}
class Dog extends AbstractEntity implements Animal {}
$serializer->deserialize($serializer->serialize(new Cat(), 'json'));
// Throws PHP Fatal error: Uncaught Symfony\Component\Serializer\Exception\NotNormalizableValueException: Type property "type" not found for the abstract object "Animal". in vendor/symfony/serializer/Exception/NotNormalizableValueException.php:31
Possible Solution
I could create an abstract classes implementing the interfaces and extending from the previous class.
I could also create a normalizer to handle this case (i have seen #47208 (comment)).
But I think this issue could be addressed in the package itself by iterating over the interface and abstract classes when looking for a DiscriminatorMap.
There will still be an issue when 2 DiscriminatorMaps are found with the same key...
Additional Context
No response