Skip to content

Commit

Permalink
class level expression exclusion strategy should work with classes ha…
Browse files Browse the repository at this point in the history
…ving parents
  • Loading branch information
goetas committed May 23, 2020
1 parent 5eb72f4 commit f114dc6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ 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;
$this->xmlNamespaces = array_merge($this->xmlNamespaces, $object->xmlNamespaces);

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

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

class PersonAccountParent
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

use JMS\Serializer\Annotation as Serializer;

class PersonWithAccounts
/**
* @Serializer\Exclude(if="object.expired")
*/
class PersonAccountWithParent extends PersonAccountParent
{
/**
* @Serializer\Type("string")
*/
public $name;

/**
* @Serializer\Type("array<JMS\Serializer\Tests\Fixtures\PersonAccount>")
* @Serializer\Type("boolean")
*/
public $accounts = [];
public $expired;
}
41 changes: 31 additions & 10 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@
use JMS\Serializer\Tests\Fixtures\ParentNoMetadataChildObject;
use JMS\Serializer\Tests\Fixtures\ParentSkipWithEmptyChild;
use JMS\Serializer\Tests\Fixtures\PersonAccount;
use JMS\Serializer\Tests\Fixtures\PersonAccountWithParent;
use JMS\Serializer\Tests\Fixtures\PersonSecret;
use JMS\Serializer\Tests\Fixtures\PersonSecretMore;
use JMS\Serializer\Tests\Fixtures\PersonSecretMoreVirtual;
use JMS\Serializer\Tests\Fixtures\PersonSecretVirtual;
use JMS\Serializer\Tests\Fixtures\PersonWithAccounts;
use JMS\Serializer\Tests\Fixtures\Price;
use JMS\Serializer\Tests\Fixtures\Publisher;
use JMS\Serializer\Tests\Fixtures\SimpleInternalObject;
Expand Down Expand Up @@ -282,31 +282,52 @@ public function testString()

public function testExcludeIfOnClass()
{
$person = new PersonWithAccounts();
$person->name = 'mike';

$accountNotExpired = new PersonAccount();
$accountNotExpired->name='Not expired account';
$accountNotExpired->expired = false;
$person->accounts[] = $accountNotExpired;

$accountExpired = new PersonAccount();
$accountExpired->name='Expired account';
$accountExpired->expired = true;
$person->accounts[] = $accountExpired;

$accounts = [$accountExpired, $accountNotExpired];

$language = new ExpressionLanguage();

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

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

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

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

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

$accounts = [$accountNotExpired,$accountExpired];

$language = new ExpressionLanguage();

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

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

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

public function testExpressionExclusionNotConfigured()
Expand Down

0 comments on commit f114dc6

Please sign in to comment.