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 b148caf..f608100 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..922e546 --- /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 4272718..ec4ea20 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 { @@ -30,6 +29,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(); 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 @@ + - - - - .