From 0b96b432e0ce67ab28bb0036d1cc64e76db0b331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Simon=20Maria=20Mo=CC=88llers?= Date: Tue, 3 Nov 2015 21:41:27 +0100 Subject: [PATCH 1/4] bug #16445 [Serializer] Fixed `array_unique` on array of objects in `getAllowedAttributes`. --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 4bc71ca667d35..e0688f73e5c82 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -204,7 +204,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu } } - return array_unique($allowedAttributes); + return $attributesAsString ? array_unique($allowedAttributes) : $allowedAttributes; } /** From 98a4fa0f54ebaaf2f6ae8f938bbe8a04f066f73d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Simon=20Maria=20Mo=CC=88llers?= Date: Wed, 4 Nov 2015 18:47:16 +0100 Subject: [PATCH 2/4] bug #16445 [Serializer] Wrote test case for allowed attributes as objects or strings. --- .../Fixtures/AbstractNormalizerDummy.php | 60 ++++++++++++ .../Normalizer/AbstractNormalizerTest.php | 91 +++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 src/Symfony/Component/Serializer/Tests/Fixtures/AbstractNormalizerDummy.php create mode 100644 src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractNormalizerDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractNormalizerDummy.php new file mode 100644 index 0000000000000..27b2f24f53fb1 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/AbstractNormalizerDummy.php @@ -0,0 +1,60 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php new file mode 100644 index 0000000000000..84b9cb18c3dd2 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -0,0 +1,91 @@ + + */ +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); + } +} From b7ddb1bfcbaa090e881dab1e363ba8df19101042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Simon=20Maria=20Mo=CC=88llers?= Date: Tue, 3 Nov 2015 21:41:27 +0100 Subject: [PATCH 3/4] bug #16445 [Serializer] Fixed `array_unique` on array of objects in `getAllowedAttributes`. --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 4bc71ca667d35..d555fbadd5208 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -204,7 +204,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu } } - return array_unique($allowedAttributes); + return $allowedAttributes; } /** From b326ac1b3670776695a1dacec71757f68d491dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Simon=20Maria=20Mo=CC=88llers?= Date: Mon, 7 Dec 2015 00:31:56 +0100 Subject: [PATCH 4/4] bug #16445 [Serializer] Mentioned set-behavior of attribute metadata. --- .../Component/Serializer/Mapping/ClassMetadataInterface.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php b/src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php index c967666bd7e6e..095587d8961b3 100644 --- a/src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php +++ b/src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php @@ -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 */