diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php index 3faf28f6341ab..c96f2946a6f9f 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php @@ -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]); diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php index 4e189a13ca68b..a896d9b766c59 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php @@ -1,7 +1,5 @@ myValue; + } + + public function getExtraValue(string $parameter) { + return $parameter; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations.php new file mode 100644 index 0000000000000..cb711462b58c7 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations.php @@ -0,0 +1,18 @@ +myValue; + } + + public function getExtraValue(string $parameter) { + return $parameter; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php index 0eb99474ba315..5a7ace0fd5563 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php @@ -1,7 +1,5 @@ myValue; + } + + public function getExtraValue(string $parameter) { + return $parameter; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations.php new file mode 100644 index 0000000000000..150634da44f47 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/IgnoreDummyAdditionalGetterWithoutIgnoreAnnotations.php @@ -0,0 +1,17 @@ +myValue; + } + + public function getExtraValue(string $parameter) { + return $parameter; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php index 5d3f3af617089..f2f5325c0e264 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php @@ -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;