From 9366022da1635c4c7f75d5f8436a2e694acb7afb Mon Sep 17 00:00:00 2001 From: Gorbachev Egor <7gorbachevm@gmail.com> Date: Fri, 23 Jun 2023 15:31:38 +0300 Subject: [PATCH] Support #[Route(path: ...)] --- src/Bridge/Symfony/SymfonyControllerVisitor.php | 10 ++++++++-- tests/TypeScriptGeneratorTest.php | 9 +++++++++ ...ScriptGeneratorTest__testTypesWithApiClient__1.ts | 12 ++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Bridge/Symfony/SymfonyControllerVisitor.php b/src/Bridge/Symfony/SymfonyControllerVisitor.php index 46671ba..d341734 100644 --- a/src/Bridge/Symfony/SymfonyControllerVisitor.php +++ b/src/Bridge/Symfony/SymfonyControllerVisitor.php @@ -82,8 +82,14 @@ private function createApiEndpoint(ClassMethod $node): void $nameArg = $this->getAttributeArgumentByName($routeAttribute, 'name'); if ($nameArg?->value instanceof Node\Scalar\String_) { $route = $nameArg->value->value; - } else { - throw new \Exception('Could not find route path. Make sure your route looks like this #[Route(\'/api/users\')] or #[Route(name: \'/api/users/\')]'); + } + $pathArg = $this->getAttributeArgumentByName($routeAttribute, 'path'); + if ($pathArg?->value instanceof Node\Scalar\String_) { + $route = $pathArg->value->value; + } + + if (!$route) { + throw new \Exception('Could not find route path. Make sure your route looks like this #[Route(\'/api/users\')] or #[Route(name: \'/api/users/\')] or #[Route(path: \'/api/users/\')]'); } } diff --git a/tests/TypeScriptGeneratorTest.php b/tests/TypeScriptGeneratorTest.php index fc594ff..c68e748 100644 --- a/tests/TypeScriptGeneratorTest.php +++ b/tests/TypeScriptGeneratorTest.php @@ -554,6 +554,14 @@ public function getUser(User $userToUpdate, #[Input] UpdateUserInput $input) {} #[DtoEndpoint(returnMany: UserOutput::class)] #[Route('/api/users-with-filters', methods: ['GET'])] public function getUsersWithFilters(#[Query] FilterQuery $query) {} + + #[DtoEndpoint()] + #[Route(path: '/api/route-with-path', methods: ['GET'])] + public function routeWithPath() {} + + #[DtoEndpoint()] + #[Route(name: '/api/route-with-name', methods: ['GET'])] + public function routeWithName() {} } CODE; @@ -562,6 +570,7 @@ public function getUsersWithFilters(#[Query] FilterQuery $query) {} new SymfonyControllerVisitor('DtoEndpoint'), ]); $result = $converter->convert([$codeWithDateTime]); + $this->assertCount(7, $result->apiEndpointList->getList()); $this->assertMatchesGeneratedTypeScriptApi($result); } diff --git a/tests/__snapshots__/TypeScriptGeneratorTest__testTypesWithApiClient__1.ts b/tests/__snapshots__/TypeScriptGeneratorTest__testTypesWithApiClient__1.ts index 724f02c..6f85fc0 100644 --- a/tests/__snapshots__/TypeScriptGeneratorTest__testTypesWithApiClient__1.ts +++ b/tests/__snapshots__/TypeScriptGeneratorTest__testTypesWithApiClient__1.ts @@ -19,6 +19,18 @@ export type UserOutput = { id: string; }; +export const apiRouteWithNameGet = (): Promise => { + return axios + .get(`/api/route-with-name`) + .then((response) => response.data); +} + +export const apiRouteWithPathGet = (): Promise => { + return axios + .get(`/api/route-with-path`) + .then((response) => response.data); +} + export const apiUsersGet = (): Promise => { return axios .get(`/api/users`)