From d4bf23bcfbd5ec745eefd6b745ab330ef25458a8 Mon Sep 17 00:00:00 2001 From: Yi Ping <7th.cubic.sg@gmail.com> Date: Thu, 7 Dec 2017 14:00:40 +0800 Subject: [PATCH 1/2] Fix complex groups for jms serializer --- ModelDescriber/JMSModelDescriber.php | 8 +++- Tests/Functional/Controller/JMSController.php | 14 ++++++ Tests/Functional/Entity/JMSComplex.php | 43 +++++++++++++++++++ Tests/Functional/Entity/JMSUser.php | 4 +- Tests/Functional/JMSFunctionalTest.php | 26 +++++++++++ composer.json | 3 +- 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 Tests/Functional/Entity/JMSComplex.php diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index d03c465..84c7109 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -95,8 +95,14 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn continue; } + if(empty($model->getGroups()) || !array_key_exists($name, $model->getGroups()) || !is_array($model->getGroups()[$name])) + $groups = $model->getGroups(); + else + $groups = $model->getGroups()[$name]; + + $property->setRef( - $this->modelRegistry->register(new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type), $model->getGroups())) + $this->modelRegistry->register(new Model(new Type(Type::BUILTIN_TYPE_OBJECT, false, $type), $groups)) ); } diff --git a/Tests/Functional/Controller/JMSController.php b/Tests/Functional/Controller/JMSController.php index a2452bc..f338180 100644 --- a/Tests/Functional/Controller/JMSController.php +++ b/Tests/Functional/Controller/JMSController.php @@ -13,6 +13,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Controller; use Nelmio\ApiDocBundle\Annotation\Model; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser; +use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Swagger\Annotations as SWG; @@ -29,4 +30,17 @@ class JMSController public function userAction() { } + + /** + * @Route("/api/jms_complex", methods={"GET"}) + * @SWG\Response( + * response=200, + * description="Success", + * @Model(type=JMSComplex::class, groups={"list", "details", "user" : {"list"}}) + * ) + */ + public function complexAction() + { + + } } diff --git a/Tests/Functional/Entity/JMSComplex.php b/Tests/Functional/Entity/JMSComplex.php new file mode 100644 index 0000000..3cb6006 --- /dev/null +++ b/Tests/Functional/Entity/JMSComplex.php @@ -0,0 +1,43 @@ +getModel('JMSUser')->toArray()); } + public function testModelComplexDocumentation() + { + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'id' => ['type' => 'integer'], + 'user' => ['$ref' => '#/definitions/JMSUser2'], + 'name' => ['type' => 'string'] + ], + ], $this->getModel('JMSComplex')->toArray()); + + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'id' => [ + 'type' => 'integer', + 'title' => 'userid', + 'description' => 'User id', + 'readOnly' => true, + 'example' => '1', + ], + ], + ], $this->getModel('JMSUser2')->toArray()); + + } + protected static function createKernel(array $options = array()) { return new TestKernel(true); diff --git a/composer.json b/composer.json index 354c5c1..3dd5989 100644 --- a/composer.json +++ b/composer.json @@ -37,13 +37,14 @@ "symfony/browser-kit": "^2.8|^3.0|^4.0", "symfony/cache": "^3.1|^4.0", "symfony/phpunit-bridge": "^3.3", + "symfony/stopwatch": "^2.8|^3.0|^4.0", "sensio/framework-extra-bundle": "^3.0", "doctrine/annotations": "^1.2", "phpdocumentor/reflection-docblock": "^3.1", "api-platform/core": "^2.0.3", "friendsofsymfony/rest-bundle": "^2.0", - "jms/serializer-bundle": "^1.0|^2.0" + "jms/serializer-bundle": "^2.0" }, "suggest": { "phpdocumentor/reflection-docblock": "For parsing php docs.", From 3b81fa4f2feb5d62c34d70dacd8a2f729c5f7cfc Mon Sep 17 00:00:00 2001 From: Yi Ping <7th.cubic.sg@gmail.com> Date: Mon, 18 Dec 2017 08:06:19 +0800 Subject: [PATCH 2/2] changes --- ModelDescriber/JMSModelDescriber.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ModelDescriber/JMSModelDescriber.php b/ModelDescriber/JMSModelDescriber.php index 84c7109..8c90739 100644 --- a/ModelDescriber/JMSModelDescriber.php +++ b/ModelDescriber/JMSModelDescriber.php @@ -95,10 +95,11 @@ class JMSModelDescriber implements ModelDescriberInterface, ModelRegistryAwareIn continue; } - if(empty($model->getGroups()) || !array_key_exists($name, $model->getGroups()) || !is_array($model->getGroups()[$name])) + if(!array_key_exists($name, $model->getGroups()) || !is_array($model->getGroups()[$name])) { $groups = $model->getGroups(); - else + } else { $groups = $model->getGroups()[$name]; + } $property->setRef(