mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-10 03:29:25 +03:00
Added support for @Security
annotations (#1201)
* Added support for `@Security` annotations * Don't output empty tag and security if none defined
This commit is contained in:
parent
4e2a25c9bc
commit
b4c3dcd6ae
32
Annotation/Security.php
Normal file
32
Annotation/Security.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?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\Annotation;
|
||||||
|
|
||||||
|
use Swagger\Annotations\AbstractAnnotation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Annotation
|
||||||
|
*/
|
||||||
|
class Security extends AbstractAnnotation
|
||||||
|
{
|
||||||
|
/** {@inheritdoc} */
|
||||||
|
public static $_types = [
|
||||||
|
'name' => 'string',
|
||||||
|
];
|
||||||
|
|
||||||
|
public static $_required = ['name'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $name;
|
||||||
|
}
|
@ -16,6 +16,7 @@ JMS Serializer
|
|||||||
|
|
||||||
SwaggerPHP
|
SwaggerPHP
|
||||||
* Handle `enum` and `default` properties from SwaggerPHP annotation
|
* Handle `enum` and `default` properties from SwaggerPHP annotation
|
||||||
|
* Support `@Security` annotations
|
||||||
|
|
||||||
Config
|
Config
|
||||||
* `nelmio_api_doc.routes` has been replaced by `nelmio_api_doc.areas`. Please update your config accordingly.
|
* `nelmio_api_doc.routes` has been replaced by `nelmio_api_doc.areas`. Please update your config accordingly.
|
||||||
|
@ -14,6 +14,7 @@ namespace Nelmio\ApiDocBundle\Describer;
|
|||||||
use Doctrine\Common\Annotations\Reader;
|
use Doctrine\Common\Annotations\Reader;
|
||||||
use EXSyst\Component\Swagger\Swagger;
|
use EXSyst\Component\Swagger\Swagger;
|
||||||
use Nelmio\ApiDocBundle\Annotation\Operation;
|
use Nelmio\ApiDocBundle\Annotation\Operation;
|
||||||
|
use Nelmio\ApiDocBundle\Annotation\Security;
|
||||||
use Nelmio\ApiDocBundle\SwaggerPhp\AddDefaults;
|
use Nelmio\ApiDocBundle\SwaggerPhp\AddDefaults;
|
||||||
use Nelmio\ApiDocBundle\SwaggerPhp\ModelRegister;
|
use Nelmio\ApiDocBundle\SwaggerPhp\ModelRegister;
|
||||||
use Nelmio\ApiDocBundle\Util\ControllerReflector;
|
use Nelmio\ApiDocBundle\Util\ControllerReflector;
|
||||||
@ -45,7 +46,7 @@ final class SwaggerPhpDescriber extends ExternalDocDescriber implements ModelReg
|
|||||||
$analysis->process($this->getProcessors());
|
$analysis->process($this->getProcessors());
|
||||||
$analysis->validate();
|
$analysis->validate();
|
||||||
|
|
||||||
return json_decode(json_encode($analysis->swagger));
|
return json_decode(json_encode($analysis->swagger), true);
|
||||||
}, $overwrite);
|
}, $overwrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,6 +94,7 @@ final class SwaggerPhpDescriber extends ExternalDocDescriber implements ModelReg
|
|||||||
$nestedContext->nested = true;
|
$nestedContext->nested = true;
|
||||||
$implicitAnnotations = [];
|
$implicitAnnotations = [];
|
||||||
$tags = [];
|
$tags = [];
|
||||||
|
$security = [];
|
||||||
foreach ($annotations as $annotation) {
|
foreach ($annotations as $annotation) {
|
||||||
$annotation->_context = $context;
|
$annotation->_context = $context;
|
||||||
$this->updateNestedAnnotations($annotation, $nestedContext);
|
$this->updateNestedAnnotations($annotation, $nestedContext);
|
||||||
@ -121,6 +123,13 @@ final class SwaggerPhpDescriber extends ExternalDocDescriber implements ModelReg
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($annotation instanceof Security) {
|
||||||
|
$annotation->validate();
|
||||||
|
$security[] = [$annotation->name => []];
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ($annotation instanceof SWG\Tag) {
|
if ($annotation instanceof SWG\Tag) {
|
||||||
$annotation->validate();
|
$annotation->validate();
|
||||||
$tags[] = $annotation->name;
|
$tags[] = $annotation->name;
|
||||||
@ -135,13 +144,26 @@ final class SwaggerPhpDescriber extends ExternalDocDescriber implements ModelReg
|
|||||||
$implicitAnnotations[] = $annotation;
|
$implicitAnnotations[] = $annotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 === count($implicitAnnotations) && 0 === count($tags)) {
|
if (0 === count($implicitAnnotations) && 0 === count($tags) && 0 === count($security)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($httpMethods as $httpMethod) {
|
foreach ($httpMethods as $httpMethod) {
|
||||||
$annotationClass = $operationAnnotations[$httpMethod];
|
$annotationClass = $operationAnnotations[$httpMethod];
|
||||||
$operation = new $annotationClass(['_context' => $context, 'path' => $path, 'value' => $implicitAnnotations, 'tags' => $tags]);
|
$constructorArg = [
|
||||||
|
'_context' => $context,
|
||||||
|
'path' => $path,
|
||||||
|
'value' => $implicitAnnotations,
|
||||||
|
];
|
||||||
|
|
||||||
|
if (0 !== count($tags)) {
|
||||||
|
$constructorArg['tags'] = $tags;
|
||||||
|
}
|
||||||
|
if (0 !== count($security)) {
|
||||||
|
$constructorArg['security'] = $security;
|
||||||
|
}
|
||||||
|
|
||||||
|
$operation = new $annotationClass($constructorArg);
|
||||||
$analysis->addAnnotation($operation, null);
|
$analysis->addAnnotation($operation, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ use FOS\RestBundle\Controller\Annotations\QueryParam;
|
|||||||
use FOS\RestBundle\Controller\Annotations\RequestParam;
|
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\Annotation\Security;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article;
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Entity\User;
|
use Nelmio\ApiDocBundle\Tests\Functional\Entity\User;
|
||||||
use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType;
|
use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType;
|
||||||
@ -157,4 +158,14 @@ class ApiController
|
|||||||
public function formAction()
|
public function formAction()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/security")
|
||||||
|
* @SWG\Response(response="201", description="")
|
||||||
|
* @Security(name="api_key")
|
||||||
|
* @Security(name="basic")
|
||||||
|
*/
|
||||||
|
public function securityAction()
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -238,4 +238,15 @@ class FunctionalTest extends WebTestCase
|
|||||||
'required' => ['foo'],
|
'required' => ['foo'],
|
||||||
], $this->getModel('DummyType')->toArray());
|
], $this->getModel('DummyType')->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSecurityAction()
|
||||||
|
{
|
||||||
|
$operation = $this->getOperation('/api/security', 'get');
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
['api_key' => []],
|
||||||
|
['basic' => []],
|
||||||
|
];
|
||||||
|
$this->assertEquals($expected, $operation->getSecurity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user