Skip to content

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

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

Closed
wants to merge 5 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
/**
* 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>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
}
}

return array_unique($allowedAttributes);
return $allowedAttributes;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be inlined:

return $attributesAsString ? array_unique($allowedAttributes) : $allowedAttributes;

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}