From 829617746a91be4b4e7eb7d2d9c3e31a38038e1b Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Fri, 17 Mar 2023 14:46:09 +0100 Subject: [PATCH] fix: GetSetMethodNormalizer::supportss should not check ignored methods --- .../Normalizer/GetSetMethodNormalizer.php | 19 ++++++++----------- .../Normalizer/GetSetMethodNormalizerTest.php | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php index b67a808cb9abd..d9339df64df5c 100644 --- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php @@ -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. * @@ -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'))) + ); } /** diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php index e6f8396fe9d15..bf0c7cd56f14f 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php @@ -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; @@ -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); @@ -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; + } +}