Skip to content

Commit db2a9de

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

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
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;
18+
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\Entity45016;
1619

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy