From 429d809f41630da13d30cee2c4aca5ebb5dedf2d Mon Sep 17 00:00:00 2001 From: Paul Dugas Date: Wed, 9 Sep 2020 02:27:47 -0400 Subject: [PATCH] Ignore ReflectionException for "magic" JMS\Accessor methods (#1715) * Update JMSModelDescriber.php Ignore ReflectionException thrown when getter or setter from JMS\ Accessor are "magic" methods. * Add tests to avoid future regressions * CS Co-authored-by: Guilhem Niot --- ModelDescriber/JMSModelDescriber.php | 13 ++++++++----- Tests/Functional/Entity/VirtualProperty.php | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index 0711be2..18e5382 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -93,11 +93,14 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn $reflections[] = new \ReflectionProperty($item->class, $item->name); } - if (null !== $item->getter) { - $reflections[] = new \ReflectionMethod($item->class, $item->getter); - } - if (null !== $item->setter) { - $reflections[] = new \ReflectionMethod($item->class, $item->setter); + try { + if (null !== $item->getter) { + $reflections[] = new \ReflectionMethod($item->class, $item->getter); + } + if (null !== $item->setter) { + $reflections[] = new \ReflectionMethod($item->class, $item->setter); + } + } catch (\ReflectionExceptions $ignored) { } $groups = $this->computeGroups($context, $item->type); diff --git a/Tests/Functional/Entity/VirtualProperty.php b/Tests/Functional/Entity/VirtualProperty.php index 3db68a1..e996256 100644 --- a/Tests/Functional/Entity/VirtualProperty.php +++ b/Tests/Functional/Entity/VirtualProperty.php @@ -37,9 +37,26 @@ class VirtualProperty */ private $user; + /** + * @Serializer\Accessor(getter="getFoo", setter="setFoo") + * @Serializer\Type("string") + * + * Ensures https://github.com/nelmio/NelmioApiDocBundle/issues/1708 is fixed. + */ + private $virtualprop; + public function __construct() { $this->user = new User(); $this->user->setEmail('dummy@test.com'); } + + public function __call(string $name, array $arguments) + { + if ('getFoo' === $name || 'setFoo' === $name) { + return 'Success'; + } + + throw new \LogicException(sprintf('%s::__call does not implement this function.', __CLASS__)); + } }