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

Consider exclude rules on parents if defined #1206

Merged
merged 1 commit into from
May 24, 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
4 changes: 3 additions & 1 deletion src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ public function merge(MergeableInterface $object): void
$this->postDeserializeMethods = array_merge($this->postDeserializeMethods, $object->postDeserializeMethods);
$this->xmlRootName = $object->xmlRootName;
$this->xmlRootNamespace = $object->xmlRootNamespace;
$this->excludeIf = $object->excludeIf;
if (null !== $object->excludeIf) {
$this->excludeIf = $object->excludeIf;
}
$this->xmlNamespaces = array_merge($this->xmlNamespaces, $object->xmlNamespaces);

if ($object->accessorOrder) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/PersonAccountOnParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation as Serializer;

class PersonAccountOnParent extends PersonAccountParentWithExclude
{
/**
* @Serializer\Type("string")
*/
public $name;
}
18 changes: 18 additions & 0 deletions tests/Fixtures/PersonAccountParentWithExclude.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation as Serializer;

/**
* @Serializer\Exclude(if="object.expired")
*/
class PersonAccountParentWithExclude
{
/**
* @Serializer\Type("boolean")
*/
public $expired;
}
26 changes: 26 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
use JMS\Serializer\Tests\Fixtures\ParentNoMetadataChildObject;
use JMS\Serializer\Tests\Fixtures\ParentSkipWithEmptyChild;
use JMS\Serializer\Tests\Fixtures\PersonAccount;
use JMS\Serializer\Tests\Fixtures\PersonAccountOnParent;
use JMS\Serializer\Tests\Fixtures\PersonAccountWithParent;
use JMS\Serializer\Tests\Fixtures\PersonSecret;
use JMS\Serializer\Tests\Fixtures\PersonSecretMore;
Expand Down Expand Up @@ -330,6 +331,31 @@ public function testExcludeIfOnClassWithParent()
$this->assertEquals($accountNotExpired->name, $deserialized[0]->name);
}

public function testExcludeIfOnParentClass()
{
$accountNotExpired = new PersonAccountOnParent();
$accountNotExpired->name='Not expired account';
$accountNotExpired->expired = false;

$accountExpired = new PersonAccountOnParent();
$accountExpired->name='Expired account';
$accountExpired->expired = true;

$accounts = [$accountNotExpired,$accountExpired];

$language = new ExpressionLanguage();

$builder = SerializerBuilder::create();
$builder->setExpressionEvaluator(new ExpressionEvaluator($language));
$serializer = $builder->build();

$serialized = $serializer->serialize($accounts, $this->getFormat(), null, sprintf('array<%s>', PersonAccountOnParent::class));
$deserialized = $serializer->deserialize($serialized, sprintf('array<%s>', PersonAccountOnParent::class), $this->getFormat());

$this->assertEquals(1, count($deserialized));
$this->assertEquals($accountNotExpired->name, $deserialized[0]->name);
}

public function testExpressionExclusionNotConfigured()
{
$person = new PersonSecret();
Expand Down