-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] When using DiscriminatorMap, order of the mapping matters #37742
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
Labels
Comments
Status: reviewed Thanks for reporting and for explaining correctly the problem. |
fabpot
added a commit
that referenced
this issue
Aug 18, 2020
…tType() when a discriminator child extends another one (fancyweb) This PR was merged into the 4.4 branch. Discussion ---------- [Serializer][ClassDiscriminatorMapping] Fix getMappedObjectType() when a discriminator child extends another one | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #37742 | License | MIT | Doc PR | - The strategy is to sort the passed classes from the "bottom" one in the hierarchy to the "top" one so that the first `is_a` in `getMappedObjectType()` is right. Commits ------- c16a192 [Serializer][ClassDiscriminatorMapping] Fix getMappedObjectType() when a discriminator child extends another one
Mapping problems were found in the new ordering logic. Example:
where
The mapping result order:
Classes 'smp' and 'nf' in the wrong order! Symfony 4.4.17, PHP 7.3.22 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Symfony version(s) affected: 4.4
Description
When using inheritance, if you have multi-level inheritance, you have to set the DiscriminatorMap in the correct order for the Serializer to find the right class because of the way it tests the class type.
How to reproduce
Create 2 classes to serialize with inheritance:
Foo
Bar extends Foo
Set the DiscriminatorMap like this:
If you serialize this, your
Bar
class will have to "type" set toFoo
.Now, invert the map:
The
Bar
class will have the correct type.This is because the function that checks the type uses
is_a()
and then exits the mapping loop if one returns true.Possible Solution
Improve the loop detecting the type and check precedence
The text was updated successfully, but these errors were encountered: