From 7aa4c829b797f87a9737214e4fa3853625f41686 Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Thu, 12 Apr 2018 16:09:22 +0200 Subject: [PATCH 1/4] Ignore LINK and UNLINK methods --- Describer/SwaggerPhpDescriber.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Describer/SwaggerPhpDescriber.php b/Describer/SwaggerPhpDescriber.php index 64b44a7..608d10b 100644 --- a/Describer/SwaggerPhpDescriber.php +++ b/Describer/SwaggerPhpDescriber.php @@ -206,6 +206,7 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface $path = $this->normalizePath($route->getPath()); $httpMethods = $route->getMethods() ?: Swagger::$METHODS; $httpMethods = array_map('strtolower', $httpMethods); + $httpMethods = array_intersect($httpMethods, Swagger::$METHODS); yield $method => [$path, $httpMethods]; } From eb7e6b6803ce158bf30dce77b449fcb8b2c3f611 Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Thu, 19 Apr 2018 09:51:52 +0200 Subject: [PATCH 2/4] Ignore path without valid methods --- Describer/SwaggerPhpDescriber.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Describer/SwaggerPhpDescriber.php b/Describer/SwaggerPhpDescriber.php index 608d10b..df670bc 100644 --- a/Describer/SwaggerPhpDescriber.php +++ b/Describer/SwaggerPhpDescriber.php @@ -208,6 +208,10 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface $httpMethods = array_map('strtolower', $httpMethods); $httpMethods = array_intersect($httpMethods, Swagger::$METHODS); + if (empty($httpMethods)) { + continue; + } + yield $method => [$path, $httpMethods]; } } From 22c6eb5958a52b08004c0926a3ed318c35d34e7a Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Fri, 20 Apr 2018 10:34:55 +0200 Subject: [PATCH 3/4] Add log when there are no valid http methods --- DependencyInjection/NelmioApiDocExtension.php | 1 + Describer/SwaggerPhpDescriber.php | 16 ++++++++++++---- Tests/Functional/Controller/ApiController.php | 4 +++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index d17877a..6d1690f 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -95,6 +95,7 @@ final class NelmioApiDocExtension extends Extension implements PrependExtensionI new Reference(sprintf('nelmio_api_doc.routes.%s', $area)), new Reference('nelmio_api_doc.controller_reflector'), new Reference('annotation_reader'), + new Reference('logger'), ]) ->addTag(sprintf('nelmio_api_doc.describer.%s', $area), ['priority' => -200]); } diff --git a/Describer/SwaggerPhpDescriber.php b/Describer/SwaggerPhpDescriber.php index df670bc..3605cf5 100644 --- a/Describer/SwaggerPhpDescriber.php +++ b/Describer/SwaggerPhpDescriber.php @@ -18,6 +18,7 @@ use Nelmio\ApiDocBundle\Annotation\Security; use Nelmio\ApiDocBundle\SwaggerPhp\AddDefaults; use Nelmio\ApiDocBundle\SwaggerPhp\ModelRegister; use Nelmio\ApiDocBundle\Util\ControllerReflector; +use Psr\Log\LoggerInterface; use Swagger\Analysis; use Swagger\Annotations\AbstractAnnotation; use Swagger\Annotations as SWG; @@ -31,13 +32,15 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface private $routeCollection; private $controllerReflector; private $annotationReader; + private $logger; private $overwrite; - public function __construct(RouteCollection $routeCollection, ControllerReflector $controllerReflector, Reader $annotationReader, bool $overwrite = false) + public function __construct(RouteCollection $routeCollection, ControllerReflector $controllerReflector, Reader $annotationReader, LoggerInterface $logger, bool $overwrite = false) { $this->routeCollection = $routeCollection; $this->controllerReflector = $controllerReflector; $this->annotationReader = $annotationReader; + $this->logger = $logger; $this->overwrite = $overwrite; } @@ -206,13 +209,18 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface $path = $this->normalizePath($route->getPath()); $httpMethods = $route->getMethods() ?: Swagger::$METHODS; $httpMethods = array_map('strtolower', $httpMethods); - $httpMethods = array_intersect($httpMethods, Swagger::$METHODS); + $validHttpMethods = array_intersect($httpMethods, Swagger::$METHODS); + + if (empty($validHttpMethods)) { + $this->logger->warning('No valid HTTP method for path', [ + 'path' => $path, + 'methods' => $httpMethods, + ]); - if (empty($httpMethods)) { continue; } - yield $method => [$path, $httpMethods]; + yield $method => [$path, $validHttpMethods]; } } } diff --git a/Tests/Functional/Controller/ApiController.php b/Tests/Functional/Controller/ApiController.php index 2db5fa5..678fee8 100644 --- a/Tests/Functional/Controller/ApiController.php +++ b/Tests/Functional/Controller/ApiController.php @@ -44,7 +44,9 @@ class ApiController } /** - * @Route("/swagger", methods={"GET"}) + * The method LINK is not supported by OpenAPI so the method will be ignored. + * + * @Route("/swagger", methods={"GET", "LINK"}) * @Route("/swagger2", methods={"GET"}) * @Operation( * @SWG\Response(response="201", description="An example resource") From de8704f6433a89d50e5e84cc7bbef69bd718cbd6 Mon Sep 17 00:00:00 2001 From: Floran Brutel Date: Fri, 20 Apr 2018 17:24:28 +0200 Subject: [PATCH 4/4] Better log message for unsupported http method --- Describer/SwaggerPhpDescriber.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Describer/SwaggerPhpDescriber.php b/Describer/SwaggerPhpDescriber.php index 3605cf5..e529fa9 100644 --- a/Describer/SwaggerPhpDescriber.php +++ b/Describer/SwaggerPhpDescriber.php @@ -209,10 +209,10 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface $path = $this->normalizePath($route->getPath()); $httpMethods = $route->getMethods() ?: Swagger::$METHODS; $httpMethods = array_map('strtolower', $httpMethods); - $validHttpMethods = array_intersect($httpMethods, Swagger::$METHODS); + $supportedHttpMethods = array_intersect($httpMethods, Swagger::$METHODS); - if (empty($validHttpMethods)) { - $this->logger->warning('No valid HTTP method for path', [ + if (empty($supportedHttpMethods)) { + $this->logger->warning('None of the HTTP methods specified for path {path} are supported by swagger-ui, skipping this path', [ 'path' => $path, 'methods' => $httpMethods, ]); @@ -220,7 +220,7 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface continue; } - yield $method => [$path, $validHttpMethods]; + yield $method => [$path, $supportedHttpMethods]; } } }