Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c89721e

Browse filesBrowse files
committed
bug #46958 [Serializer] Ignore getter with required parameters (Fix #46592) (astepin)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Ignore getter with required parameters (Fix #46592) | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #46592 | License | MIT | Doc PR | na If no Ignore annotation is used, the attributes for serialization are obtained using `Symfony\Component\Serializer\Normalizer\ObjectNormalizer::extractAttributes`. There it is checked if the method has required parameters and if yes, the method is ignored. However, if you use the Ignore annotation, the attributes are determined with a different method. Here I have adapted at least for get methods the behavior as it was before. If someone serialized a class with Ignore annotations before, he got here `\Symfony\Component\PropertyAccess\PropertyAccessor::readProperty` an exception as written in ticket #46592. With this fix the methods are ignored and there is no exception anymore. Commits ------- fda9281 Fix #46592 - Ignore getter with required parameters
2 parents 14d5083 + fda9281 commit c89721e
Copy full SHA for c89721e

8 files changed

+101
-4
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php
+5Lines 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 && 0 !== $method->getNumberOfRequiredParameters()) {
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]);

‎src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;
64

75
use Symfony\Component\Serializer\Annotation\Ignore;
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;
4+
5+
use Symfony\Component\Serializer\Annotation\Ignore;
6+
7+
class IgnoreDummyAdditionalGetter
8+
{
9+
10+
private $myValue;
11+
12+
/**
13+
* @Ignore()
14+
*/
15+
public function getMyValue()
16+
{
17+
return $this->myValue;
18+
}
19+
20+
public function getExtraValue(string $parameter) {
21+
return $parameter;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;
4+
5+
class IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations
6+
{
7+
8+
private $myValue;
9+
10+
public function getMyValue()
11+
{
12+
return $this->myValue;
13+
}
14+
15+
public function getExtraValue(string $parameter) {
16+
return $parameter;
17+
}
18+
}

‎src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;
64

75
use Symfony\Component\Serializer\Annotation\Ignore;
+20Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php
+18Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.