Skip to content

Commit 6dd1d17

Browse files
authored
Merge pull request #2592 from tarlepp/feat/open-api-annotations-to-attributes
Feat - Use Open API attributes instead of annotations
2 parents 1d7572d + ace5bf9 commit 6dd1d17

27 files changed

+817
-754
lines changed

src/Controller/HealthzController.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use App\Rest\Interfaces\ResponseHandlerInterface;
1212
use App\Rest\ResponseHandler;
1313
use App\Utils\HealthzService;
14-
use OpenApi\Annotations as OA;
14+
use OpenApi\Attributes as OA;
15+
use OpenApi\Attributes\JsonContent;
16+
use OpenApi\Attributes\Property;
1517
use Symfony\Component\HttpFoundation\Request;
1618
use Symfony\Component\HttpFoundation\Response;
1719
use Symfony\Component\HttpKernel\Attribute\AsController;
@@ -39,27 +41,34 @@ public function __construct(
3941
*
4042
* @see https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/
4143
*
42-
* @OA\Get(
43-
* operationId="healthz",
44-
* responses={
45-
* @OA\Response(
46-
* response=200,
47-
* description="success",
48-
* @OA\Schema(
49-
* type="object",
50-
* example={"timestamp": "2018-01-01T13:08:05+00:00"},
51-
* @OA\Property(property="timestamp", type="string"),
52-
* ),
53-
* ),
54-
* },
55-
* )
56-
*
5744
* @throws Throwable
5845
*/
5946
#[Route(
6047
path: '/healthz',
6148
methods: [Request::METHOD_GET],
6249
)]
50+
#[OA\Get(
51+
operationId: 'healthz',
52+
responses: [
53+
new OA\Response(
54+
response: 200,
55+
description: 'success',
56+
content: new JsonContent(
57+
properties: [
58+
new Property(
59+
property: 'timestamp',
60+
description: 'Timestamp when health check was performed',
61+
type: 'string',
62+
),
63+
],
64+
type: 'object',
65+
example: [
66+
'timestamp' => '2018-01-01T13:08:05+00:00',
67+
],
68+
),
69+
),
70+
],
71+
)]
6372
public function __invoke(Request $request): Response
6473
{
6574
return $this->responseHandler->createResponse(

src/Controller/VersionController.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace App\Controller;
1010

1111
use App\Service\Version;
12-
use OpenApi\Annotations as OA;
12+
use OpenApi\Attributes as OA;
13+
use OpenApi\Attributes\JsonContent;
14+
use OpenApi\Attributes\Property;
1315
use Symfony\Component\HttpFoundation\JsonResponse;
1416
use Symfony\Component\HttpFoundation\Request;
1517
use Symfony\Component\HttpKernel\Attribute\AsController;
@@ -30,27 +32,34 @@ public function __construct(
3032
}
3133

3234
/**
33-
* Route for get API version.
34-
*
35-
* @OA\Get(
36-
* operationId="version",
37-
* responses={
38-
* @OA\Response(
39-
* response=200,
40-
* description="success",
41-
* @OA\Schema(
42-
* type="object",
43-
* example={"version": "1.2.3"},
44-
* @OA\Property(property="version", type="string", description="Version number"),
45-
* ),
46-
* ),
47-
* },
48-
* )
35+
* Route to get API version.
4936
*/
5037
#[Route(
5138
path: '/version',
5239
methods: [Request::METHOD_GET],
5340
)]
41+
#[OA\Get(
42+
operationId: 'version',
43+
responses: [
44+
new OA\Response(
45+
response: 200,
46+
description: 'success',
47+
content: new JsonContent(
48+
properties: [
49+
new Property(
50+
property: 'version',
51+
description: 'Version number of the API in semver format',
52+
type: 'string',
53+
),
54+
],
55+
type: 'object',
56+
example: [
57+
'version' => '1.2.3',
58+
],
59+
),
60+
),
61+
],
62+
)]
5463
public function __invoke(): JsonResponse
5564
{
5665
return new JsonResponse([

src/Controller/v1/ApiKey/ApiKeyController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use App\Resource\ApiKeyResource;
1515
use App\Rest\Controller;
1616
use App\Rest\Traits\Actions;
17-
use OpenApi\Annotations as OA;
17+
use OpenApi\Attributes as OA;
1818
use Symfony\Component\HttpKernel\Attribute\AsController;
1919
use Symfony\Component\Routing\Annotation\Route;
2020
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
@@ -23,8 +23,6 @@
2323
/**
2424
* Class ApiKeyController
2525
*
26-
* @OA\Tag(name="ApiKey Management")
27-
*
2826
* @package App\Controller
2927
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
3028
*
@@ -35,6 +33,7 @@
3533
path: '/v1/api_key',
3634
)]
3735
#[IsGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY)]
36+
#[OA\Tag(name: 'ApiKey Management')]
3837
class ApiKeyController extends Controller
3938
{
4039
use Actions\Root\CountAction;

src/Controller/v1/Auth/GetTokenController.php

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
use App\Utils\JSON;
1212
use JsonException;
13-
use OpenApi\Annotations as OA;
13+
use OpenApi\Attributes as OA;
14+
use OpenApi\Attributes\JsonContent;
15+
use OpenApi\Attributes\Property;
1416
use Symfony\Component\HttpFoundation\Request;
1517
use Symfony\Component\HttpFoundation\Response;
1618
use Symfony\Component\HttpKernel\Attribute\AsController;
@@ -30,52 +32,77 @@ class GetTokenController
3032
/**
3133
* Endpoint action to get user Json Web Token (JWT) for authentication.
3234
*
33-
* @OA\RequestBody(
34-
* request="body",
35-
* description="Credentials object",
36-
* required=true,
37-
* @OA\Schema(
38-
* example={"username": "username", "password": "password"},
39-
* )
40-
* )
41-
* @OA\Response(
42-
* response=200,
43-
* description="JSON Web Token for user",
44-
* @OA\Schema(
45-
* type="object",
46-
* example={"token": "_json_web_token_"},
47-
* @OA\Property(property="token", type="string", description="Json Web Token"),
48-
* ),
49-
* )
50-
* @OA\Response(
51-
* response=400,
52-
* description="Bad Request",
53-
* @OA\Schema(
54-
* type="object",
55-
* example={"code": 400, "message": "Bad Request"},
56-
* @OA\Property(property="code", type="integer", description="Error code"),
57-
* @OA\Property(property="message", type="string", description="Error description"),
58-
* ),
59-
* )
60-
* @OA\Response(
61-
* response=401,
62-
* description="Unauthorized",
63-
* @OA\Schema(
64-
* type="object",
65-
* example={"code": 401, "message": "Bad credentials"},
66-
* @OA\Property(property="code", type="integer", description="Error code"),
67-
* @OA\Property(property="message", type="string", description="Error description"),
68-
* ),
69-
* )
70-
* @OA\Tag(name="Authentication")
71-
*
7235
* @throws HttpException
7336
* @throws JsonException
7437
*/
7538
#[Route(
7639
path: '/v1/auth/get_token',
7740
methods: [Request::METHOD_POST],
7841
)]
42+
#[OA\RequestBody(
43+
request: 'body',
44+
description: 'Credentials object',
45+
required: true,
46+
content: new JsonContent(
47+
properties: [
48+
new Property(property: 'username', type: 'string'),
49+
new Property(property: 'password', type: 'string'),
50+
],
51+
type: 'object',
52+
example: [
53+
'username' => 'username',
54+
'password' => 'password',
55+
],
56+
),
57+
)]
58+
#[OA\Response(
59+
response: 200,
60+
description: 'JSON Web Token for user',
61+
content: new JsonContent(
62+
properties: [
63+
new Property(
64+
property: 'token',
65+
description: 'Json Web Token',
66+
type: 'string',
67+
),
68+
],
69+
type: 'object',
70+
example: [
71+
'token' => '_json_web_token_',
72+
],
73+
),
74+
)]
75+
#[OA\Response(
76+
response: 400,
77+
description: 'Bad Request',
78+
content: new JsonContent(
79+
properties: [
80+
new Property(property: 'code', type: 'integer'),
81+
new Property(property: 'message', type: 'string'),
82+
],
83+
type: 'object',
84+
example: [
85+
'code' => 400,
86+
'message' => 'Bad Request',
87+
],
88+
),
89+
)]
90+
#[OA\Response(
91+
response: 401,
92+
description: 'Unauthorized',
93+
content: new JsonContent(
94+
properties: [
95+
new Property(property: 'code', type: 'integer'),
96+
new Property(property: 'message', type: 'string'),
97+
],
98+
type: 'object',
99+
example: [
100+
'code' => 401,
101+
'message' => 'Bad credentials',
102+
],
103+
),
104+
)]
105+
#[OA\Tag(name: 'Authentication')]
79106
public function __invoke(): never
80107
{
81108
$message = sprintf(

src/Controller/v1/Localization/LanguageController.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
namespace App\Controller\v1\Localization;
1010

1111
use App\Service\Localization;
12-
use OpenApi\Annotations as OA;
12+
use OpenApi\Attributes as OA;
13+
use OpenApi\Attributes\JsonContent;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpKernel\Attribute\AsController;
@@ -18,12 +19,11 @@
1819
/**
1920
* Class LanguageController
2021
*
21-
* @OA\Tag(name="Localization")
22-
*
2322
* @package App\Controller\v1\Localization
2423
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
2524
*/
2625
#[AsController]
26+
#[OA\Tag(name: 'Localization')]
2727
class LanguageController
2828
{
2929
public function __construct(
@@ -34,21 +34,23 @@ public function __construct(
3434
/**
3535
* Endpoint action to get supported languages. This is for use to choose
3636
* what language your frontend application can use within its translations.
37-
*
38-
* @OA\Response(
39-
* response=200,
40-
* description="List of language strings.",
41-
* @OA\Schema(
42-
* type="array",
43-
* example={"en","fi"},
44-
* @OA\Items(type="string"),
45-
* ),
46-
* )
4737
*/
4838
#[Route(
4939
path: '/v1/localization/language',
5040
methods: [Request::METHOD_GET],
5141
)]
42+
#[OA\Response(
43+
response: 200,
44+
description: 'List of language strings.',
45+
content: new JsonContent(
46+
type: 'array',
47+
items: new OA\Items(
48+
type: 'string',
49+
example: 'en',
50+
),
51+
example: ['en', 'fi'],
52+
),
53+
)]
5254
public function __invoke(): JsonResponse
5355
{
5456
return new JsonResponse($this->localization->getLanguages());

src/Controller/v1/Localization/LocaleController.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
namespace App\Controller\v1\Localization;
1010

1111
use App\Service\Localization;
12-
use OpenApi\Annotations as OA;
12+
use OpenApi\Attributes as OA;
13+
use OpenApi\Attributes\JsonContent;
1314
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpKernel\Attribute\AsController;
@@ -18,12 +19,11 @@
1819
/**
1920
* Class LocaleController
2021
*
21-
* @OA\Tag(name="Localization")
22-
*
2322
* @package App\Controller\v1\Localization
2423
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
2524
*/
2625
#[AsController]
26+
#[OA\Tag(name: 'Localization')]
2727
class LocaleController
2828
{
2929
public function __construct(
@@ -35,21 +35,23 @@ public function __construct(
3535
* Endpoint action to get supported locales. This is for use to choose what
3636
* locale your frontend application can use within its number, time, date,
3737
* datetime, etc. formatting.
38-
*
39-
* @OA\Response(
40-
* response=200,
41-
* description="List of locale strings.",
42-
* @OA\Schema(
43-
* type="array",
44-
* example={"en","fi"},
45-
* @OA\Items(type="string"),
46-
* ),
47-
* )
4838
*/
4939
#[Route(
5040
path: '/v1/localization/locale',
5141
methods: [Request::METHOD_GET],
5242
)]
43+
#[OA\Response(
44+
response: 200,
45+
description: 'List of locale strings.',
46+
content: new JsonContent(
47+
type: 'array',
48+
items: new OA\Items(
49+
type: 'string',
50+
example: 'en',
51+
),
52+
example: ['en', 'fi'],
53+
),
54+
)]
5355
public function __invoke(): JsonResponse
5456
{
5557
return new JsonResponse($this->localization->getLocales());

0 commit comments

Comments
 (0)