Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a552e84

Browse files
bug #27826 [Serializer] Fix serialization of items with groups across 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
2 parents bbe3ce8 + c648b93 commit a552e84

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,16 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
142142
return false;
143143
}
144144

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

148-
foreach ($discriminatorMapping->getTypesMapping() as $class) {
149-
$allowedAttributes = array_merge($allowedAttributes, parent::getAllowedAttributes($class, $context, $attributesAsString));
151+
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
152+
foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) {
153+
$allowedAttributes = array_merge($allowedAttributes, parent::getAllowedAttributes($mappedClass, $context, $attributesAsString));
154+
}
150155
}
151156
}
152157

src/Symfony/Component/Serializer/Tests/Fixtures/DummyMessageNumberTwo.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111

1212
namespace Symfony\Component\Serializer\Tests\Fixtures;
1313

14+
use Symfony\Component\Serializer\Annotation\Groups;
15+
1416
/**
1517
* @author Samuel Roze <[email protected]>
1618
*/
1719
class DummyMessageNumberTwo implements DummyMessageInterface
1820
{
21+
/**
22+
* @Groups({"two"})
23+
*/
24+
public $three;
1925
}

0 commit comments

Comments
 (0)