Skip to content

Commit d624202

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

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
/**
19+
* @Ignore()
20+
*/
21+
public function badIgnore(): bool
22+
{
23+
return true;
24+
}
25+
}
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\Attributes;
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/AnnotationLoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,19 @@ public function testThrowsOnContextOnInvalidMethod()
134134
$loader->loadClassMetadata($classMetadata);
135135
}
136136

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

139152
abstract protected function getNamespace(): string;

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