10000 Prevent that bad Ignore method annotations lead to incorrect results · symfony/symfony@6c6333f · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c6333f

Browse files
committed
Prevent that bad Ignore method annotations lead to incorrect results
fix #45016
1 parent 51f68b1 commit 6c6333f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-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): bool
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
public function __construct(private int $id = 1234) {}
12+
13+
public function getId(): int
14+
{
15+
return $this->id;
16+
}
17+
18+
#[Ignore]
19+
public function badIgnore(): bool
20+
{
21+
return true;
22+
}
23+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Serializer\Tests\Mapping\Loader;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15+
use Symfony\Component\Serializer\Exception\MappingException;
16+
use Symfony\Component\Serializer\Mapping\ClassMetadata;
1517
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1618

1719
class AnnotationLoaderWithDoctrineAnnotationsTest extends AnnotationLoaderTest
@@ -25,4 +27,15 @@ protected function getNamespace(): string
2527
{
2628
return 'Symfony\Component\Serializer\Tests\Fixtures\Annotations';
2729
}
30+
31+
public function testCanHandleUnrelatedIgnoredMethods()
32+
{
33+
$class = $this->getNamespace().'\Entity45016';
34+
35+
$this->expectException(MappingException::class);
36+
$this->expectExceptionMessage(sprintf('Ignore on "%s::badIgnore()" cannot be added', $class));
37+
38+
$metadata = new ClassMetadata($class);
39+
$this->loader->loadClassMetadata($metadata);
40+
}
2841
}

0 commit comments

Comments
 (0)
0