Skip to content

Commit 2dc8cf0

Browse files
Feature | Extend Swagger Coverage for controller LanguagesApiController (#358)
* feat: Add OpenAPI documentation to "getAll" method * fix: Open API path to endpoint * fix: Reuse already defined schema and add Paginated Schema
1 parent 5f03463 commit 2dc8cf0

File tree

4 files changed

+65
-30
lines changed

4 files changed

+65
-30
lines changed

app/Http/Controllers/Apis/LanguagesApiController.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212
* limitations under the License.
1313
**/
1414
use App\Models\Foundation\Main\Repositories\ILanguageRepository;
15-
use Illuminate\Support\Facades\Log;
16-
use models\exceptions\EntityNotFoundException;
17-
use models\exceptions\ValidationException;
15+
use Illuminate\Http\Response;
16+
use OpenApi\Attributes as OA;
1817
use utils\PagingResponse;
19-
use Illuminate\Support\Facades\Request;
2018
/**
2119
* Class LanguagesApiController
2220
* @package App\Http\Controllers
2321
*/
2422
final class LanguagesApiController extends JsonController
2523
{
24+
use RequestProcessor;
2625
/**
2726
* @var ILanguageRepository
2827
*/
@@ -37,13 +36,28 @@ public function __construct(ILanguageRepository $language_repository)
3736
$this->language_repository = $language_repository;
3837
}
3938

40-
/**
41-
* @return mixed
42-
*/
43-
public function getAll(){
44-
try {
45-
$languages = $this->language_repository->getAll();
46-
$response = new PagingResponse
39+
#[OA\Get(
40+
path: "/api/public/v1/languages",
41+
description: "Get all available languages with ISO codes",
42+
summary: 'Get all languages',
43+
operationId: 'getAllLanguages',
44+
tags: ['Languages'],
45+
responses: [
46+
new OA\Response(
47+
response: 200,
48+
description: 'Success - Returns paginated list of languages',
49+
content: new OA\JsonContent(ref: '#/components/schemas/PaginatedISOLanguageElementResponseSchema'),
50+
),
51+
new OA\Response(response: Response::HTTP_PRECONDITION_FAILED, description: "Validation Error"),
52+
new OA\Response(response: Response::HTTP_NOT_FOUND, description: "Not Found"),
53+
new OA\Response(response: Response::HTTP_INTERNAL_SERVER_ERROR, description: "Server Error")
54+
]
55+
)]
56+
public function getAll()
57+
{
58+
return $this->processRequest(function () {
59+
$languages = $this->language_repository->getAll();
60+
$response = new PagingResponse
4761
(
4862
count($languages),
4963
count($languages),
@@ -52,20 +66,8 @@ public function getAll(){
5266
$languages
5367
);
5468

55-
return $this->ok($response->toArray($expand = Request::input('expand','')));
56-
}
57-
catch (ValidationException $ex1) {
58-
Log::warning($ex1);
59-
return $this->error412(array($ex1->getMessage()));
60-
}
61-
catch(EntityNotFoundException $ex2)
62-
{
63-
Log::warning($ex2);
64-
return $this->error404(array('message'=> $ex2->getMessage()));
65-
}
66-
catch (\Exception $ex) {
67-
Log::error($ex);
68-
return $this->error500($ex);
69-
}
69+
return $this->ok($response->toArray());
70+
});
7071
}
71-
}
72+
73+
}

app/Swagger/Countries.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'name' => new OA\Property(property: 'name', type: 'string', example: 'United States')
1111
]
1212
)]
13-
class ISOElementSchema {};
13+
class ISOCountryElementSchema {};
1414

1515
#[OA\Schema(
1616
schema: 'PaginatedISOCountryElementResponseSchema',
@@ -29,4 +29,4 @@ class ISOElementSchema {};
2929
)
3030
]
3131
)]
32-
class PaginatedISOElementResponseSchema {};
32+
class PaginatedISOCountryElementResponseSchema {};

app/Swagger/Languages.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php namespace App\Swagger\schemas;
2+
3+
use OpenApi\Attributes as OA;
4+
5+
#[OA\Schema(
6+
schema: 'ISOLanguageElementSchema',
7+
type: 'object',
8+
properties: [
9+
'iso_code' => new OA\Property(property: 'iso_code', type: 'string', example: 'US'),
10+
'name' => new OA\Property(property: 'name', type: 'string', example: 'United States')
11+
]
12+
)]
13+
class ISOLanguageElementSchema {};
14+
15+
#[OA\Schema(
16+
schema: 'PaginatedISOLanguageElementResponseSchema',
17+
type: 'object',
18+
allOf: [
19+
new OA\Schema(ref: '#/components/schemas/PaginateDataSchemaResponse'),
20+
new OA\Schema(
21+
type: 'object',
22+
properties: [
23+
new OA\Property(
24+
property: 'data',
25+
type: 'array',
26+
items: new OA\Items(ref: "#/components/schemas/ISOLanguageElementSchema")
27+
)
28+
]
29+
)
30+
]
31+
)]
32+
class PaginatedISOLanguageElementResponseSchema {};

start_local_server.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export DOCKER_SCAN_SUGGEST=false
66
docker compose up -d app
77
# Run all setup commands inside the running container
88
docker compose exec app composer install
9+
docker compose exec app composer dump-autoload -o
910
docker compose exec app php artisan db:create_test_db --schema=config
1011
docker compose exec app php artisan db:create_test_db --schema=model
1112
docker compose exec app php artisan doctrine:migrations:migrate --no-interaction --em=config
@@ -17,4 +18,4 @@ docker compose exec app php artisan l5-swagger:generate
1718
# Now bring up all remaining services
1819
docker compose up -d
1920
# Open shell as appuser
20-
docker compose exec app /bin/bash
21+
docker compose exec app /bin/bash

0 commit comments

Comments
 (0)