From de4066034174231998733019b0d2d6acd4d54793 Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Tue, 13 Jun 2017 13:34:26 +0200 Subject: [PATCH 1/3] Add groups support --- Annotation/Model.php | 14 +++++++++ Model/Model.php | 19 ++++++++++-- ModelDescriber/CollectionModelDescriber.php | 2 +- ModelDescriber/ObjectModelDescriber.php | 9 ++++-- SwaggerPhp/ModelRegister.php | 3 +- Tests/Functional/Controller/ApiController.php | 13 ++++++++ Tests/Functional/Entity/Article.php | 31 +++++++++++++++++++ Tests/Functional/FunctionalTest.php | 15 +++++++++ 8 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 Tests/Functional/Entity/Article.php diff --git a/Annotation/Model.php b/Annotation/Model.php index 06d3b63..2d3c04a 100644 --- a/Annotation/Model.php +++ b/Annotation/Model.php @@ -18,11 +18,25 @@ use Swagger\Annotations\AbstractAnnotation; */ final class Model extends AbstractAnnotation { + + /** @inheritdoc */ + public static $_types = [ + 'type' => 'string', + 'groups' => '[string]', + ]; public static $_required = ['type']; public static $_parents = [ 'Swagger\Annotations\Parameter', 'Swagger\Annotations\Response', ]; + /** + * @var string + */ public $type; + + /** + * @var string[] + */ + public $groups; } diff --git a/Model/Model.php b/Model/Model.php index 1ffb7e1..43fffb6 100644 --- a/Model/Model.php +++ b/Model/Model.php @@ -16,22 +16,35 @@ use Symfony\Component\PropertyInfo\Type; final class Model { private $type; + private $groups; - public function __construct(Type $type) + /** + * @param string[]|null $groups + */ + public function __construct(Type $type, array $groups = null) { $this->type = $type; + $this->groups = $groups; } /** - * @return Type|null + * @return Type */ public function getType() { return $this->type; } + /** + * @return string[]|null + */ + public function getGroups() + { + return $this->groups; + } + public function getHash(): string { - return md5(serialize($this->type)); + return md5(serialize([$this->type, $this->groups])); } } diff --git a/ModelDescriber/CollectionModelDescriber.php b/ModelDescriber/CollectionModelDescriber.php index 304076a..d238b26 100644 --- a/ModelDescriber/CollectionModelDescriber.php +++ b/ModelDescriber/CollectionModelDescriber.php @@ -24,7 +24,7 @@ class CollectionModelDescriber implements ModelDescriberInterface, ModelRegistry { $schema->setType('array'); $schema->getItems()->setRef( - $this->modelRegistry->register(new Model($model->getType()->getCollectionValueType())) + $this->modelRegistry->register(new Model($model->getType()->getCollectionValueType(), $model->getGroups())) ); } diff --git a/ModelDescriber/ObjectModelDescriber.php b/ModelDescriber/ObjectModelDescriber.php index 924d5ac..469cfc5 100644 --- a/ModelDescriber/ObjectModelDescriber.php +++ b/ModelDescriber/ObjectModelDescriber.php @@ -35,7 +35,12 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar $properties = $schema->getProperties(); $class = $model->getType()->getClassName(); - $propertyInfoProperties = $this->propertyInfo->getProperties($class); + $context = []; + if (null !== $model->getGroups()) { + $context = ['serializer_groups' => $model->getGroups()]; + } + + $propertyInfoProperties = $this->propertyInfo->getProperties($class, $context); if (null === $propertyInfoProperties) { return; } @@ -50,7 +55,7 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar } $properties->get($propertyName)->setRef( - $this->modelRegistry->register(new Model($types[0])) + $this->modelRegistry->register(new Model($types[0], $model->getGroups())) ); } } diff --git a/SwaggerPhp/ModelRegister.php b/SwaggerPhp/ModelRegister.php index 5fe105e..e234dd5 100644 --- a/SwaggerPhp/ModelRegister.php +++ b/SwaggerPhp/ModelRegister.php @@ -70,8 +70,9 @@ final class ModelRegister continue; } + $annotation->merge([new $annotationClass([ - 'ref' => $this->modelRegistry->register(new Model($this->createType($model->type))), + 'ref' => $this->modelRegistry->register(new Model($this->createType($model->type), $model->groups)), ])]); // It is no longer an unmerged annotation diff --git a/Tests/Functional/Controller/ApiController.php b/Tests/Functional/Controller/ApiController.php index 32dce72..648f588 100644 --- a/Tests/Functional/Controller/ApiController.php +++ b/Tests/Functional/Controller/ApiController.php @@ -16,6 +16,7 @@ use FOS\RestBundle\Controller\Annotations\RequestParam; use Nelmio\ApiDocBundle\Annotation\Model; use Nelmio\ApiDocBundle\Annotation\Operation; use Nelmio\ApiDocBundle\Tests\Functional\Entity\User; +use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Swagger\Annotations as SWG; @@ -24,6 +25,18 @@ use Swagger\Annotations as SWG; */ class ApiController { + /** + * @SWG\Response( + * response="200", + * description="Success", + * @Model(type=Article::class, groups={"light"}) + * ) + * @Route("/article/{id}", methods={"GET"}) + */ + public function fetchArticleAction() + { + } + /** * @Route("/swagger", methods={"GET"}) * @Route("/swagger2", methods={"GET"}) diff --git a/Tests/Functional/Entity/Article.php b/Tests/Functional/Entity/Article.php new file mode 100644 index 0000000..5d295e4 --- /dev/null +++ b/Tests/Functional/Entity/Article.php @@ -0,0 +1,31 @@ + + */ +class Article +{ + /** + * @Groups("light") + */ + public function setAuthor(User $author) + { + } + + public function setContent(string $content) + { + } +} diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index 92366ad..f9039c0 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -30,6 +30,21 @@ class FunctionalTest extends WebTestCase $this->assertFalse($paths->has('/api/admin')); } + public function testFetchArticleAction() + { + $operation = $this->getOperation('/api/article/{id}', 'get'); + + $responses = $operation->getResponses(); + $this->assertTrue($responses->has('200')); + $this->assertEquals('#/definitions/Article', $responses->get('200')->getSchema()->getRef()); + + // Ensure that groups are supported + $modelProperties = $this->getModel('Article')->getProperties(); + $this->assertCount(1, $modelProperties); + $this->assertTrue($modelProperties->has('author')); + $this->assertFalse($modelProperties->has('content')); + } + public function testFilteredAction() { $paths = $this->getSwaggerDefinition()->getPaths(); From 7a7b7c1030e828c4bb077d750660aefb2ded5424 Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Wed, 14 Jun 2017 13:27:07 +0200 Subject: [PATCH 2/3] Bump dependencies --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index b988770..01ead18 100644 --- a/composer.json +++ b/composer.json @@ -16,13 +16,13 @@ ], "require": { "php": "~7.0|~7.1", - "symfony/framework-bundle": "^2.8.18|^3.0|^4.0", - "symfony/property-info": "^2.8|^3.0|^4.0", + "symfony/framework-bundle": "^3.1|^4.0", + "symfony/property-info": "^3.1|^4.0", "exsyst/swagger": "~0.2.3", "zircote/swagger-php": "^2.0.9" }, "require-dev": { - "symfony/twig-bundle": "^2.8|^3.0|^4.0", + "symfony/twig-bundle": "^3.0|^4.0", "symfony/asset": "^2.8|^3.0|^4.0", "symfony/console": "^2.8|^3.0|^4.0", "symfony/config": "^2.8|^3.0|^4.0", From 9c0db1ea51c624085ce25990c000f07dd4c0111b Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Wed, 14 Jun 2017 13:47:53 +0200 Subject: [PATCH 3/3] Fix symfony 4 support --- Tests/Functional/Entity/Article.php | 2 +- Tests/Functional/FunctionalTest.php | 1 - Tests/Functional/SwaggerUiTest.php | 2 -- Tests/Functional/TestKernel.php | 3 +++ Tests/Functional/WebTestCase.php | 27 +++++++++++++++++++++++++++ composer.json | 3 ++- phpunit.xml.dist | 4 ---- 7 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 Tests/Functional/WebTestCase.php diff --git a/Tests/Functional/Entity/Article.php b/Tests/Functional/Entity/Article.php index 5d295e4..922e546 100644 --- a/Tests/Functional/Entity/Article.php +++ b/Tests/Functional/Entity/Article.php @@ -19,7 +19,7 @@ use Symfony\Component\Serializer\Annotation\Groups; class Article { /** - * @Groups("light") + * @Groups({"light"}) */ public function setAuthor(User $author) { diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index f9039c0..dc87b22 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -14,7 +14,6 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; use EXSyst\Component\Swagger\Operation; use EXSyst\Component\Swagger\Schema; use EXSyst\Component\Swagger\Tag; -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class FunctionalTest extends WebTestCase { diff --git a/Tests/Functional/SwaggerUiTest.php b/Tests/Functional/SwaggerUiTest.php index 0b17ae8..189f033 100644 --- a/Tests/Functional/SwaggerUiTest.php +++ b/Tests/Functional/SwaggerUiTest.php @@ -11,8 +11,6 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; -use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; - class SwaggerUiTest extends WebTestCase { public function testSwaggerUi() diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index f1bc297..a1ec318 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +namespace Nelmio\ApiDocBundle\Tests\Functional; + use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle; use Nelmio\ApiDocBundle\NelmioApiDocBundle; use Nelmio\ApiDocBundle\Tests\Functional\TestBundle; @@ -62,6 +64,7 @@ class TestKernel extends Kernel 'templating' => [ 'engines' => ['twig'], ], + 'serializer' => ['enable_annotations' => true], ]); // Filter routes diff --git a/Tests/Functional/WebTestCase.php b/Tests/Functional/WebTestCase.php new file mode 100644 index 0000000..af25700 --- /dev/null +++ b/Tests/Functional/WebTestCase.php @@ -0,0 +1,27 @@ + - - - - .