Skip to content

Commit ccb2d0e

Browse files
committed
Fix #46592 - Ignore getter with required parameters
1 parent 8dd6bb7 commit ccb2d0e

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
100100
continue;
101101
}
102102

103+
$getAccessor = preg_match('/^(get|)(.+)$/i', $method->name);
104+
if ($getAccessor && $method->getNumberOfRequiredParameters() !== 0) {
105+
continue; /* matches the BC behavior in `Symfony\Component\Serializer\Normalizer\ObjectNormalizer::extractAttributes` */
106+
}
107+
103108
$accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
104109
if ($accessorOrMutator) {
105110
$attributeName = lcfirst($matches[2]);
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 IgnoreDummyAdditionalGetter
10+
{
11+
12+
private $myValue;
13+
14+
/**
15+
* @Ignore()
16+
*/
17+
public function getMyValue()
18+
{
19+
return $this->myValue;
20+
}
21+
22+
public function getExtraValue(string $parameter) {
23+
return $parameter;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;
6+
7+
class IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations
8+
{
9+
10+
private $myValue;
11+
12+
public function getMyValue()
13+
{
14+
return $this->myValue;
15+
}
16+
17+
public function getExtraValue(string $parameter) {
18+
return $parameter;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;
4+
5+
use Symfony\Component\Serializer\Annotation\Ignore;
6+
7+
class IgnoreDummyAdditionalGetter
8+
{
9+
private $myValue;
10+
11+
#[Ignore]
12+
public function getIgnored2()
13+
{
14+
return $this->myValue;
15+
}
16+
17+
public function getExtraValue(string $parameter) {
18+
return $parameter;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;
4+
5+
class IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations
6+
{
7+
private $myValue;
8+
9+
public function getIgnored2()
10+
{
11+
return $this->myValue;
12+
}
13+
14+
public function getExtraValue(string $parameter) {
15+
return $parameter;
16+
}
17+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ public function testCanHandleUnrelatedIgnoredMethods()
150150
$loader->loadClassMetadata($metadata);
151151
}
152152

153+
public function testIgnoreGetterWirhRequiredParameterIfIgnoreAnnotationIsUsed()
154+
{
155+
$classMetadata = new ClassMetadata($this->getNamespace().'\IgnoreDummyAdditionalGetter');
156+
$this->getLoaderForContextMapping()->loadClassMetadata($classMetadata);
157+
158+
$attributes = $classMetadata->getAttributesMetadata();
159+
self::assertArrayNotHasKey('extraValue', $attributes);
160+
}
161+
162+
public function testIgnoreGetterWirhRequiredParameterIfIgnoreAnnotationIsNotUsed()
163+
{
164+
$classMetadata = new ClassMetadata($this->getNamespace().'\IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations');
165+
$this->getLoaderForContextMapping()->loadClassMetadata($classMetadata);
166+
167+
$attributes = $classMetadata->getAttributesMetadata();
168+
self::assertArrayNotHasKey('extraValue', $attributes);
169+
}
170+
153171
abstract protected function createLoader(): AnnotationLoader;
154172

155173
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