bug #16450 [Serializer] Fixed array_unique on array of objects in getAllowedAttributes. (CornyPhoenix)

This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #16450).

Discussion
----------

[Serializer] Fixed `array_unique` on array of objects in `getAllowedAttributes`.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #16445
| License       | MIT
| Doc PR        | symfony/symfony-docs#16445

Commits
-------

6110bd9 [Serializer] Fixed  on array of objects in .
This commit is contained in:
Fabien Potencier 2015-12-08 21:34:13 +01:00
commit 0b0070e81b
4 changed files with 155 additions and 2 deletions

View File

@ -14,7 +14,9 @@ namespace Symfony\Component\Serializer\Mapping;
/**
* Stores metadata needed for serializing and deserializing objects of specific class.
*
* Primarily, the metadata stores the list of attributes to serialize or deserialize.
* Primarily, the metadata stores the set of attributes to serialize or deserialize.
*
* There may only exist one metadata for each attribute according to its name.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/

View File

@ -259,7 +259,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
}
}
return array_unique($allowedAttributes);
return $allowedAttributes;
}
/**

View File

@ -0,0 +1,60 @@
<?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\Serializer\Tests\Fixtures;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
/**
* Provides a dummy Normalizer which extends the AbstractNormalizer.
*
* @author Konstantin S. M. Möllers <ksm.moellers@gmail.com>
*/
class AbstractNormalizerDummy extends AbstractNormalizer
{
/**
* {@inheritdoc}
*/
public function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
{
return parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return true;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return true;
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace Symfony\Component\Serializer\Tests\Normalizer;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
/**
* Provides a dummy Normalizer which extends the AbstractNormalizer.
*
* @author Konstantin S. M. Möllers <ksm.moellers@gmail.com>
*/
class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var AbstractNormalizerDummy
*/
private $normalizer;
/**
* @var ClassMetadataFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $classMetadata;
protected function setUp()
{
$loader = $this->getMock('Symfony\Component\Serializer\Mapping\Loader\LoaderChain', [], [[]]);
$this->classMetadata = $this->getMock('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory', [], [$loader]);
$this->normalizer = new AbstractNormalizerDummy($this->classMetadata);
}
public function testGetAllowedAttributesAsString()
{
$classMetadata = new ClassMetadata('c');
$a1 = new AttributeMetadata('a1');
$classMetadata->addAttributeMetadata($a1);
$a2 = new AttributeMetadata('a2');
$a2->addGroup('test');
$classMetadata->addAttributeMetadata($a2);
$a3 = new AttributeMetadata('a3');
$a3->addGroup('other');
$classMetadata->addAttributeMetadata($a3);
$a4 = new AttributeMetadata('a4');
$a4->addGroup('test');
$a4->addGroup('other');
$classMetadata->addAttributeMetadata($a4);
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['test']], true);
$this->assertEquals(['a2', 'a4'], $result);
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['other']], true);
$this->assertEquals(['a3', 'a4'], $result);
}
public function testGetAllowedAttributesAsObjects()
{
$classMetadata = new ClassMetadata('c');
$a1 = new AttributeMetadata('a1');
$classMetadata->addAttributeMetadata($a1);
$a2 = new AttributeMetadata('a2');
$a2->addGroup('test');
$classMetadata->addAttributeMetadata($a2);
$a3 = new AttributeMetadata('a3');
$a3->addGroup('other');
$classMetadata->addAttributeMetadata($a3);
$a4 = new AttributeMetadata('a4');
$a4->addGroup('test');
$a4->addGroup('other');
$classMetadata->addAttributeMetadata($a4);
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['test']], false);
$this->assertEquals([$a2, $a4], $result);
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['other']], false);
$this->assertEquals([$a3, $a4], $result);
}
}