Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore unsupported HTTP methods #1294

Merged
merged 4 commits into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -206,8 +209,18 @@ private function getMethodsToParse(): \Generator
$path = $this->normalizePath($route->getPath());
$httpMethods = $route->getMethods() ?: Swagger::$METHODS;
$httpMethods = array_map('strtolower', $httpMethods);
$validHttpMethods = array_intersect($httpMethods, Swagger::$METHODS);

yield $method => [$path, $httpMethods];
if (empty($validHttpMethods)) {
$this->logger->warning('No valid HTTP method for path', [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about replacing no valid by no supported? It is less alarming imo :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to help the user some more, we could even say: "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, $validHttpMethods];
}
}
}
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