From 75bdf2f0c06dd8b97afdaad5247d40b9fd2085a5 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Thu, 12 Dec 2024 12:25:46 +0330 Subject: [PATCH 1/7] Refactor resolveClassMethodDependencies method; --- src/Traits/GetDependenciesTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Traits/GetDependenciesTrait.php b/src/Traits/GetDependenciesTrait.php index 9db9dfe..b6e5d5e 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -11,10 +11,10 @@ trait GetDependenciesTrait { - protected function resolveClassMethodDependencies(array $parameters, $instance, $method) + protected function resolveClassMethodDependencies($instance, $method) { if (!method_exists($instance, $method)) { - return $parameters; + return []; } return $this->getDependencies( From c72525d6956e28f7eaed6ed95607b1a0495b6526 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Thu, 12 Dec 2024 12:26:05 +0330 Subject: [PATCH 2/7] Refactor resolveClassMethodDependencies usage; --- src/Services/SwaggerService.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index 5c8cf87..ed8ee26 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -647,10 +647,8 @@ public function getConcreteRequest() $method = $explodedController[1]; $instance = app($class); - $route = $this->request->route(); $parameters = $this->resolveClassMethodDependencies( - $route->parametersWithoutNulls(), $instance, $method ); From 3bc1d8e46895096bd5bf60b51fa164930f910ef0 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Tue, 17 Dec 2024 18:20:03 +0330 Subject: [PATCH 3/7] Refactor GetDependenciesTrait; --- src/Traits/GetDependenciesTrait.php | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/Traits/GetDependenciesTrait.php b/src/Traits/GetDependenciesTrait.php index b6e5d5e..2922e34 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -11,33 +11,22 @@ trait GetDependenciesTrait { - protected function resolveClassMethodDependencies($instance, $method) - { - if (!method_exists($instance, $method)) { - return []; - } - - return $this->getDependencies( - new ReflectionMethod($instance, $method) - ); - } - - public function getDependencies(ReflectionFunctionAbstract $reflector) + public function resolveClassMethodDependencies(object $instance, string $method): array { return array_map(function ($parameter) { return $this->transformDependency($parameter); - }, $reflector->getParameters()); + }, (new ReflectionMethod($instance, $method))->getParameters()); } protected function transformDependency(ReflectionParameter $parameter) { - $class = $parameter->getClass(); + $class = $parameter->getType(); if (empty($class)) { return null; } - return interface_exists($class->name) ? $this->getClassByInterface($class->name) : $class->name; + return interface_exists($class->getName()) ? $this->getClassByInterface($class->getName()) : $class->getName(); } protected function getClassByInterface($interfaceName) From 8b725cb3862caf1a50b340b12ed961d50a90cabf Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Tue, 17 Dec 2024 18:20:26 +0330 Subject: [PATCH 4/7] Update getConcreteRequest method; --- src/Services/SwaggerService.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index ed8ee26..85ed1b3 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -646,10 +646,12 @@ public function getConcreteRequest() $class = $explodedController[0]; $method = $explodedController[1]; - $instance = app($class); + if (!method_exists($class, $method)) { + return null; + } $parameters = $this->resolveClassMethodDependencies( - $instance, + app($class), $method ); From f54827bd9809042f04dd486f1017826ec7d4c002 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Tue, 17 Dec 2024 19:09:52 +0330 Subject: [PATCH 5/7] Update testAddDataWithNotExistsMethodOnController testcase; Use mockDriverGetEmptyAndSaveTmpData instead of mockDriverGetTmpData method to ensure the recieved data will be saved without any modifications; --- tests/SwaggerServiceTest.php | 2 +- ...et_user_request_without_request_class.json | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_without_request_class.json diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index 8145bfc..d4faedf 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -687,7 +687,7 @@ public function testAddDataPostRequestWithObjectParams() public function testAddDataWithNotExistsMethodOnController() { - $this->mockDriverGetTmpData($this->getJsonFixture('tmp_data_get_user_request')); + $this->mockDriverGetEmptyAndSaveTmpData($this->getJsonFixture('tmp_data_get_user_request_without_request_class')); $service = app(SwaggerService::class); diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_without_request_class.json b/tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_without_request_class.json new file mode 100644 index 0000000..b5bc231 --- /dev/null +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_without_request_class.json @@ -0,0 +1,99 @@ +{ + "openapi": "3.1.0", + "servers": [ + { + "url": "http:\/\/localhost" + } + ], + "paths": { + "/users/{id}/assign-role/{role-id}": { + "get": { + "tags": [ + "users" + ], + "consumes": [], + "produces": [ + "application/json" + ], + "parameters": [ + { + "in": "path", + "name": "id", + "description": "", + "required": true, + "schema": { + "type": "string" + } + }, + { + "in": "path", + "name": "role-id", + "description": "", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/getUsers{id}assignRole{roleId}200ResponseObject", + "type": "object" + }, + "example": { + "id": 2, + "name": "first_client", + "likes_count": 23, + "role": { + "id": 2, + "name": "client" + }, + "type": "reader" + } + } + } + } + }, + "security": [], + "description": "" + } + } + }, + "components": { + "schemas": { + "getUsers{id}assignRole{roleId}200ResponseObject": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "likes_count": { + "type": "integer" + }, + "role": { + "type": "array" + }, + "type": { + "type": "string" + } + } + } + } + }, + "info": { + "description": "This is automatically collected documentation", + "version": "0.0.0", + "title": "Name of Your Application", + "termsOfService": "", + "contact": { + "email": "your@email.com" + } + } +} From 4374b4e663f83404354aefcf0b14255f6a50b58c Mon Sep 17 00:00:00 2001 From: DenTray Date: Mon, 23 Dec 2024 11:18:08 +0600 Subject: [PATCH 6/7] Update src/Traits/GetDependenciesTrait.php --- src/Traits/GetDependenciesTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Traits/GetDependenciesTrait.php b/src/Traits/GetDependenciesTrait.php index 2922e34..f0d053a 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -20,9 +20,9 @@ public function resolveClassMethodDependencies(object $instance, string $method) protected function transformDependency(ReflectionParameter $parameter) { - $class = $parameter->getType(); + $type = $parameter->getType(); - if (empty($class)) { + if (empty($type)) { return null; } From bc248205281e5e4b13d3ecc57976dde279de904f Mon Sep 17 00:00:00 2001 From: DenTray Date: Mon, 23 Dec 2024 11:18:14 +0600 Subject: [PATCH 7/7] Update src/Traits/GetDependenciesTrait.php --- src/Traits/GetDependenciesTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/GetDependenciesTrait.php b/src/Traits/GetDependenciesTrait.php index f0d053a..c0e8715 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -26,7 +26,7 @@ protected function transformDependency(ReflectionParameter $parameter) return null; } - return interface_exists($class->getName()) ? $this->getClassByInterface($class->getName()) : $class->getName(); + return interface_exists($type->getName()) ? $this->getClassByInterface($type->getName()) : $type->getName(); } protected function getClassByInterface($interfaceName)