From 523d28e9554ea763c1524ae285a9ad41c4f19deb Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Sat, 18 Jul 2020 13:31:41 +0200 Subject: [PATCH] Add annotations support at methods level when using the JMS serializer (#1682) --- ModelDescriber/JMSModelDescriber.php | 49 ++++++++++++++++---------- Tests/Functional/Entity/JMSComplex.php | 10 ++++++ Tests/Functional/JMSFunctionalTest.php | 1 + 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index ec661e8..86282fa 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -85,31 +85,42 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn $context->pushPropertyMetadata($item); $name = true === $isJmsV1 ? $this->namingStrategy->translateName($item) : $item->serializedName; - // read property options from Swagger Property annotation if it exists - try { - if (true === $isJmsV1 && property_exists($item, 'reflection') && null !== $item->reflection) { - $reflection = $item->reflection; - } else { - $reflection = new \ReflectionProperty($item->class, $item->name); + + $reflections = []; + if (true === $isJmsV1 && property_exists($item, 'reflection') && null !== $item->reflection) { + $reflections[] = $item->reflection; + } elseif (\property_exists($item->class, $item->name)) { + $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); + } + + $groups = $this->computeGroups($context, $item->type); + + if (true === $item->inline && isset($item->type['name'])) { + // currently array types can not be documented :-/ + if (!in_array($item->type['name'], ['array', 'ArrayCollection'], true)) { + $inlineModel = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $item->type['name']), $groups); + $this->describe($inlineModel, $schema); } + $context->popPropertyMetadata(); - $groups = $this->computeGroups($context, $item->type); + continue; + } - if (true === $item->inline && isset($item->type['name'])) { - // currently array types can not be documented :-/ - if (!in_array($item->type['name'], ['array', 'ArrayCollection'], true)) { - $inlineModel = new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $item->type['name']), $groups); - $this->describe($inlineModel, $schema); - } - $context->popPropertyMetadata(); + foreach ($reflections as $reflection) { + $name = $annotationsReader->getPropertyName($reflection, $name); + } - continue; - } + $property = $properties->get($name); - $property = $properties->get($annotationsReader->getPropertyName($reflection, $name)); + foreach ($reflections as $reflection) { $annotationsReader->updateProperty($reflection, $property, $groups); - } catch (\ReflectionException $e) { - $property = $properties->get($name); } if (null !== $property->getType() || null !== $property->getRef()) { diff --git a/Tests/Functional/Entity/JMSComplex.php b/Tests/Functional/Entity/JMSComplex.php index 66f7e62..5d55df2 100644 --- a/Tests/Functional/Entity/JMSComplex.php +++ b/Tests/Functional/Entity/JMSComplex.php @@ -45,4 +45,14 @@ class JMSComplex * @Serializer\Groups({"list"}) */ private $name; + + /** + * @Serializer\VirtualProperty + * @Serializer\Expose + * @Serializer\Groups({"list"}) + * @SWG\Property(ref=@Model(type=JMSUser::class)) + */ + public function getVirtualFriend() + { + } } diff --git a/Tests/Functional/JMSFunctionalTest.php b/Tests/Functional/JMSFunctionalTest.php index 0fa81e5..a9d581c 100644 --- a/Tests/Functional/JMSFunctionalTest.php +++ b/Tests/Functional/JMSFunctionalTest.php @@ -260,6 +260,7 @@ class JMSFunctionalTest extends WebTestCase 'user' => ['$ref' => '#/definitions/JMSUser'], 'name' => ['type' => 'string'], 'virtual' => ['$ref' => '#/definitions/JMSUser'], + 'virtual_friend' => ['$ref' => '#/definitions/JMSUser'], ], 'required' => [ 'id',