Skip to content

[Serializer] Discriminating on an Enum field leads to bad cache values being used #50370

Closed
@Aweptimum

Description

@Aweptimum

Symfony version(s) affected

6.2

Description

I'm getting weird behavior in the serializer's ObjectNormalizer where it's normalizing the value of an enum property using the cached value of a previous object's enum. I have an array containing two objects - they have an enumerated property that is also used as a discriminator for the serializer. When the second object is normalized, its property value is set the first object's value instead.

It's happening in the ternary at the end of ObjectNormalizer->getAttributeValue, specifically this:

return $attribute === $this->discriminatorCache[$cacheKey] ? $this->classDiscriminatorResolver->getTypeForMappedObject($object) :
$this->propertyAccessor->getValue($object, $attribute);

Which evaluates to: "operator" === true ? "a" : "A"
The $cacheKey is the FQCN of the object, there's no hash on the end to distinguish it from other instances of the object :/

How to reproduce

I have an MRE reproducing the behavior here: https://github.com/Aweptimum/serializer-test

Possible Solution

Have the object normalizer delegate to the BackedEnum normalizer instead of the PropertyAccessor?

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions