Skip to content

Commit

Permalink
Merge pull request #659 from schmittjoh/discriminator-hierarchy-deser…
Browse files Browse the repository at this point in the history
…ialize

Allow discriminator map in the middle of the hierarchy when deserializing
  • Loading branch information
goetas authored Oct 28, 2016
2 parents 2b77c52 + 113dea5 commit 3d958dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/JMS/Serializer/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ public function merge(MergeableInterface $object)
$this->discriminatorDisabled = $object->discriminatorDisabled;
}

if ($object->discriminatorMap) {
$this->discriminatorFieldName = $object->discriminatorFieldName;
$this->discriminatorMap = $object->discriminatorMap;
$this->discriminatorBaseClass = $object->discriminatorBaseClass;
}

if ($this->discriminatorMap && ! $this->reflection->isAbstract()) {
if (false === $typeValue = array_search($this->name, $this->discriminatorMap, true)) {
throw new \LogicException(sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use JMS\Serializer\Tests\Fixtures\Doctrine\SingleTableInheritance\Student;
use JMS\Serializer\Tests\Fixtures\Doctrine\SingleTableInheritance\Teacher;
use JMS\Serializer\Tests\Fixtures\Doctrine\SingleTableInheritance\School;
use JMS\Serializer\Tests\Fixtures\Doctrine\SingleTableInheritance\Organization;

class IntegrationTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -38,13 +39,19 @@ public function testDiscriminatorIsInferredForEntityBaseClass()
$school = new School();
$json = $this->serializer->serialize($school, 'json');
$this->assertEquals('{"type":"school"}', $json);

$deserialized = $this->serializer->deserialize($json, Organization::class, 'json');
$this->assertEquals($school, $deserialized);
}

public function testDiscriminatorIsInferredForGenericBaseClass()
{
$student = new Student();
$json = $this->serializer->serialize($student, 'json');
$this->assertEquals('{"type":"student"}', $json);

$deserialized = $this->serializer->deserialize($json, Person::class, 'json');
$this->assertEquals($student, $deserialized);
}

public function testDiscriminatorIsInferredFromDoctrine()
Expand Down

0 comments on commit 3d958dc

Please sign in to comment.