From edb10386dd505679cd1cfb298429874b49a7b98b Mon Sep 17 00:00:00 2001 From: Artem Stepin Date: Fri, 15 Jul 2022 09:14:59 +0200 Subject: [PATCH] Prevent that bad Ignore method annotations lead to incorrect results fix https://github.com/symfony/symfony/issues/45016 --- .../Mapping/Loader/AnnotationLoader.php | 4 +++ .../Fixtures/Annotations/Entity45016.php | 28 +++++++++++++++++++ .../Tests/Fixtures/Attributes/Entity45016.php | 26 +++++++++++++++++ .../Mapping/Loader/AnnotationLoaderTest.php | 13 +++++++++ 4 files changed, 71 insertions(+) create mode 100644 src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php create mode 100644 src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php index ebe8eefae7881..3faf28f6341ab 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php @@ -134,6 +134,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) $attributeMetadata->setSerializedName($annotation->getSerializedName()); } elseif ($annotation instanceof Ignore) { + if (!$accessorOrMutator) { + throw new MappingException(sprintf('Ignore on "%s::%s()" cannot be added. Ignore can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name)); + } + $attributeMetadata->setIgnore(true); } elseif ($annotation instanceof Context) { if (!$accessorOrMutator) { diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php new file mode 100644 index 0000000000000..4e189a13ca68b --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Annotations/Entity45016.php @@ -0,0 +1,28 @@ +id; + } + + /** + * @Ignore() + */ + public function badIgnore(): bool + { + return true; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php new file mode 100644 index 0000000000000..0eb99474ba315 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Attributes/Entity45016.php @@ -0,0 +1,26 @@ +id; + } + + #[Ignore] + public function badIgnore(): bool + { + return true; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php index a135bfdaab16f..5d3f3af617089 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php @@ -137,6 +137,19 @@ public function testThrowsOnContextOnInvalidMethod() $loader->loadClassMetadata($classMetadata); } + public function testCanHandleUnrelatedIgnoredMethods() + { + $class = $this->getNamespace().'\Entity45016'; + + $this->expectException(MappingException::class); + $this->expectExceptionMessage(sprintf('Ignore on "%s::badIgnore()" cannot be added', $class)); + + $metadata = new ClassMetadata($class); + $loader = $this->getLoaderForContextMapping(); + + $loader->loadClassMetadata($metadata); + } + abstract protected function createLoader(): AnnotationLoader; abstract protected function getNamespace(): string; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy