bug #39299 [PropertyInfo][Serializer] Fixed extracting ignored properties for Serializer (javer)

This PR was merged into the 5.1 branch.

Discussion
----------

[PropertyInfo][Serializer] Fixed extracting ignored properties for Serializer

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Fixed typo in `SerializerExtractor::getProperties()` introduced in 8526d7c050 which leads to the error after https://github.com/symfony/symfony/pull/37040.
`$serializerClassMetadata` is instance of `Symfony\Component\Serializer\Mapping\ClassMetadata`, which doesn't contain `isIgnored` method, this methods is located in `Symfony\Component\Serializer\Mapping\AttributeMetadata` which is `$serializerAttributeMetadata` here. More over, it doesn't make sense to check the method existence in one class and call it for another.

Commits
-------

594ce465ce [PropertyInfo][Serializer] Fixed extracting ignored properties
This commit is contained in:
Nicolas Grekas 2020-12-10 23:52:55 +01:00
commit ae2925dca0
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;
}