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

Fix Support conditional exclude for classes #1187

Merged
merged 1 commit into from
Apr 19, 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
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