Issue 1848 operation id by route name (#1907)

* Fix #1885 update psr/log and psr/container

* Issue #1848 operation id by route name

Co-authored-by: Vlad Gaiduk <uahaiduk@gmail.com>
This commit is contained in:
Vladislav 2021-11-22 22:18:16 +03:00 committed by GitHub
parent 0ed76d7d24
commit d59dbbd859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 5 deletions

View File

@ -47,7 +47,7 @@ final class OpenApiPhpDescriber
$classAnnotations = []; $classAnnotations = [];
/** @var \ReflectionMethod $method */ /** @var \ReflectionMethod $method */
foreach ($this->getMethodsToParse() as $method => list($path, $httpMethods)) { foreach ($this->getMethodsToParse() as $method => list($path, $httpMethods, $routeName)) {
$declaringClass = $method->getDeclaringClass(); $declaringClass = $method->getDeclaringClass();
$path = Util::getPath($api, $path); $path = Util::getPath($api, $path);
@ -134,6 +134,10 @@ final class OpenApiPhpDescriber
$operation = Util::getOperation($path, $httpMethod); $operation = Util::getOperation($path, $httpMethod);
$operation->merge($implicitAnnotations); $operation->merge($implicitAnnotations);
$operation->mergeProperties($mergeProperties); $operation->mergeProperties($mergeProperties);
if (OA\UNDEFINED === $operation->operationId) {
$operation->operationId = $httpMethod.'_'.$routeName;
}
} }
} }
@ -143,7 +147,7 @@ final class OpenApiPhpDescriber
private function getMethodsToParse(): \Generator private function getMethodsToParse(): \Generator
{ {
foreach ($this->routeCollection->all() as $route) { foreach ($this->routeCollection->all() as $routeName => $route) {
if (!$route->hasDefault('_controller')) { if (!$route->hasDefault('_controller')) {
continue; continue;
} }
@ -161,7 +165,7 @@ final class OpenApiPhpDescriber
continue; continue;
} }
yield $reflectedMethod => [$path, $supportedHttpMethods]; yield $reflectedMethod => [$path, $supportedHttpMethods, $routeName];
} }
} }

View File

@ -26,7 +26,7 @@ use OpenApi\Annotations as OA;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
/** /**
* @Route("/api", host="api.example.com") * @Route("/api", name="api_", host="api.example.com")
*/ */
class ApiController class ApiController
{ {
@ -232,4 +232,23 @@ class ApiController
public function discriminatorMappingAction() public function discriminatorMappingAction()
{ {
} }
/**
* @Route("/named_route-operation-id", name="named_route_operation_id", methods={"GET", "POST"})
*
* @OA\Response(response=200, description="success")
*/
public function namedRouteOperationIdAction()
{
}
/**
* @Route("/custom-operation-id", methods={"GET", "POST"})
*
* @Operation(operationId="custom-operation-id")
* @OA\Response(response=200, description="success")
*/
public function customOperationIdAction()
{
}
} }

View File

@ -506,7 +506,25 @@ class FunctionalTest extends WebTestCase
public function testDefaultOperationId() public function testDefaultOperationId()
{ {
$operation = $this->getOperation('/api/article/{id}', 'get'); $operation = $this->getOperation('/api/article/{id}', 'get');
$this->assertNull($operation->operationId); $this->assertEquals('get_api_nelmio_apidoc_tests_functional_api_fetcharticle', $operation->operationId);
}
public function testNamedRouteOperationId()
{
$operation = $this->getOperation('/api/named_route-operation-id', 'get');
$this->assertEquals('get_api_named_route_operation_id', $operation->operationId);
$operation = $this->getOperation('/api/named_route-operation-id', 'post');
$this->assertEquals('post_api_named_route_operation_id', $operation->operationId);
}
public function testCustomOperationId()
{
$operation = $this->getOperation('/api/custom-operation-id', 'get');
$this->assertEquals('custom-operation-id', $operation->operationId);
$operation = $this->getOperation('/api/custom-operation-id', 'post');
$this->assertEquals('custom-operation-id', $operation->operationId);
} }
/** /**