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

Handle discriminator groups #1175

Merged
merged 2 commits into from
Mar 21, 2020
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
1 change: 1 addition & 0 deletions src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public function merge(MergeableInterface $object): void
$this->discriminatorFieldName = $object->discriminatorFieldName;
$this->discriminatorMap = $object->discriminatorMap;
$this->discriminatorBaseClass = $object->discriminatorBaseClass;
$this->discriminatorGroups = $object->discriminatorGroups;
}

$this->handleDiscriminatorProperty();
Expand Down
37 changes: 37 additions & 0 deletions tests/Fixtures/Discriminator/Serialization/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\Discriminator\Serialization;

use JMS\Serializer\Annotation as JMS;
use ReflectionClass;
use ReflectionException;

abstract class Entity
{
/**
* @JMS\Type("int")
* @JMS\Groups({"base"})
* @var int
*/
public $id;

public function __construct(int $id)
{
$this->id = $id;
}

/**
* @throws ReflectionException
*
* @JMS\VirtualProperty()
* @JMS\SerializedName("entityName")
* @JMS\Groups({"entity.identification"})
*/
public function getEntityName(): string
{
$reflect = new ReflectionClass($this);
return $reflect->getShortName();
}
}
23 changes: 23 additions & 0 deletions tests/Fixtures/Discriminator/Serialization/ExtendedUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\Discriminator\Serialization;

use JMS\Serializer\Annotation as JMS;

class ExtendedUser extends User
{
/**
* @JMS\Type("string")
* @JMS\Groups({"base"})
* @var string
*/
public $extendAttribute;

public function __construct($id, $name, $description, $extendAttribute)
{
parent::__construct($id, $name, $description);
$this->extendAttribute = $extendAttribute;
}
}
40 changes: 40 additions & 0 deletions tests/Fixtures/Discriminator/Serialization/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures\Discriminator\Serialization;

use JMS\Serializer\Annotation as JMS;

/**
* Class User
*
* @JMS\Discriminator(field = "entityName",
* groups={"entity.identification"},
* map = {
* "User": "JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\User",
* "ExtendedUser": "JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\ExtendedUser"
* })
*/
class User extends Entity
{
/**
* @JMS\Type("string")
* @JMS\Groups({"base"})
* @var string
*/
public $name;
/**
* @JMS\Type("string")
* @JMS\Groups({"base"})
* @var string
*/
public $description;

public function __construct(int $id, string $name, string $description)
{
parent::__construct($id);
$this->name = $name;
$this->description = $description;
}
}
29 changes: 29 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
use JMS\Serializer\Tests\Fixtures\Discriminator\ImagePost;
use JMS\Serializer\Tests\Fixtures\Discriminator\Moped;
use JMS\Serializer\Tests\Fixtures\Discriminator\Post;
use JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\ExtendedUser;
use JMS\Serializer\Tests\Fixtures\Discriminator\Serialization\User;
use JMS\Serializer\Tests\Fixtures\DiscriminatorGroup\Car as DiscriminatorGroupCar;
use JMS\Serializer\Tests\Fixtures\ExclusionStrategy\AlwaysExcludeExclusionStrategy;
use JMS\Serializer\Tests\Fixtures\FirstClassListCollection;
Expand Down Expand Up @@ -1312,6 +1314,33 @@ public function testPolymorphicObjectsWithGroup()
);
}

public function getDiscrimatorObjectsSamples(): array
{
$u1 = new User(5, 'userName', 'userDesc');
$u2 = new ExtendedUser(5, 'userName', 'userDesc', 'extednedContent');
$arr = new ArrayCollection([$u1, $u2]);

return [
[$u1, 'user_discriminator'],
[$u2, 'user_discriminator_extended'],
[$arr, 'user_discriminator_array'],
];
}

/**
* Test serializing entity that uses Discriminator and extends some base model class
*
* @dataProvider getDiscrimatorObjectsSamples
*/
public function testDiscrimatorObjects($data, $contentId)
{
$context = SerializationContext::create()->setGroups(['entity.identification']);
self::assertEquals(
$this->getContent($contentId),
$this->serialize($data, $context)
);
}

/**
* @group polymorphic
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ protected function getContent($key)
$outputs['array_iterator'] = '{"iterator":{"foo":"bar","bar":"foo"}}';
$outputs['generator'] = '{"generator":{"foo":"bar","bar":"foo"}}';
$outputs['ParentNoMetadataChildObject'] = '{"bar":"John"}';
$outputs['user_discriminator_array'] = '[{"entityName":"User"},{"entityName":"ExtendedUser"}]';
$outputs['user_discriminator'] = '{"entityName":"User"}';
$outputs['user_discriminator_extended'] = '{"entityName":"ExtendedUser"}';
}

if (!isset($outputs[$key])) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Serializer/xml/user_discriminator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<entityName><![CDATA[User]]></entityName>
</result>
9 changes: 9 additions & 0 deletions tests/Serializer/xml/user_discriminator_array.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<entry>
<entityName><![CDATA[User]]></entityName>
</entry>
<entry>
<entityName><![CDATA[ExtendedUser]]></entityName>
</entry>
</result>
4 changes: 4 additions & 0 deletions tests/Serializer/xml/user_discriminator_extended.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<entityName><![CDATA[ExtendedUser]]></entityName>
</result>