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

Skip to content

Commit c648b93

Browse files
committed
Fix serialization of abstract items with groups across multiple entities
1 parent 31dd002 commit c648b93

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)