From c90f2a11c19f10d57afc7462781bf66cb9372cdf Mon Sep 17 00:00:00 2001 From: Baptiste Lafontaine Date: Mon, 16 Apr 2018 13:43:42 +0200 Subject: [PATCH] Load class NelmioApiDocBundle annotations --- Describer/SwaggerPhpDescriber.php | 13 ++++++-- .../Controller/ClassApiController.php | 31 +++++++++++++++++++ Tests/Functional/FunctionalTest.php | 10 ++++++ Tests/Functional/TestKernel.php | 1 + 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 Tests/Functional/Controller/ClassApiController.php diff --git a/Describer/SwaggerPhpDescriber.php b/Describer/SwaggerPhpDescriber.php index 64b44a70f..f6eef4afb 100644 --- a/Describer/SwaggerPhpDescriber.php +++ b/Describer/SwaggerPhpDescriber.php @@ -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; }); @@ -105,7 +115,6 @@ public function ref($ref) continue; } - $declaringClass = $method->getDeclaringClass(); $context = new Context([ 'namespace' => $method->getNamespaceName(), 'class' => $declaringClass->getShortName(), @@ -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); diff --git a/Tests/Functional/Controller/ClassApiController.php b/Tests/Functional/Controller/ClassApiController.php new file mode 100644 index 000000000..4d92b8f41 --- /dev/null +++ b/Tests/Functional/Controller/ClassApiController.php @@ -0,0 +1,31 @@ +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([ diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index 2a2f253d6..dbd90cda1 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -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');