From d15b4123c1529542c00ac3f21909bd6f14703485 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Fri, 11 Jan 2019 10:40:53 +0100 Subject: [PATCH] add hateoas v3 compat --- ModelDescriber/JMSModelDescriber.php | 17 +++++++++++++---- Tests/Functional/Entity/BazingaUser.php | 2 +- composer.json | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index 518023d..3a6449d 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -13,6 +13,7 @@ namespace Nelmio\ApiDocBundle\ModelDescriber; use Doctrine\Common\Annotations\Reader; use EXSyst\Component\Swagger\Schema; +use JMS\Serializer\Annotation\VirtualProperty; use JMS\Serializer\Exclusion\GroupsExclusionStrategy; use JMS\Serializer\Naming\PropertyNamingStrategyInterface; use JMS\Serializer\SerializationContext; @@ -94,10 +95,18 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn $name = $this->namingStrategy ? $this->namingStrategy->translateName($item) : $item->serializedName; // read property options from Swagger Property annotation if it exists - if (null !== $item->reflection) { - $property = $properties->get($annotationsReader->getPropertyName($item->reflection, $name)); - $annotationsReader->updateProperty($item->reflection, $property, $groups); - } else { + try { + if (property_exists($item, 'reflection') && null !== $item->reflection) { + $reflection = $item->reflection; + } elseif ($item instanceof VirtualProperty) { + $reflection = new \ReflectionProperty($item->class, $item->name); + } else { + $reflection = new \ReflectionProperty($item->class, $item->name); + } + + $property = $properties->get($annotationsReader->getPropertyName($reflection, $name)); + $annotationsReader->updateProperty($reflection, $property, $groups); + } catch (\ReflectionException $e) { $property = $properties->get($name); } diff --git a/Tests/Functional/Entity/BazingaUser.php b/Tests/Functional/Entity/BazingaUser.php index 128453a..0eb4a1c 100644 --- a/Tests/Functional/Entity/BazingaUser.php +++ b/Tests/Functional/Entity/BazingaUser.php @@ -18,7 +18,7 @@ use Hateoas\Configuration\Annotation as Hateoas; * * @Hateoas\Relation(name="example", attributes={"str_att":"bar", "float_att":5.6, "bool_att": false}, href="http://www.example.com") * @Hateoas\Relation(name="route", href=@Hateoas\Route("foo")) - * @Hateoas\Relation(name="route", attributes={"foo":"bar"}, embedded=@Hateoas\Embedded("expr(foo)")) + * @Hateoas\Relation(name="route", attributes={"foo":"bar"}, embedded=@Hateoas\Embedded("expr(service('xx'))")) */ class BazingaUser { diff --git a/composer.json b/composer.json index fa0d364..16d96af 100644 --- a/composer.json +++ b/composer.json @@ -43,7 +43,7 @@ "api-platform/core": "^2.1.0", "friendsofsymfony/rest-bundle": "^2.0", - "willdurand/hateoas-bundle": "^1.0", + "willdurand/hateoas-bundle": "^1.0|^2.0", "jms/serializer-bundle": "^2.0|^3.0" }, "suggest": {