Skip to content

Commit

Permalink
Fix #46592 - Ignore getter with required parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
astepin committed Jul 17, 2022
1 parent f777331 commit 3a4a91e
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 3a4a91e

Please sign in to comment.