Skip to content

Commit

Permalink
[Serializer] Support multiple levels of discriminator mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroennoten committed Jul 21, 2020
1 parent 29eeb19 commit 0fe47b7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
throw new RuntimeException(sprintf('The type "%s" has no mapped class for the abstract object "%s".', $type, $class));
}

$class = $mappedClass;
$reflectionClass = new \ReflectionClass($class);
if ($mappedClass !== $class) {
return $this->instantiateObject($data, $mappedClass, $context, new \ReflectionClass($mappedClass), $allowedAttributes, $format);
}
}

return parent::instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes, $format);
Expand Down
38 changes: 38 additions & 0 deletions Tests/Normalizer/AbstractObjectNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
Expand Down Expand Up @@ -235,6 +236,43 @@ public function hasMetadataFor($value): bool
$this->assertInstanceOf(DummySecondChildQuux::class, $normalizedData->quux);
}

public function testDenormalizeWithNestedDiscriminatorMap()
{
$classDiscriminatorResolver = new class() implements ClassDiscriminatorResolverInterface {
public function getMappingForClass(string $class): ?ClassDiscriminatorMapping
{
switch ($class) {
case AbstractDummy::class:
return new ClassDiscriminatorMapping('type', [
'foo' => AbstractDummyFirstChild::class,
]);
case AbstractDummyFirstChild::class:
return new ClassDiscriminatorMapping('nested_type', [
'bar' => AbstractDummySecondChild::class,
]);
default:
return null;
}
}

public function getMappingForMappedObject($object): ?ClassDiscriminatorMapping
{
return null;
}

public function getTypeForMappedObject($object): ?string
{
return null;
}
};

$normalizer = new AbstractObjectNormalizerDummy(null, null, null, $classDiscriminatorResolver);

$denormalizedData = $normalizer->denormalize(['type' => 'foo', 'nested_type' => 'bar'], AbstractDummy::class);

$this->assertInstanceOf(AbstractDummySecondChild::class, $denormalizedData);
}

/**
* Test that additional attributes throw an exception if no metadata factory is specified.
*/
Expand Down

0 comments on commit 0fe47b7

Please sign in to comment.