8000 Add tests · symfony/symfony@383ea22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 383ea22

Browse files
committed
Add tests
1 parent 2c9c40b commit 383ea22

File tree

14 files changed

+152
-10
lines changed

14 files changed

+152
-10
lines changed

src/Symfony/Component/PropertyInfo/Extractor/SerializerExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getProperties(string $class, array $context = []): ?array
4747
$serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class);
4848

4949
foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
50-
if (array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups())) {
50+
if (!$serializerAttributeMetadata->getIgnore() && array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups())) {
5151
$properties[] = $serializerAttributeMetadata->getName();
5252
}
5353
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class AttributeMetadata implements AttributeMetadataInterface
5555
*
5656
* @internal This property is public in order to reduce the size of the
5757
* class' serialized representation. Do not access it. Use
58-
* {@link getSerializedName()} instead.
58+
* {@link getIgnore()} instead.
5959
*/
6060
public $ignore = false;
6161

@@ -123,15 +123,15 @@ public function getSerializedName(): ?string
123123
}
124124

125125
/**
126-
* Sets if this attribute must be ignored or not.
126+
* {@inheritdoc}
127127
*/
128128
public function setIgnore(bool $ignore)
129129
{
130130
$this->ignore = $ignore;
131131
}
132132

133133
/**
134-
* Gets if this attribute must be ignored or not.
134+
* {@inheritdoc}
135135
*/
136136
public function getIgnore(): bool
137137
{
@@ -156,6 +156,10 @@ public function merge(AttributeMetadataInterface $attributeMetadata)
156156
if (null === $this->serializedName) {
157157
$this->serializedName = $attributeMetadata->getSerializedName();
158158
}
159+
160+
if ($ignore = $attributeMetadata->getIgnore()) {
161+
$this->ignore = $ignore;
162+
}
159163
}
160164

161165
/**

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ public function setSerializedName(string $serializedName = null);
6161
*/
6262
public function getSerializedName(): ?string;
6363

64+
/**
65+
* Sets if this attribute must be ignored or not.
66+
*/
67+
public function setIgnore(bool $ignore);
68+
69+
/**
70+
* Gets if this attribute must be ignored or not.
71+
*/
72+
public function getIgnore(): bool;
73+
6474
/**
6575
* Merges an {@see AttributeMetadataInterface} with in the current one.
6676
*/

src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
7272
$attributesMetadata[$property->name]->setMaxDepth($annotation->getMaxDepth());
7373
} elseif ($annotation instanceof SerializedName) {
7474
$attributesMetadata[$property->name]->setSerializedName($annotation->getSerializedName());
75+
} elseif ($annotation instanceof Ignore) {
76+
$attributesMetadata[$property->name]->setIgnore(true);
7577
}
7678

7779
$loaded = true;

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,29 @@ protected function getAllowedAttributes($classOrObject, array $context, bool $at
239239

240240
$tmpGroups = $context[self::GROUPS] ?? $this->defaultContext[self::GROUPS] ?? null;
241241
$groups = (\is_array($tmpGroups) || is_scalar($tmpGroups)) ? (array) $tmpGroups : false;
242-
if (false === $groups && $allowExtraAttributes) {
243-
return false;
244-
}
245242

246243
$allowedAttributes = [];
244+
$ignoreUsed = false;
247245
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
248-
$name = $attributeMetadata->getName();
246+
if ($ignore = $attributeMetadata->getIgnore()) {
247+
$ignoreUsed = true;
248+
}
249249

250+
// If you update this check, update accordingly the one in Symfony\Component\PropertyInfo\Extractor\SerializerExtractor::getProperties()
250251
if (
251-
!$attributeMetadata->getIgnore() &&
252+
!$ignore &&
252253
(false === $groups || array_intersect($attributeMetadata->getGroups(), $groups)) &&
253-
$this->isAllowedAttribute($classOrObject, $name, null, $context)
254+
$this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context)
254255
) {
255256
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
256257
}
257258
}
258259

260+
if (!$ignoreUsed && false === $groups && $allowExtraAttributes) {
261+
// Backward Compatibility with the code using this method written before the introduction of @Ignore
262+
return false;
263+
}
264+
259265
return $allowedAttributes;
260266
}
261267

Lines changed: 35 additions & 0 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Annotation\Ignore;
15+
16+
/**
17+
* @author Kévin Dunglas <dunglas@gmail.com>
18+
*/
19+
class IgnoreDummy
20+
{
21+
public $notIgnored;
22+
/**
23+
* @Ignore
24+
*/
25+
public $ignored1;
26+
private $ignored2;
27+
28+
/**
29+
* @Ignore
30+
*/
31+
public function getIgnored2()
32+
{
33+
return $this->ignored2;
34+
}
35+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy':
2+
attributes:
3+
ignored1:
4+
ignore: foo

src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@
3434
<attribute name="foo" />
3535
</class>
3636

37+
<class name="Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy">
38+
<attribute name="ignored1" ignore="true" />
39+
<attribute name="ignored2" ignore="true" />
40+
</class>
41+
3742
</serializer>

src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@
2424
second: 'Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild'
2525
attributes:
2626
foo: ~
27+
'Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy':
28+
attributes:
29+
ignored1:
30+
ignore: true
31+
ignored2:
32+
ignore: true

src/Symfony/Component/Serializer/Tests/Mapping/AttributeMetadataTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public function testSerializedName()
5757
$this->assertEquals('serialized_name', $attributeMetadata->getSerializedName());
5858
}
5959

60+
public function testIgnore()
61+
{
62+
$attributeMetadata = new AttributeMetadata('ignored');
63+
$this->assertFalse($attributeMetadata->getIgnore());
64+
$attributeMetadata->setIgnore(true);
65+
$this->assertTrue($attributeMetadata->getIgnore());
66+
}
67+
6068
public function testMerge()
6169
{
6270
$attributeMetadata1 = new AttributeMetadata('a1');
@@ -69,11 +77,14 @@ public function testMerge()
6977
$attributeMetadata2->setMaxDepth(2);
7078
$attributeMetadata2->setSerializedName('a3');
7179

80+
$attributeMetadata2->setIgnore(true);
81+
7282
$attributeMetadata1->merge($attributeMetadata2);
7383

7484
$this->assertEquals(['a', 'b', 'c'], $attributeMetadata1->getGroups());
7585
$this->assertEquals(2, $attributeMetadata1->getMaxDepth());
7686
$this->assertEquals('a3', $attributeMetadata1->getSerializedName());
87+
$this->assertTrue($attributeMetadata1->getIgnore());
7788
}
7889

7990
public function testSerialize()

0 commit comments

Comments
 (0)
0