Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b0070e

Browse files
committed
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 .
2 parents 420da14 + 6110bd9 commit 0b0070e

File tree

4 files changed

+155
-2
lines changed

4 files changed

+155
-2
lines changed

src/Symfony/Component/Serializer/Mapping/ClassMetadataInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
/**
1515
* Stores metadata needed for serializing and deserializing objects of specific class.
1616
*
17-
* Primarily, the metadata stores the list of attributes to serialize or deserialize.
17+
* Primarily, the metadata stores the set of attributes to serialize or deserialize.
18+
*
19+
* There may only exist one metadata for each attribute according to its name.
1820
*
1921
* @author Kévin Dunglas <[email protected]>
2022
*/

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
259259
}
260260
}
261261

262-
return array_unique($allowedAttributes);
262+
return $allowedAttributes;
263263
}
264264

265265
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
15+
16+
/**
17+
* Provides a dummy Normalizer which extends the AbstractNormalizer.
18+
*
19+
* @author Konstantin S. M. Möllers <[email protected]>
20+
*/
21+
class AbstractNormalizerDummy extends AbstractNormalizer
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
27+
{
28+
return parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function normalize($object, $format = null, array $context = array())
35+
{
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function supportsNormalization($data, $format = null)
42+
{
43+
return true;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function denormalize($data, $class, $format = null, array $context = array())
50+
{
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function supportsDenormalization($data, $type, $format = null)
57+
{
58+
return true;
59+
}
60+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Normalizer;
4+
5+
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
6+
use Symfony\Component\Serializer\Mapping\ClassMetadata;
7+
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
8+
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
9+
10+
/**
11+
* Provides a dummy Normalizer which extends the AbstractNormalizer.
12+
*
13+
* @author Konstantin S. M. Möllers <[email protected]>
14+
*/
15+
class AbstractNormalizerTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var AbstractNormalizerDummy
19+
*/
20+
private $normalizer;
21+
22+
/**
23+
* @var ClassMetadataFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $classMetadata;
26+
27+
protected function setUp()
28+
{
29+
$loader = $this->getMock('Symfony\Component\Serializer\Mapping\Loader\LoaderChain', [], [[]]);
30+
$this->classMetadata = $this->getMock('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory', [], [$loader]);
31+
$this->normalizer = new AbstractNormalizerDummy($this->classMetadata);
32+
}
33+
34+
public function testGetAllowedAttributesAsString()
35+
{
36+
$classMetadata = new ClassMetadata('c');
37+
38+
$a1 = new AttributeMetadata('a1');
39+
$classMetadata->addAttributeMetadata($a1);
40+
41+
$a2 = new AttributeMetadata('a2');
42+
$a2->addGroup('test');
43+
$classMetadata->addAttributeMetadata($a2);
44+
45+
$a3 = new AttributeMetadata('a3');
46+
$a3->addGroup('other');
47+
$classMetadata->addAttributeMetadata($a3);
48+
49+
$a4 = new AttributeMetadata('a4');
50+
$a4->addGroup('test');
51+
$a4->addGroup('other');
52+
$classMetadata->addAttributeMetadata($a4);
53+
54+
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
55+
56+
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['test']], true);
57+
$this->assertEquals(['a2', 'a4'], $result);
58+
59+
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['other']], true);
60+
$this->assertEquals(['a3', 'a4'], $result);
61+
}
62+
63+
public function testGetAllowedAttributesAsObjects()
64+
{
65+
$classMetadata = new ClassMetadata('c');
66+
67+
$a1 = new AttributeMetadata('a1');
68+
$classMetadata->addAttributeMetadata($a1);
69+
70+
$a2 = new AttributeMetadata('a2');
71+
$a2->addGroup('test');
72+
$classMetadata->addAttributeMetadata($a2);
73+
74+
$a3 = new AttributeMetadata('a3');
75+
$a3->addGroup('other');
76+
$classMetadata->addAttributeMetadata($a3);
77+
78+
$a4 = new AttributeMetadata('a4');
79+
$a4->addGroup('test');
80+
$a4->addGroup('other');
81+
$classMetadata->addAttributeMetadata($a4);
82+
83+
$this->classMetadata->method('getMetadataFor')->willReturn($classMetadata);
84+
85+
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['test']], false);
86+
$this->assertEquals([$a2, $a4], $result);
87+
88+
$result = $this->normalizer->getAllowedAttributes('c', ['groups' => ['other']], false);
89+
$this->assertEquals([$a3, $a4], $result);
90+
}
91+
}

0 commit comments

Comments
 (0)