Skip to content

Commit

Permalink
Merge pull request #1297 from magnetik/class
Browse files Browse the repository at this point in the history
Load class NelmioApiDocBundle annotations
  • Loading branch information
GuilhemN authored Apr 19, 2018
2 parents 604ab3d + c90f2a1 commit 473a8cc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Describer/SwaggerPhpDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,17 @@ public function ref($ref)
'head' => SWG\Head::class,
];

$classAnnotations = [];

foreach ($this->getMethodsToParse() as $method => list($path, $httpMethods)) {
$declaringClass = $method->getDeclaringClass();
if (!array_key_exists($declaringClass->getName(), $classAnnotations)) {
$classAnnotations = array_filter($this->annotationReader->getClassAnnotations($declaringClass), function ($v) {
return $v instanceof SWG\AbstractAnnotation;
});
$classAnnotations[$declaringClass->getName()] = $classAnnotations;
}

$annotations = array_filter($this->annotationReader->getMethodAnnotations($method), function ($v) {
return $v instanceof SWG\AbstractAnnotation;
});
Expand All @@ -105,7 +115,6 @@ public function ref($ref)
continue;
}

$declaringClass = $method->getDeclaringClass();
$context = new Context([
'namespace' => $method->getNamespaceName(),
'class' => $declaringClass->getShortName(),
Expand All @@ -117,7 +126,7 @@ public function ref($ref)
$implicitAnnotations = [];
$tags = [];
$security = [];
foreach ($annotations as $annotation) {
foreach (array_merge($annotations, $classAnnotations[$declaringClass->getName()]) as $annotation) {
$annotation->_context = $context;
$this->updateNestedAnnotations($annotation, $nestedContext);

Expand Down
31 changes: 31 additions & 0 deletions Tests/Functional/Controller/ClassApiController.php
Original file line number Diff line number Diff line change
@@ -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\Controller;

use Nelmio\ApiDocBundle\Annotation\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Swagger\Annotations as SWG;

/**
* @Route("/api", host="api.example.com")
* @Security(name="basic")
*/
class ClassApiController
{
/**
* @Route("/security/class")
* @SWG\Response(response="201", description="")
*/
public function securityAction()
{
}
}
10 changes: 10 additions & 0 deletions Tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,16 @@ public function testSecurityAction()
$this->assertEquals($expected, $operation->getSecurity());
}

public function testClassSecurityAction()
{
$operation = $this->getOperation('/api/security/class', 'get');

$expected = [
['basic' => []],
];
$this->assertEquals($expected, $operation->getSecurity());
}

public function testSymfonyConstraintDocumentation()
{
$this->assertEquals([
Expand Down
1 change: 1 addition & 0 deletions Tests/Functional/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->import(__DIR__.'/Controller/TestController.php', '/', 'annotation');
$routes->import(__DIR__.'/Controller/ApiController.php', '/', 'annotation');
$routes->import(__DIR__.'/Controller/ClassApiController.php', '/', 'annotation');
$routes->import(__DIR__.'/Controller/UndocumentedController.php', '/', 'annotation');
$routes->import('', '/api', 'api_platform');
$routes->add('/docs/{area}', 'nelmio_api_doc.controller.swagger_ui')->setDefault('area', 'default');
Expand Down

0 comments on commit 473a8cc

Please sign in to comment.