Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Ignore getter with required parameters (Fix #46592) #46958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex looks "fishy": ignoring capturing groups (as matches are not used), it's equivalent to /^(get)?.+$/i i.e. just /^.+$/ i.e. will match any method (not only getters)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've filed #47255 to fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that. A copy paste error. Thanks @jsor for fixing

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
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
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;
}
}
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
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;
}
}
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