8000 [Serializer] GetSetMethodNormalizer::supportss should not check ignored methods by nikophil · Pull Request #49720 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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 @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\Annotation\Ignore;

/**
* Converts between objects with getter and setter methods and arrays.
*
Expand Down Expand Up @@ -81,17 +83,12 @@ private function supports(string $class): bool
*/
private function isGetMethod(\ReflectionMethod $method): bool
{
$methodLength = \strlen($method->name);

return
!$method->isStatic() &&
(
((str_starts_with($method->name, 'get') && 3 < $methodLength) ||
(str_starts_with($method->name, 'is') && 2 < $methodLength) ||
(str_starts_with($method->name, 'has') && 3 < $methodLength)) &&
0 === $method->getNumberOfRequiredParameters()
)
;
return !$method->isStatic()
&& (\PHP_VERSION_ID < 80000 || !$method->getAttributes(Ignore::class))
&& !$method->getNumberOfRequiredParameters()
&& ((2 < ($methodLength = \strlen($method->name)) && str_starts_with($method->name, 'is'))
|| (3 < $methodLength && (str_starts_with($method->name, 'has') || str_starts_with($method->name, 'get')))
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Annotation\Ignore;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
Expand Down Expand Up @@ -430,6 +431,11 @@ public function testNoStaticGetSetSupport()
$this->assertFalse($this->normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy()));
}

public function testNotIgnoredMethodSupport()
{
$this->assertFalse($this->normalizer->supportsNormalization(new ClassWithIgnoreAttribute()));
}

public function testPrivateSetter()
{
$obj = $this->normalizer->denormalize(['foo' => 'foobar'], ObjectWithPrivateSetterDummy::class);
Expand Down Expand Up @@ -753,3 +759,14 @@ public function __call($key, $value)
throw new \RuntimeException('__call should not be called. Called with: '.$key);
}
}

class ClassWithIgnoreAttribute
{
public string $foo;

#[Ignore]
public function isSomeIgnoredMethod(): bool
{
return true;
}
}
0