Merge pull request #1010 from nelmio/groups

Add groups support
This commit is contained in:
Guilhem Niot 2017-06-14 12:26:52 +00:00 committed by GitHub
commit d1761826ab
13 changed files with 133 additions and 17 deletions

View File

@ -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;
}

View File

@ -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]));
}
}

View File

@ -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()))
);
}

View File

@ -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()))
);
}
}

View File

@ -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

View File

@ -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"})

View File

@ -0,0 +1,31 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @author Guilhem N. <guilhem.niot@gmail.com>
*/
class Article
{
/**
* @Groups({"light"})
*/
public function setAuthor(User $author)
{
}
public function setContent(string $content)
{
}
}

View File

@ -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();

View File

@ -11,8 +11,6 @@
namespace Nelmio\ApiDocBundle\Tests\Functional;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SwaggerUiTest extends WebTestCase
{
public function testSwaggerUi()

View File

@ -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

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Functional;
use Nelmio\ApiDocBundle\Tests\Functional\TestKernel;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
class WebTestCase extends BaseWebTestCase
{
/**
* {@inheritdoc}
*/
protected static function getKernelClass()
{
return TestKernel::class;
}
}

View File

@ -16,13 +16,14 @@
],
"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.2.5|^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/templating": "^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",

View File

@ -17,10 +17,6 @@
</testsuite>
</testsuites>
<php>
<server name="KERNEL_DIR" value="Tests/Functional/" />
</php>
<filter>
<whitelist>
<directory>.</directory>