Skip to content

Commit

Permalink
Merge pull request #1294 from Hexanet/fix-link-unlink-methods
Browse files Browse the repository at this point in the history
Ignore unsupported HTTP methods
  • Loading branch information
GuilhemN authored Apr 20, 2018
2 parents 473a8cc + de8704f commit 1680ba3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/NelmioApiDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function load(array $configs, ContainerBuilder $container)
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]);
}
Expand Down
17 changes: 15 additions & 2 deletions Describer/SwaggerPhpDescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -215,8 +218,18 @@ private function getMethodsToParse(): \Generator
$path = $this->normalizePath($route->getPath());
$httpMethods = $route->getMethods() ?: Swagger::$METHODS;
$httpMethods = array_map('strtolower', $httpMethods);
$supportedHttpMethods = array_intersect($httpMethods, Swagger::$METHODS);

yield $method => [$path, $httpMethods];
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,
]);

continue;
}

yield $method => [$path, $supportedHttpMethods];
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/Functional/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function fetchArticleAction()
}

/**
* @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")
Expand Down

0 comments on commit 1680ba3

Please sign in to comment.