[PropertyInfo][Serializer] Fixed extracting ignored properties

This commit is contained in:
javer 2020-12-05 21:59:57 +02:00 committed by Nicolas Grekas
parent c59dc2eaab
commit 594ce465ce
3 changed files with 44 additions and 1 deletions

View File

@ -47,7 +47,7 @@ class SerializerExtractor implements PropertyListExtractorInterface
$serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class); $serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class);
foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) { foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
$ignored = method_exists($serializerClassMetadata, 'isIgnored') && $serializerAttributeMetadata->isIgnored(); $ignored = method_exists($serializerAttributeMetadata, 'isIgnored') && $serializerAttributeMetadata->isIgnored();
if (!$ignored && array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups())) { if (!$ignored && array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups())) {
$properties[] = $serializerAttributeMetadata->getName(); $properties[] = $serializerAttributeMetadata->getName();
} }

View File

@ -14,6 +14,8 @@ namespace Symfony\Component\PropertyInfo\Tests\Extractor;
use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor; use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;
use Symfony\Component\PropertyInfo\Tests\Fixtures\IgnorePropertyDummy;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
@ -40,4 +42,13 @@ class SerializerExtractorTest extends TestCase
$this->extractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', ['serializer_groups' => ['a']]) $this->extractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', ['serializer_groups' => ['a']])
); );
} }
public function testGetPropertiesWithIgnoredProperties()
{
if (!class_exists(Ignore::class)) {
$this->markTestSkipped('Ignore annotation is not implemented in current symfony/serializer version');
}
$this->assertSame(['visibleProperty'], $this->extractor->getProperties(IgnorePropertyDummy::class, ['serializer_groups' => ['a']]));
}
} }

View File

@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\PropertyInfo\Tests\Fixtures;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Ignore;
/**
* @author Vadim Borodavko <vadim.borodavko@gmail.com>
*/
class IgnorePropertyDummy
{
/**
* @Groups({"a"})
*/
public $visibleProperty;
/**
* @Groups({"a"})
* @Ignore
*/
private $ignoredProperty;
}