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

Skip to content

Commit 7dccccd

Browse files
bug #49720 [Serializer] GetSetMethodNormalizer::supportss should not check ignored methods (nikophil)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] GetSetMethodNormalizer::supportss should not check ignored methods | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT `GetSetMethodNormalizer` support methods do not check if potentiel getters have `#[Ignore]` attribute Commits ------- 8296177 fix: GetSetMethodNormalizer::supportss should not check ignored methods
2 parents 6ab60c4 + 8296177 commit 7dccccd

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\Serializer\Annotation\Ignore;
15+
1416
/**
1517
* Converts between objects with getter and setter methods and arrays.
1618
*
@@ -81,17 +83,12 @@ private function supports(string $class): bool
8183
*/
8284
private function isGetMethod(\ReflectionMethod $method): bool
8385
{
84-
$methodLength = \strlen($method->name);
85-
86-
return
87-
!$method->isStatic() &&
88-
(
89-
((str_starts_with($method->name, 'get') && 3 < $methodLength) ||
90-
(str_starts_with($method->name, 'is') && 2 < $methodLength) ||
91-
(str_starts_with($method->name, 'has') && 3 < $methodLength)) &&
92-
0 === $method->getNumberOfRequiredParameters()
93-
)
94-
;
86+
return !$method->isStatic()
87+
&& (\PHP_VERSION_ID < 80000 || !$method->getAttributes(Ignore::class))
88+
&& !$method->getNumberOfRequiredParameters()
89+
&& ((2 < ($methodLength = \strlen($method->name)) && str_starts_with($method->name, 'is'))
90+
|| (3 < $methodLength && (str_starts_with($method->name, 'has') || str_starts_with($method->name, 'get')))
91+
);
9592
}
9693

9794
/**

src/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1717
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1818
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
19+
use Symfony\Component\Serializer\Annotation\Ignore;
1920
use Symfony\Component\Serializer\Exception\LogicException;
2021
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2122
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
@@ -430,6 +431,11 @@ public function testNoStaticGetSetSupport()
430431
$this->assertFalse($this->normalizer->supportsNormalization(new ObjectWithJustStaticSetterDummy()));
431432
}
432433

434+
public function testNotIgnoredMethodSupport()
435+
{
436+
$this->assertFalse($this->normalizer->supportsNormalization(new ClassWithIgnoreAttribute()));
437+
}
438+
433439
public function testPrivateSetter()
434440
{
435441
$obj = $this->normalizer->denormalize(['foo' => 'foobar'], ObjectWithPrivateSetterDummy::class);
@@ -753,3 +759,14 @@ public function __call($key, $value)
753759
throw new \RuntimeException('__call should not be called. Called with: '.$key);
754760
}
755761
}
762+
763+
class ClassWithIgnoreAttribute
764+
{
765+
public string $foo;
766+
767+
#[Ignore]
768+
public function isSomeIgnoredMethod(): bool
769+
{
770+
return true;
771+
}
772+
}

0 commit comments

Comments
 (0)