Skip to content

Commit ee21499

Browse files
garciasdosdunglas
authored andcommitted
Update JWT Documentation
Update with the new OpenApi documentation way. It works with Api Platform 2.6.0 beta 1
1 parent 04de0a2 commit ee21499

File tree

1 file changed

+57
-60
lines changed

1 file changed

+57
-60
lines changed

core/jwt.md

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -202,86 +202,83 @@ declare(strict_types=1);
202202
203203
namespace App\Swagger;
204204
205+
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
206+
use ApiPlatform\Core\OpenApi\OpenApi;
207+
use ApiPlatform\Core\OpenApi\Model;
208+
use ArrayObject;
205209
use Symfony\Component\HttpFoundation\Response;
206-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
207210
208-
final class SwaggerDecorator implements NormalizerInterface
211+
final class JWTSwaggerDecorator implements OpenApiFactoryInterface
209212
{
210-
private NormalizerInterface $decorated;
213+
private OpenApiFactoryInterface $decorated;
211214
212-
public function __construct(NormalizerInterface $decorated)
215+
public function __construct(OpenApiFactoryInterface $decorated)
213216
{
214217
$this->decorated = $decorated;
215218
}
216219
217-
public function supportsNormalization($data, string $format = null): bool
220+
public function __invoke(array $context = []): OpenApi
218221
{
219-
return $this->decorated->supportsNormalization($data, $format);
220-
}
221-
222-
public function normalize($object, string $format = null, array $context = [])
223-
{
224-
$docs = $this->decorated->normalize($object, $format, $context);
222+
$openApi = $this->decorated->__invoke($context);
225223
226-
$docs['components']['schemas']['Token'] = [
224+
$openApi->getComponents()->getSchemas()['Token'] = new ArrayObject([
227225
'type' => 'object',
228226
'properties' => [
229227
'token' => [
230228
'type' => 'string',
231229
'readOnly' => true,
232230
],
233231
],
234-
];
235-
236-
$docs['components']['schemas']['Credentials'] = [
237-
'type' => 'object',
238-
'properties' => [
239-
'username' => [
240-
'type' => 'string',
241-
'example' => 'api',
242-
],
243-
'password' => [
244-
'type' => 'string',
245-
'example' => 'api',
232+
]);
233+
234+
$openApi->getComponents()->getSchemas()['Credentials'] = new ArrayObject([
235+
'type' => 'object',
236+
'properties' => [
237+
'email' => [
238+
'type' => 'string',
239+
'example' => '[email protected]',
240+
],
241+
'password' => [
242+
'type' => 'string',
243+
'example' => 'apassword',
244+
],
246245
],
247-
],
248-
];
249-
250-
$tokenDocumentation = [
251-
'paths' => [
252-
'/authentication_token' => [
253-
'post' => [
254-
'tags' => ['Token'],
255-
'operationId' => 'postCredentialsItem',
256-
'summary' => 'Get JWT token to login.',
257-
'requestBody' => [
258-
'description' => 'Create new JWT Token',
259-
'content' => [
260-
'application/json' => [
261-
'schema' => [
262-
'$ref' => '#/components/schemas/Credentials',
263-
],
264-
],
246+
]
247+
);
248+
249+
$pathItem = new Model\PathItem(
250+
ref: 'JWT Token',
251+
post: new Model\Operation(
252+
operationId: 'postCredentialsItem',
253+
responses: [
254+
Response::HTTP_OK => [
255+
'description' => 'Get JWT token',
256+
'content' => [
257+
'application/json' => [
258+
'schema' => [
259+
'$ref' => '#/components/schemas/Token',
265260
],
266261
],
267-
'responses' => [
268-
Response::HTTP_OK => [
269-
'description' => 'Get JWT token',
270-
'content' => [
271-
'application/json' => [
272-
'schema' => [
273-
'$ref' => '#/components/schemas/Token',
274-
],
275-
],
276-
],
277-
],
262+
],
263+
]],
264+
summary: 'Get JWT token to login.',
265+
requestBody: new Model\RequestBody(
266+
description: 'Generate new JWT Token',
267+
content: new ArrayObject(
268+
[
269+
'application/json' => [
270+
'schema' => [
271+
'$ref' => '#/components/schemas/Credentials',
278272
],
279273
],
280-
],
281-
],
282-
];
274+
]),
275+
),
276+
277+
)
278+
);
279+
$openApi->getPaths()->addPath('/authentication_token', $pathItem);
283280
284-
return array_merge_recursive($docs, $tokenDocumentation);
281+
return $openApi;
285282
}
286283
}
287284
```
@@ -292,9 +289,9 @@ And register this service in `config/services.yaml`:
292289
services:
293290
# ...
294291

295-
App\Swagger\SwaggerDecorator:
296-
decorates: 'api_platform.swagger.normalizer.documentation'
297-
arguments: ['@App\Swagger\SwaggerDecorator.inner']
292+
App\Swagger\JWTSwaggerDecorator:
293+
decorates: 'api_platform.openapi.factory'
294+
arguments: ['@App\Swagger\JWTSwaggerDecorator.inner']
298295
autoconfigure: false
299296
```
300297

0 commit comments

Comments
 (0)