10000 bug #46947 [Serializer] Prevent that bad Ignore method annotations le… · symfony/symfony@8dd6bb7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8dd6bb7

Browse files
committed
bug #46947 [Serializer] Prevent that bad Ignore method annotations lead to incorrect results (astepin)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Prevent that bad Ignore method annotations lead to incorrect results fix #45016 | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #45016 | License | MIT | Doc PR | na By attaching the Ignore serializer annotation to a method that is not a set/get/has/is method, the property that was read before the method was marked as ignored. I have tweaked the behavior here so that it matches the other annotations. However, the change could now cause exceptions in older projects if they already have this bug built in. From my point of view, however, this is justifiable, because at the moment data may not be output correctly. Commits ------- edb1038 Prevent that bad Ignore method annotations lead to incorrect results
2 parents 6dc9924 + edb1038 commit 8dd6bb7

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
134134

135135
$attributeMetadata->setSerializedName($annotation->getSerializedName());
136136
} elseif ($annotation instanceof Ignore) {
137+
if (!$accessorOrMutator) {
138+
throw new MappingException(sprintf('Ignore on "%s::%s()" cannot be added. Ignore can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
139+
}
140+
137141
$attributeMetadata->setIgnore(true);
138142
} elseif ($annotation instanceof Context) {
139143
if (!$accessorOrMutator) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;
6+
7+
use Symfony\Component\Serializer\Annotation\Ignore;
8+
9+
class Entity45016
10+
{
11+
/**
12+
* @var int
13+
*/
14+
private $id = 1234;
15+
16+
public function getId(): int
17+
{
18+
return $this->id;
19+
}
20+
21+
/**
22+
* @Ignore()
23+
*/
24+
public function badIgnore(): bool
25+
{
26+
return true;
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;
6+
7+
use Symfony\Component\Serializer\Annotation\Ignore;
8+
9+
class Entity45016
10+
{
11+
/**
12+
* @var int
13+
*/
14+
private $id = 1234;
15+
16+
public function getId(): int
17+
{
18+
return $this->id;
19+
}
20+
21+
#[Ignore]
22+
public function badIgnore(): bool
23+
{
24+
return true;
25+
}
26+
}

src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ public function testThrowsOnContextOnInvalidMethod()
137137
$loader->loadClassMetadata($classMetadata);
138138
}
139139

140+
public function testCanHandleUnrelatedIgnoredMethods()
141+
{
142+
$class = $this->getNamespace().'\Entity45016';
143+
144+
$this->expectException(MappingException::class);
145+
$this->expectExceptionMessage(sprintf('Ignore on "%s::badIgnore()" cannot be added', $class));
146+
147+
$metadata = new ClassMetadata($class);
148+
$loader = $this->getLoaderForContextMapping();
149+
150+
$loader->loadClassMetadata($metadata);
151+
}
152+
140153
abstract protected function createLoader(): AnnotationLoader;
141154

142155
abstract protected function getNamespace(): string;

0 commit comments

Comments
 (0)
0