-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add support for discriminator map in property normalizer #60511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
@derrabus You're probably very busy, but I'm really curious to hear your feedback on this 😊 |
@mtarld @nicolas-grekas Friendly ping to pick your brain on this topic, since you worked on the discriminator mapping in #52681. I'm curious to hear if my approach makes sense, or that I should investigate time in finding an alternative solution. Any advice is much appreciated, thanks 😊 |
src/Symfony/Component/Serializer/Tests/Fixtures/DummyMessageNumberFour.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php
Outdated
Show resolved
Hide resolved
ad5ce53
to
930d792
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me. BC is not affected since no API changes. The behavior change is the bug fix so no regression either to me.
src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last thing, otherwise, LGTM!
src/Symfony/Component/Serializer/Tests/Normalizer/PropertyNormalizerTest.php
Outdated
Show resolved
Hide resolved
Fixes symfony#60214 Currently it's not possible to serialize an object using the PropertyNormalizer when a DiscriminatorMap attribute is used. It produces the following error: > Symfony\Component\Serializer\Exception\NotNormalizableValueException: Type property "type" not found > for the abstract object "Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface". The ObjectNormalizer overrides the `getAllowedAttributes` from AbstractNormalizer and adds support for discriminators. But the PropertyNormalizer does not do this. Therefore it doesn't work. For now, we copy the logic from ObjectNormalizer to PropertyNormalizer and the problem goes away.
Thanks for the help @mtarld and @nicolas-grekas 🎉 The failing test is unrelated to these changes. |
Thank you @ruudk. |
I probably got similar problem at symfony 7.1.5 Test Case:
Runs this: $serialized = $this->serializer->serialize($payload, 'json');
But then i drop Ignore attribute - it passes!! {"type":"base_payload","a":"string","aString":true} |
@VitekSkrip please give this PR a try and create a new issue if it doesn't fix it for you then. |
what version of symfony or symfony/serializer should i use then? |
@VitekSkrip This was just merged to 6.4.x.dev. It will take some time before it is merged to 7.2 and 7.3. Since you are on 7.1 you won't get it, unless you upgrade. |
Currently it's not possible to serialize an object using the PropertyNormalizer when a DiscriminatorMap attribute is used.
It produces the following error:
The ObjectNormalizer overrides the
getAllowedAttributes
from AbstractNormalizer and adds support for discriminators. But the PropertyNormalizer does not do this. Therefore it doesn't work.For now, we copy the logic from ObjectNormalizer to PropertyNormalizer and the problem goes away.