diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index 5c8cf87..85ed1b3 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -646,12 +646,12 @@ public function getConcreteRequest() $class = $explodedController[0]; $method = $explodedController[1]; - $instance = app($class); - $route = $this->request->route(); + if (!method_exists($class, $method)) { + return null; + } $parameters = $this->resolveClassMethodDependencies( - $route->parametersWithoutNulls(), - $instance, + app($class), $method ); diff --git a/src/Traits/GetDependenciesTrait.php b/src/Traits/GetDependenciesTrait.php index 9db9dfe..c0e8715 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -11,33 +11,22 @@ trait GetDependenciesTrait { - protected function resolveClassMethodDependencies(array $parameters, $instance, $method) - { - if (!method_exists($instance, $method)) { - return $parameters; - } - - 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(); + $type = $parameter->getType(); - if (empty($class)) { + if (empty($type)) { return null; } - return interface_exists($class->name) ? $this->getClassByInterface($class->name) : $class->name; + return interface_exists($type->getName()) ? $this->getClassByInterface($type->getName()) : $type->getName(); } protected function getClassByInterface($interfaceName) 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" + } + } +}