From 0fb5d7afa24f576c5f16a0a67f50578258a8402f Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sat, 26 Jan 2019 14:52:13 +0100 Subject: [PATCH] jms 2.0 groups tested --- DependencyInjection/NelmioApiDocExtension.php | 1 - ModelDescriber/JMSModelDescriber.php | 12 ++-- Tests/Functional/Controller/JMSController.php | 13 +++++ .../Entity/NestedGroup/JMSChatFriend.php | 42 ++++++++++++++ .../Entity/NestedGroup/JMSChatLivingRoom.php | 28 ++++++++++ .../Entity/NestedGroup/JMSChatRoom.php | 42 ++++++++++++++ .../Entity/NestedGroup/JMSChatRoomUser.php | 35 ++++++++++++ Tests/Functional/JMSFunctionalTest.php | 55 +++++++++++++++++++ 8 files changed, 223 insertions(+), 5 deletions(-) create mode 100644 Tests/Functional/Entity/NestedGroup/JMSChatFriend.php create mode 100644 Tests/Functional/Entity/NestedGroup/JMSChatLivingRoom.php create mode 100644 Tests/Functional/Entity/NestedGroup/JMSChatRoom.php create mode 100644 Tests/Functional/Entity/NestedGroup/JMSChatRoomUser.php diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 7ddd0ce..e109a39 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -23,7 +23,6 @@ use Nelmio\ApiDocBundle\Routing\FilteredRouteCollectionBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index 3a6449d..f092a85 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -68,6 +68,8 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn $annotationsReader = new AnnotationsReader($this->doctrineReader, $this->modelRegistry); $annotationsReader->updateDefinition(new \ReflectionClass($className), $schema); + $isJmsV1 = null !== $this->namingStrategy; + $properties = $schema->getProperties(); foreach ($metadata->propertyMetadata as $item) { // filter groups @@ -84,8 +86,10 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn } elseif (!isset($groups[$item->name]) && !empty($this->previousGroups[$model->getHash()])) { $groups = false === $this->propertyTypeUsesGroups($item->type) ? null - : ($this->namingStrategy ? [GroupsExclusionStrategy::DEFAULT_GROUP] : $this->previousGroups[$model->getHash()]); - } elseif (is_array($groups)) { + : ($isJmsV1 ? [GroupsExclusionStrategy::DEFAULT_GROUP] : $this->previousGroups[$model->getHash()]); + } + + if (is_array($groups)) { $groups = array_filter($groups, 'is_scalar'); } @@ -93,10 +97,10 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn $groups = null; } - $name = $this->namingStrategy ? $this->namingStrategy->translateName($item) : $item->serializedName; + $name = true === $isJmsV1 ? $this->namingStrategy->translateName($item) : $item->serializedName; // read property options from Swagger Property annotation if it exists try { - if (property_exists($item, 'reflection') && null !== $item->reflection) { + if (true === $isJmsV1 && property_exists($item, 'reflection') && null !== $item->reflection) { $reflection = $item->reflection; } elseif ($item instanceof VirtualProperty) { $reflection = new \ReflectionProperty($item->class, $item->name); diff --git a/Tests/Functional/Controller/JMSController.php b/Tests/Functional/Controller/JMSController.php index c932c57..47742ef 100644 --- a/Tests/Functional/Controller/JMSController.php +++ b/Tests/Functional/Controller/JMSController.php @@ -17,6 +17,7 @@ use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSDualComplex; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSNamingStrategyConstraints; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser; use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChat; +use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatRoomUser; use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatUser; use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSPicture; use Nelmio\ApiDocBundle\Tests\Functional\Entity\VirtualProperty; @@ -123,4 +124,16 @@ class JMSController public function minUserAction() { } + + /** + * @Route("/api/jms_mini_user_nested", methods={"GET"}) + * @SWG\Response( + * response=200, + * description="Success", + * @Model(type=JMSChatRoomUser::class, groups={"mini", "friend": {"living":{"Default"}}}) + * ) + */ + public function minUserNestedAction() + { + } } diff --git a/Tests/Functional/Entity/NestedGroup/JMSChatFriend.php b/Tests/Functional/Entity/NestedGroup/JMSChatFriend.php new file mode 100644 index 0000000..00792b8 --- /dev/null +++ b/Tests/Functional/Entity/NestedGroup/JMSChatFriend.php @@ -0,0 +1,42 @@ +getModel('JMSDualComplex')->toArray()); } + public function testNestedGroupsV1() + { + if (interface_exists(SerializationVisitorInterface::class)){ + $this->markTestSkipped('This applies only for jms/serializer v1.x'); + } + + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'living' => ['$ref' => '#/definitions/JMSChatLivingRoom'], + 'dining' => ['$ref' => '#/definitions/JMSChatRoom'], + ], + ], $this->getModel('JMSChatFriend')->toArray()); + + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'id1' => ['type' => 'integer'], + 'id2' => ['type' => 'integer'], + 'id3' => ['type' => 'integer'], + ], + ], $this->getModel('JMSChatRoom')->toArray()); + } + + public function testNestedGroupsV2() + { + if (!interface_exists(SerializationVisitorInterface::class)){ + $this->markTestSkipped('This applies only for jms/serializer v2.x'); + } + + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'living' => ['$ref' => '#/definitions/JMSChatLivingRoom'], + 'dining' => ['$ref' => '#/definitions/JMSChatRoom'], + ], + ], $this->getModel('JMSChatFriend')->toArray()); + + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'id2' => ['type' => 'integer'], + ], + ], $this->getModel('JMSChatRoom')->toArray()); + + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'id' => ['type' => 'integer'], + ], + ], $this->getModel('JMSChatLivingRoom')->toArray()); + } + public function testModelComplexDocumentation() { $this->assertEquals([