Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow discriminator map in the middle of the hierarchy when deserializing #659

Merged
merged 1 commit into from
Oct 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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