From c8d98cffc87040121d1e132ab19545bad06686f1 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Wed, 15 Jan 2025 20:52:51 +0330 Subject: [PATCH 1/5] Update SwaggerService.php The generateEmptyData and getAppUrl methods updated; --- src/Services/SwaggerService.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index 85ed1b33..c2c106d1 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -137,10 +137,13 @@ protected function generateEmptyData(): array throw new EmptyContactEmailException(); } + + $basePath = ltrim($this->config['basePath'], '/'); + $basePath = blank($basePath) ? '' : '/'.$basePath; $data = [ 'openapi' => self::OPEN_API_VERSION, 'servers' => [ - ['url' => $this->getAppUrl() . $this->config['basePath']], + ['url' => $this->getAppUrl() . $basePath], ], 'paths' => [], 'components' => [ @@ -160,9 +163,13 @@ protected function generateEmptyData(): array protected function getAppUrl(): string { - $url = config('app.url'); + $url = rtrim(config('app.url'), '/'); + + if (!str_starts_with( $url, 'http://') && !str_starts_with($url,'https://')) { + $url = "http://$url"; + } - return str_replace(['http://', 'https://', '/'], '', $url); + return $url; } protected function generateSecurityDefinition(): ?array From 54ab9c46276ce924032440d89d8c698ed9962b73 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Wed, 15 Jan 2025 20:52:59 +0330 Subject: [PATCH 2/5] Update tests; --- .../tmp_data_get_route_parameters_description.json | 2 +- .../tmp_data_request_with_empty_data_and_info.json | 2 +- .../tmp_data_request_with_empty_data_jwt.json | 2 +- .../tmp_data_request_with_empty_data_laravel.json | 2 +- .../tmp_data_request_with_empty_data_query.json | 2 +- .../tmp_data_search_roles_closure_request.json | 2 +- .../SwaggerServiceTest/tmp_data_search_roles_request.json | 2 +- .../tmp_data_search_roles_request_jwt_security.json | 2 +- .../tmp_data_search_roles_request_laravel_security.json | 2 +- .../tmp_data_search_roles_request_query_security.json | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_get_route_parameters_description.json b/tests/fixtures/SwaggerServiceTest/tmp_data_get_route_parameters_description.json index 1590e348..4f177cde 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_get_route_parameters_description.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_get_route_parameters_description.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": { diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_and_info.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_and_info.json index e6bd61b2..9c2985a1 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_and_info.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_and_info.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": [], diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_jwt.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_jwt.json index 016eceeb..2c994d33 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_jwt.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_jwt.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": [], diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_laravel.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_laravel.json index 69e44879..53b5b5a3 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_laravel.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_laravel.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": [], diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json index 70d01625..8b95d9c9 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": [], diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_closure_request.json b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_closure_request.json index a0ec5c3b..d6f077a4 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_closure_request.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_closure_request.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": { diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request.json b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request.json index 813d24d1..064333e6 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": { diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_jwt_security.json b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_jwt_security.json index a6d8d26c..e58ee187 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_jwt_security.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_jwt_security.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": { diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_laravel_security.json b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_laravel_security.json index 8f546f74..d45826fa 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_laravel_security.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_laravel_security.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": { diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_query_security.json b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_query_security.json index 0b154949..3cdbaa57 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_query_security.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_search_roles_request_query_security.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "localhost/" + "url": "http://localhost" } ], "paths": { From fd8f8d4fd88eaec94f5d0c386c72a83f3743d52b Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Wed, 15 Jan 2025 21:26:07 +0330 Subject: [PATCH 3/5] Add test to cover the new line; --- tests/SwaggerServiceTest.php | 6 +++++- .../tmp_data_request_with_empty_data_query.json | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index d4faedf4..3db30419 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -333,21 +333,25 @@ public static function getAddEmptyData(): array [ 'security' => 'laravel', 'savedTmpDataFixture' => 'tmp_data_request_with_empty_data_laravel', + 'appUrl' => 'http://localhost//', ], [ 'security' => 'jwt', 'savedTmpDataFixture' => 'tmp_data_request_with_empty_data_jwt', + 'appUrl' => 'localhost//', ], [ 'security' => 'query', 'savedTmpDataFixture' => 'tmp_data_request_with_empty_data_query', + 'appUrl' => 'https://localhost//', ], ]; } #[DataProvider('getAddEmptyData')] - public function testAddDataRequestWithEmptyDataLaravel(string $security, string $savedTmpDataFixture) + public function testAddDataRequestWithEmptyDataLaravel(string $security, string $savedTmpDataFixture, string $appUrl) { + config()->set('app.url', $appUrl); config([ 'auto-doc.security' => $security, 'auto-doc.security_drivers' => [ diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json index 8b95d9c9..5526f896 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "http://localhost" + "url": "https://localhost" } ], "paths": [], From c041efe3bb025643beecacbe2dfcecf671cffba8 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Tue, 21 Jan 2025 11:52:12 +0330 Subject: [PATCH 4/5] Making suggested changes; --- src/Services/SwaggerService.php | 17 ++--------------- src/Traits/GetDependenciesTrait.php | 2 +- tests/SwaggerServiceTest.php | 6 +----- .../tmp_data_request_with_empty_data_query.json | 2 +- 4 files changed, 5 insertions(+), 22 deletions(-) diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index c2c106d1..67fac694 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -6,6 +6,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Testing\File; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\URL; use Illuminate\Support\Str; use ReflectionClass; use RonasIT\AutoDoc\Exceptions\DocFileNotExistsException; @@ -137,13 +138,10 @@ protected function generateEmptyData(): array throw new EmptyContactEmailException(); } - - $basePath = ltrim($this->config['basePath'], '/'); - $basePath = blank($basePath) ? '' : '/'.$basePath; $data = [ 'openapi' => self::OPEN_API_VERSION, 'servers' => [ - ['url' => $this->getAppUrl() . $basePath], + ['url' => URL::query($this->config['basePath'])], ], 'paths' => [], 'components' => [ @@ -161,17 +159,6 @@ protected function generateEmptyData(): array return $data; } - protected function getAppUrl(): string - { - $url = rtrim(config('app.url'), '/'); - - if (!str_starts_with( $url, 'http://') && !str_starts_with($url,'https://')) { - $url = "http://$url"; - } - - return $url; - } - protected function generateSecurityDefinition(): ?array { if (empty($this->security)) { diff --git a/src/Traits/GetDependenciesTrait.php b/src/Traits/GetDependenciesTrait.php index c0e8715e..998abea6 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -22,7 +22,7 @@ protected function transformDependency(ReflectionParameter $parameter) { $type = $parameter->getType(); - if (empty($type)) { + if (empty($type) || !$type instanceof \ReflectionNamedType) { return null; } diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index 3db30419..d4faedf4 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -333,25 +333,21 @@ public static function getAddEmptyData(): array [ 'security' => 'laravel', 'savedTmpDataFixture' => 'tmp_data_request_with_empty_data_laravel', - 'appUrl' => 'http://localhost//', ], [ 'security' => 'jwt', 'savedTmpDataFixture' => 'tmp_data_request_with_empty_data_jwt', - 'appUrl' => 'localhost//', ], [ 'security' => 'query', 'savedTmpDataFixture' => 'tmp_data_request_with_empty_data_query', - 'appUrl' => 'https://localhost//', ], ]; } #[DataProvider('getAddEmptyData')] - public function testAddDataRequestWithEmptyDataLaravel(string $security, string $savedTmpDataFixture, string $appUrl) + public function testAddDataRequestWithEmptyDataLaravel(string $security, string $savedTmpDataFixture) { - config()->set('app.url', $appUrl); config([ 'auto-doc.security' => $security, 'auto-doc.security_drivers' => [ diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json index 5526f896..8b95d9c9 100644 --- a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_query.json @@ -2,7 +2,7 @@ "openapi": "3.1.0", "servers": [ { - "url": "https://localhost" + "url": "http://localhost" } ], "paths": [], From 4bc635f47fdbd1ed3ea5383ca6ffd38f297ebcfd Mon Sep 17 00:00:00 2001 From: DenTray Date: Wed, 22 Jan 2025 11:59:00 +0600 Subject: [PATCH 5/5] 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 998abea6..c0e8715e 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -22,7 +22,7 @@ protected function transformDependency(ReflectionParameter $parameter) { $type = $parameter->getType(); - if (empty($type) || !$type instanceof \ReflectionNamedType) { + if (empty($type)) { return null; }