Skip to content

Commit

Permalink
bug #27826 [Serializer] Fix serialization of items with groups across…
Browse files Browse the repository at this point in the history
… entities and discrimination map (sroze)

This PR was merged into the 4.1 branch.

Discussion
----------

[Serializer] Fix serialization of items with groups across entities and discrimination map

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27816, #27641
| License       | MIT
| Doc PR        | ø

I believe this approach is better than the one taken in #27816. At least, it's an alternative :)

Commits
-------

c648b93 Fix serialization of abstract items with groups across multiple entities
  • Loading branch information
nicolas-grekas committed Jul 9, 2018
2 parents bbe3ce8 + c648b93 commit a552e84
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Expand Up @@ -142,11 +142,16 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
return false;
}

if (null !== $this->classDiscriminatorResolver && null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForMappedObject($classOrObject)) {
$allowedAttributes[] = $attributesAsString ? $discriminatorMapping->getTypeProperty() : new AttributeMetadata($discriminatorMapping->getTypeProperty());
if (null !== $this->classDiscriminatorResolver) {
$class = \is_object($classOrObject) ? \get_class($classOrObject) : $classOrObject;
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForMappedObject($classOrObject)) {
$allowedAttributes[] = $attributesAsString ? $discriminatorMapping->getTypeProperty() : new AttributeMetadata($discriminatorMapping->getTypeProperty());
}

foreach ($discriminatorMapping->getTypesMapping() as $class) {
$allowedAttributes = array_merge($allowedAttributes, parent::getAllowedAttributes($class, $context, $attributesAsString));
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) {
$allowedAttributes = array_merge($allowedAttributes, parent::getAllowedAttributes($mappedClass, $context, $attributesAsString));
}
}
}

Expand Down
Expand Up @@ -11,9 +11,15 @@

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Annotation\Groups;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*/
class DummyMessageNumberTwo implements DummyMessageInterface
{
/**
* @Groups({"two"})
*/
public $three;
}

0 comments on commit a552e84

Please sign in to comment.