mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 23:59:26 +03:00
commit
d1761826ab
@ -18,11 +18,25 @@ use Swagger\Annotations\AbstractAnnotation;
|
|||||||
*/
|
*/
|
||||||
final class Model extends AbstractAnnotation
|
final class Model extends AbstractAnnotation
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** @inheritdoc */
|
||||||
|
public static $_types = [
|
||||||
|
'type' => 'string',
|
||||||
|
'groups' => '[string]',
|
||||||
|
];
|
||||||
public static $_required = ['type'];
|
public static $_required = ['type'];
|
||||||
public static $_parents = [
|
public static $_parents = [
|
||||||
'Swagger\Annotations\Parameter',
|
'Swagger\Annotations\Parameter',
|
||||||
'Swagger\Annotations\Response',
|
'Swagger\Annotations\Response',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
public $type;
|
public $type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
public $groups;
|
||||||
}
|
}
|
||||||
|
@ -16,22 +16,35 @@ use Symfony\Component\PropertyInfo\Type;
|
|||||||
final class Model
|
final class Model
|
||||||
{
|
{
|
||||||
private $type;
|
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->type = $type;
|
||||||
|
$this->groups = $groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Type|null
|
* @return Type
|
||||||
*/
|
*/
|
||||||
public function getType()
|
public function getType()
|
||||||
{
|
{
|
||||||
return $this->type;
|
return $this->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string[]|null
|
||||||
|
*/
|
||||||
|
public function getGroups()
|
||||||
|
{
|
||||||
|
return $this->groups;
|
||||||
|
}
|
||||||
|
|
||||||
public function getHash(): string
|
public function getHash(): string
|
||||||
{
|
{
|
||||||
return md5(serialize($this->type));
|
return md5(serialize([$this->type, $this->groups]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ class CollectionModelDescriber implements ModelDescriberInterface, ModelRegistry
|
|||||||
{
|
{
|
||||||
$schema->setType('array');
|
$schema->setType('array');
|
||||||
$schema->getItems()->setRef(
|
$schema->getItems()->setRef(
|
||||||
$this->modelRegistry->register(new Model($model->getType()->getCollectionValueType()))
|
$this->modelRegistry->register(new Model($model->getType()->getCollectionValueType(), $model->getGroups()))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,12 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
|
|||||||
$properties = $schema->getProperties();
|
$properties = $schema->getProperties();
|
||||||
|
|
||||||
$class = $model->getType()->getClassName();
|
$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) {
|
if (null === $propertyInfoProperties) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -50,7 +55,7 @@ class ObjectModelDescriber implements ModelDescriberInterface, ModelRegistryAwar
|
|||||||
}
|
}
|
||||||
|
|
||||||
$properties->get($propertyName)->setRef(
|
$properties->get($propertyName)->setRef(
|
||||||
$this->modelRegistry->register(new Model($types[0]))
|
$this->modelRegistry->register(new Model($types[0], $model->getGroups()))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,8 +70,9 @@ final class ModelRegister
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$annotation->merge([new $annotationClass([
|
$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
|
// It is no longer an unmerged annotation
|
||||||
|
@ -16,6 +16,7 @@ use FOS\RestBundle\Controller\Annotations\RequestParam;
|
|||||||
use Nelmio\ApiDocBundle\Annotation\Model;
|
use Nelmio\ApiDocBundle\Annotation\Model;
|
||||||
use Nelmio\ApiDocBundle\Annotation\Operation;
|
use Nelmio\ApiDocBundle\Annotation\Operation;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\User;
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\User;
|
||||||
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article;
|
||||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
||||||
use Swagger\Annotations as SWG;
|
use Swagger\Annotations as SWG;
|
||||||
|
|
||||||
@ -24,6 +25,18 @@ use Swagger\Annotations as SWG;
|
|||||||
*/
|
*/
|
||||||
class ApiController
|
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("/swagger", methods={"GET"})
|
||||||
* @Route("/swagger2", methods={"GET"})
|
* @Route("/swagger2", methods={"GET"})
|
||||||
|
31
Tests/Functional/Entity/Article.php
Normal file
31
Tests/Functional/Entity/Article.php
Normal 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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -14,7 +14,6 @@ namespace Nelmio\ApiDocBundle\Tests\Functional;
|
|||||||
use EXSyst\Component\Swagger\Operation;
|
use EXSyst\Component\Swagger\Operation;
|
||||||
use EXSyst\Component\Swagger\Schema;
|
use EXSyst\Component\Swagger\Schema;
|
||||||
use EXSyst\Component\Swagger\Tag;
|
use EXSyst\Component\Swagger\Tag;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
||||||
|
|
||||||
class FunctionalTest extends WebTestCase
|
class FunctionalTest extends WebTestCase
|
||||||
{
|
{
|
||||||
@ -30,6 +29,21 @@ class FunctionalTest extends WebTestCase
|
|||||||
$this->assertFalse($paths->has('/api/admin'));
|
$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()
|
public function testFilteredAction()
|
||||||
{
|
{
|
||||||
$paths = $this->getSwaggerDefinition()->getPaths();
|
$paths = $this->getSwaggerDefinition()->getPaths();
|
||||||
|
@ -11,8 +11,6 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
||||||
|
|
||||||
class SwaggerUiTest extends WebTestCase
|
class SwaggerUiTest extends WebTestCase
|
||||||
{
|
{
|
||||||
public function testSwaggerUi()
|
public function testSwaggerUi()
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\Tests\Functional;
|
||||||
|
|
||||||
use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle;
|
use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle;
|
||||||
use Nelmio\ApiDocBundle\NelmioApiDocBundle;
|
use Nelmio\ApiDocBundle\NelmioApiDocBundle;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\TestBundle;
|
use Nelmio\ApiDocBundle\Tests\Functional\TestBundle;
|
||||||
@ -62,6 +64,7 @@ class TestKernel extends Kernel
|
|||||||
'templating' => [
|
'templating' => [
|
||||||
'engines' => ['twig'],
|
'engines' => ['twig'],
|
||||||
],
|
],
|
||||||
|
'serializer' => ['enable_annotations' => true],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Filter routes
|
// Filter routes
|
||||||
|
27
Tests/Functional/WebTestCase.php
Normal file
27
Tests/Functional/WebTestCase.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -16,13 +16,14 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": "~7.0|~7.1",
|
"php": "~7.0|~7.1",
|
||||||
"symfony/framework-bundle": "^2.8.18|^3.0|^4.0",
|
"symfony/framework-bundle": "^3.2.5|^4.0",
|
||||||
"symfony/property-info": "^2.8|^3.0|^4.0",
|
"symfony/property-info": "^3.1|^4.0",
|
||||||
"exsyst/swagger": "~0.2.3",
|
"exsyst/swagger": "~0.2.3",
|
||||||
"zircote/swagger-php": "^2.0.9"
|
"zircote/swagger-php": "^2.0.9"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"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/asset": "^2.8|^3.0|^4.0",
|
||||||
"symfony/console": "^2.8|^3.0|^4.0",
|
"symfony/console": "^2.8|^3.0|^4.0",
|
||||||
"symfony/config": "^2.8|^3.0|^4.0",
|
"symfony/config": "^2.8|^3.0|^4.0",
|
||||||
|
@ -17,10 +17,6 @@
|
|||||||
</testsuite>
|
</testsuite>
|
||||||
</testsuites>
|
</testsuites>
|
||||||
|
|
||||||
<php>
|
|
||||||
<server name="KERNEL_DIR" value="Tests/Functional/" />
|
|
||||||
</php>
|
|
||||||
|
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist>
|
<whitelist>
|
||||||
<directory>.</directory>
|
<directory>.</directory>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user