Skip to content

Commit

Permalink
Merge pull request #1187 from arneee/master
Browse files Browse the repository at this point in the history
Fix Support conditional exclude for classes
  • Loading branch information
goetas authored Apr 19, 2020
2 parents 6c1a951 + a685638 commit a7f3490
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
$classMetadata->registerNamespace($annot->uri, $annot->prefix);
} elseif ($annot instanceof Exclude) {
if (null !== $annot->if) {
$classMetadata->excludeIf = $this->parseExpression('!(' . $annot->if . ')');
$classMetadata->excludeIf = $this->parseExpression($annot->if);
} else {
$excludeAll = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $path):
$excludeAll = null !== $exclude ? 'true' === strtolower((string) $exclude) : false;

if (null !== $excludeIf = $elem->attributes()->{'exclude-if'}) {
$metadata->excludeIf = $this->parseExpression('!(' . (string) $excludeIf . ')');
$metadata->excludeIf = $this->parseExpression((string) $excludeIf);
}

$classAccessType = (string) ($elem->attributes()->{'access-type'} ?: PropertyMetadata::ACCESS_TYPE_PROPERTY);
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function loadMetadataFromFile(ReflectionClass $class, string $file): ?
$excludeAll = isset($config['exclude']) ? (bool) $config['exclude'] : false;

if (isset($config['exclude_if'])) {
$metadata->excludeIf = $this->parseExpression('!(' . (string) $config['exclude_if'] . ')');
$metadata->excludeIf = $this->parseExpression((string) $config['exclude_if']);
}

$classAccessType = $config['access_type'] ?? PropertyMetadata::ACCESS_TYPE_PROPERTY;
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/PersonAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use JMS\Serializer\Annotation as Serializer;

/**
* @Serializer\Exclude(if="object && object.expired != true")
* @Serializer\Exclude(if="object.expired")
*/
class PersonAccount
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Metadata/Driver/BaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ public function testExclusionIfOnClass()
$m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass($class));

$c = new ClassMetadata($class);
$c->excludeIf = $this->getExpressionEvaluator()->parse('!(object && object.expired != true)', ['context', 'class_metadata', 'object']);
$c->excludeIf = $this->getExpressionEvaluator()->parse('object.expired', ['context', 'class_metadata', 'object']);
self::assertEquals($c->excludeIf->serialize(), $m->excludeIf->serialize());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Metadata/Driver/xml/PersonAccount.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<class name="JMS\Serializer\Tests\Fixtures\PersonAccount" exclude-if="object &amp;&amp; object.expired != true">
<class name="JMS\Serializer\Tests\Fixtures\PersonAccount" exclude-if="object.expired">
<property name="name" type="string" />
<property name="expired" type="boolean"/>
</class>
Expand Down
2 changes: 1 addition & 1 deletion tests/Metadata/Driver/yml/PersonAccount.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
JMS\Serializer\Tests\Fixtures\PersonAccount:
exclude_if: 'object && object.expired != true'
exclude_if: 'object.expired'
properties:
title:
name: string
Expand Down

0 comments on commit a7f3490

Please sign in to comment.