Skip to content

Commit 5f03463

Browse files
Feature | Extend Swagger Coverage for controller CountriesApiController (#357)
* feat: Add OpenAPI documentation to "getAll" method * fix: Open API path to endpoint * fix: Use only one tag * chore: Add ISOElementSchema * fix: Reuse already defined schema and add Paginated Schema
1 parent ee9422d commit 5f03463

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

app/Http/Controllers/Apis/CountriesApiController.php

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,49 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
1415
use App\Models\Foundation\Main\CountryCodes;
15-
use Illuminate\Support\Facades\Log;
16-
use models\exceptions\EntityNotFoundException;
17-
use models\exceptions\ValidationException;
16+
use Illuminate\Http\Response;
17+
use OpenApi\Attributes as OA;
1818
use utils\PagingResponse;
19-
use Illuminate\Support\Facades\Request;
19+
2020
/**
2121
* Class CountriesApiController
2222
* @package App\Http\Controllers
2323
*/
2424
final class CountriesApiController extends JsonController
2525
{
26-
/**
27-
* @return mixed
28-
*/
29-
public function getAll(){
30-
try {
26+
use RequestProcessor;
27+
28+
#[OA\Get(
29+
path: "/api/public/v1/countries",
30+
description: "Get all countries with ISO codes",
31+
summary: 'Get all countries',
32+
operationId: 'getAllCountries',
33+
tags: ['Countries'],
34+
responses: [
35+
new OA\Response(
36+
response: 200,
37+
description: 'Success - Returns paginated list of countries',
38+
content: new OA\JsonContent(ref: '#/components/schemas/PaginatedISOCountryElementResponseSchema'),
39+
),
40+
new OA\Response(response: Response::HTTP_PRECONDITION_FAILED, description: "Validation Error"),
41+
new OA\Response(response: Response::HTTP_NOT_FOUND, description: "Not Found"),
42+
new OA\Response(response: Response::HTTP_INTERNAL_SERVER_ERROR, description: "Server Error")
43+
]
44+
)]
45+
public function getAll()
46+
{
47+
return $this->processRequest(function () {
3148
$countries = [];
32-
foreach(CountryCodes::$iso_3166_countryCodes as $iso_code => $name){
49+
foreach (CountryCodes::$iso_3166_countryCodes as $iso_code => $name) {
3350
$countries[] = [
3451
'iso_code' => $iso_code,
3552
'name' => $name,
3653
];
3754
}
3855

39-
$response = new PagingResponse
56+
$response = new PagingResponse
4057
(
4158
count($countries),
4259
count($countries),
@@ -45,20 +62,8 @@ public function getAll(){
4562
$countries
4663
);
4764

48-
return $this->ok($response->toArray($expand = Request::input('expand','')));
49-
}
50-
catch (ValidationException $ex1) {
51-
Log::warning($ex1);
52-
return $this->error412(array($ex1->getMessage()));
53-
}
54-
catch(EntityNotFoundException $ex2)
55-
{
56-
Log::warning($ex2);
57-
return $this->error404(array('message'=> $ex2->getMessage()));
58-
}
59-
catch (\Exception $ex) {
60-
Log::error($ex);
61-
return $this->error500($ex);
62-
}
65+
return $this->ok($response->toArray());
66+
});
67+
6368
}
64-
}
69+
}

app/Swagger/Countries.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: 'ISOCountryElementSchema',
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 ISOElementSchema {};
14+
15+
#[OA\Schema(
16+
schema: 'PaginatedISOCountryElementResponseSchema',
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/ISOCountryElementSchema")
27+
)
28+
]
29+
)
30+
]
31+
)]
32+
class PaginatedISOElementResponseSchema {};

app/Swagger/schemas.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ class RSVPUpdateRequestSchema_{
340340

341341
}
342342

343-
344343
#[OA\Schema(
345344
schema: 'RSVPAdminAddRequest',
346345
type: 'object',

0 commit comments

Comments
 (0)