@@ -202,86 +202,83 @@ declare(strict_types=1);
202
202
203
203
namespace App\Swagger;
204
204
205
+ use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
206
+ use ApiPlatform\Core\OpenApi\OpenApi;
207
+ use ApiPlatform\Core\OpenApi\Model;
208
+ use ArrayObject;
205
209
use Symfony\Component\HttpFoundation\Response;
206
- use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
207
210
208
- final class SwaggerDecorator implements NormalizerInterface
211
+ final class JWTSwaggerDecorator implements OpenApiFactoryInterface
209
212
{
210
- private NormalizerInterface $decorated;
213
+ private OpenApiFactoryInterface $decorated;
211
214
212
- public function __construct(NormalizerInterface $decorated)
215
+ public function __construct(OpenApiFactoryInterface $decorated)
213
216
{
214
217
$this->decorated = $decorated;
215
218
}
216
219
217
- public function supportsNormalization($data, string $format = null ): bool
220
+ public function __invoke(array $context = [] ): OpenApi
218
221
{
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);
225
223
226
- $docs['components']['schemas'][' Token'] = [
224
+ $openApi->getComponents()->getSchemas()[' Token'] = new ArrayObject( [
227
225
'type' => 'object',
228
226
'properties' => [
229
227
'token' => [
230
228
'type' => 'string',
231
229
'readOnly' => true,
232
230
],
233
231
],
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
+
240
+ ],
241
+ 'password' => [
242
+ 'type' => 'string',
243
+ 'example' => 'apassword',
244
+ ],
246
245
],
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',
265
260
],
266
261
],
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',
278
272
],
279
273
],
280
- ],
281
- ],
282
- ];
274
+ ]),
275
+ ),
276
+
277
+ )
278
+ );
279
+ $openApi->getPaths()->addPath('/authentication_token', $pathItem);
283
280
284
- return array_merge_recursive($docs, $tokenDocumentation) ;
281
+ return $openApi ;
285
282
}
286
283
}
287
284
```
@@ -292,9 +289,9 @@ And register this service in `config/services.yaml`:
292
289
services :
293
290
# ...
294
291
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']
298
295
autoconfigure : false
299
296
` ` `
300
297
0 commit comments