Skip to content

Commit

Permalink
Merge pull request #1206 from schmittjoh/exclude-on-parent
Browse files Browse the repository at this point in the history
Consider exclude rules on parents if defined
  • Loading branch information
goetas authored May 24, 2020
2 parents 6ae57ed + a076a72 commit d5f44b0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
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

0 comments on commit d5f44b0

Please sign in to comment.