diff --git a/src/OpenApi/Factory/OpenApiFactory.php b/src/OpenApi/Factory/OpenApiFactory.php index da3cd42d7c..b17f969a01 100644 --- a/src/OpenApi/Factory/OpenApiFactory.php +++ b/src/OpenApi/Factory/OpenApiFactory.php @@ -225,7 +225,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection $this->routeCollection = $this->router->getRouteCollection(); } - if ($this->routeCollection && $routeName && $route = $this->routeCollection->get($routeName)) { + $extraProperties = $operation->getExtraProperties(); + if ($operation->getRouteName() && ($extraProperties['user_defined_uri_template'] ?? false) && true === ($extraProperties['openapi_use_uri_template'] ?? false)) { + $path = rtrim($operation->getRoutePrefix() ?? '', '/').'/'.ltrim($operation->getUriTemplate() ?? '', '/'); + } elseif ($this->routeCollection && $routeName && $route = $this->routeCollection->get($routeName)) { $path = $route->getPath(); } else { $path = rtrim($operation->getRoutePrefix() ?? '', '/').'/'.ltrim($operation->getUriTemplate() ?? '', '/'); diff --git a/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php b/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php index 54201a8cb1..f818481959 100644 --- a/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php +++ b/src/OpenApi/Tests/Factory/OpenApiFactoryTest.php @@ -74,6 +74,8 @@ use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Psr\Container\ContainerInterface; +use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; use Symfony\Component\TypeInfo\Type; @@ -2023,6 +2025,25 @@ public function testUriVariableAbsentFromPathIsSkipped(): void $this->assertSame(['id'], array_map(static fn (Parameter $parameter): string => $parameter->getName(), array_values(array_filter($parameters, static fn (Parameter $parameter): bool => 'path' === $parameter->getIn())))); } + public function testUserDefinedUriTemplateIsDocumentedInsteadOfSharedRoutePathWhenOptedIn(): void + { + $routeCollection = new RouteCollection(); + $routeCollection->add('shared_route', new Route('/shared/{kind}')); + $router = $this->createStub(RouterInterface::class); + $router->method('getRouteCollection')->willReturn($routeCollection); + + $operations = static fn (array $extraProperties): array => [ + (new Get())->withUriTemplate('/shared/a')->withRouteName('shared_route')->withShortName('Dummy')->withName('shared_a')->withExtraProperties($extraProperties), + (new Get())->withUriTemplate('/shared/b')->withRouteName('shared_route')->withShortName('Dummy')->withName('shared_b')->withExtraProperties($extraProperties), + ]; + + $optedIn = $this->createOpenApiForOperations($operations(['user_defined_uri_template' => true, 'openapi_use_uri_template' => true]), $router); + $this->assertSame(['/shared/a', '/shared/b'], array_keys($optedIn->getPaths()->getPaths())); + + $default = $this->createOpenApiForOperations($operations(['user_defined_uri_template' => true]), $router); + $this->assertSame(['/shared/{kind}'], array_keys($default->getPaths()->getPaths())); + } + /** * @param HttpOperation[] $operations */ diff --git a/src/OpenApi/composer.json b/src/OpenApi/composer.json index 48bef8240f..466cda2f4a 100644 --- a/src/OpenApi/composer.json +++ b/src/OpenApi/composer.json @@ -44,6 +44,7 @@ "api-platform/doctrine-orm": "^5.0", "api-platform/doctrine-odm": "^5.0", "api-platform/serializer": "^5.0", + "symfony/routing": "^7.4 || ^8.0", "symfony/type-info": "^7.4 || ^8.0" }, "autoload": {