Skip to content

Commit

Permalink
bug #46958 [Serializer] Ignore getter with required parameters (Fix #…
Browse files Browse the repository at this point in the history
…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
-------

fda9281bd6 Fix #46592 - Ignore getter with required parameters
  • Loading branch information
fabpot committed Jul 19, 2022
2 parents c373ca6 + 3a4a91e commit c6e6540
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Mapping/Loader/AnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
continue;
}

$getAccessor = preg_match('/^(get|)(.+)$/i', $method->name);
if ($getAccessor && 0 !== $method->getNumberOfRequiredParameters()) {
continue; /* matches the BC behavior in `Symfony\Component\Serializer\Normalizer\ObjectNormalizer::extractAttributes` */
}

$accessorOrMutator = preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches);
if ($accessorOrMutator) {
$attributeName = lcfirst($matches[2]);
Expand Down
2 changes: 0 additions & 2 deletions Tests/Fixtures/Annotations/Entity45016.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;

use Symfony\Component\Serializer\Annotation\Ignore;
Expand Down
23 changes: 23 additions & 0 deletions Tests/Fixtures/Annotations/IgnoreDummyAdditionalGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;

use Symfony\Component\Serializer\Annotation\Ignore;

class IgnoreDummyAdditionalGetter
{

private $myValue;

/**
* @Ignore()
*/
public function getMyValue()
{
return $this->myValue;
}

public function getExtraValue(string $parameter) {
return $parameter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures\Annotations;

class IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations
{

private $myValue;

public function getMyValue()
{
return $this->myValue;
}

public function getExtraValue(string $parameter) {
return $parameter;
}
}
2 changes: 0 additions & 2 deletions Tests/Fixtures/Attributes/Entity45016.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;

use Symfony\Component\Serializer\Annotation\Ignore;
Expand Down
20 changes: 20 additions & 0 deletions Tests/Fixtures/Attributes/IgnoreDummyAdditionalGetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;

use Symfony\Component\Serializer\Annotation\Ignore;

class IgnoreDummyAdditionalGetter
{
private $myValue;

#[Ignore]
public function getIgnored2()
{
return $this->myValue;
}

public function getExtraValue(string $parameter) {
return $parameter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;

class IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations
{
private $myValue;

public function getIgnored2()
{
return $this->myValue;
}

public function getExtraValue(string $parameter) {
return $parameter;
}
}
18 changes: 18 additions & 0 deletions Tests/Mapping/Loader/AnnotationLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ public function testCanHandleUnrelatedIgnoredMethods()
$loader->loadClassMetadata($metadata);
}

public function testIgnoreGetterWirhRequiredParameterIfIgnoreAnnotationIsUsed()
{
$classMetadata = new ClassMetadata($this->getNamespace().'\IgnoreDummyAdditionalGetter');
$this->getLoaderForContextMapping()->loadClassMetadata($classMetadata);

$attributes = $classMetadata->getAttributesMetadata();
self::assertArrayNotHasKey('extraValue', $attributes);
}

public function testIgnoreGetterWirhRequiredParameterIfIgnoreAnnotationIsNotUsed()
{
$classMetadata = new ClassMetadata($this->getNamespace().'\IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations');
$this->getLoaderForContextMapping()->loadClassMetadata($classMetadata);

$attributes = $classMetadata->getAttributesMetadata();
self::assertArrayNotHasKey('extraValue', $attributes);
}

abstract protected function createLoader(): AnnotationLoader;

abstract protected function getNamespace(): string;
Expand Down

0 comments on commit c6e6540

Please sign in to comment.