Last active
January 15, 2024 13:51
-
-
Save ilyamon/b8e9cffc71fdd607e31a6b8e20794956 to your computer and use it in GitHub Desktop.
Swagger decorator for API Platform, adding refresh JWT endpoint in documentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\User\Swagger; | |
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; | |
use ApiPlatform\OpenApi\Model; | |
use ApiPlatform\OpenApi\OpenApi; | |
final class JwtRefreshDecorator implements OpenApiFactoryInterface | |
{ | |
public function __construct(private OpenApiFactoryInterface $decorated) | |
{ | |
} | |
public function __invoke(array $context = []): OpenApi | |
{ | |
$openApi = ($this->decorated)($context); | |
$schemas = $openApi->getComponents()->getSchemas(); | |
$schemas['jwtRefreshCredentials'] = new \ArrayObject([ | |
'type' => 'object', | |
'properties' => [ | |
'user_id' => [ | |
'type' => 'string' | |
], | |
'refresh_token' => [ | |
'type' => 'string' | |
], | |
], | |
]); | |
$schemas['jwtResponses'] = new \ArrayObject([ | |
'type' => 'object', | |
'properties' => [ | |
'token' => [ | |
'type' => 'string', | |
'readOnly' => true, | |
], | |
'refresh_token' => [ | |
'type' => 'string', | |
'readOnly' => true, | |
], | |
], | |
]); | |
$jwtRefresh = new Model\PathItem( | |
ref: 'Refresh the JWT.', | |
summary: 'Refresh the JWT.', | |
description: 'Refresh the JWT.', | |
post: new Model\Operation( | |
operationId: 'postJwtRefresh', | |
tags: ['Login Check'], | |
responses: [ | |
'200' => [ | |
'description' => 'New JWT.', | |
'content' => [ | |
'application/json' => [ | |
'schema' => [ | |
'$ref' => '#/components/schemas/jwtResponses', | |
], | |
], | |
], | |
], | |
], | |
summary: 'Refresh the JWT.', | |
description: 'Refresh the JWT.', | |
requestBody: new Model\RequestBody( | |
description: 'Generate new JWT.', | |
content: new \ArrayObject([ | |
'application/json' => [ | |
'schema' => [ | |
'$ref' => '#/components/schemas/jwtRefreshCredentials', | |
], | |
], | |
]), | |
required: true | |
) | |
) | |
); | |
$openApi->getPaths()->addPath(path: '/api/jwt-refresh', pathItem: $jwtRefresh); | |
return $openApi; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add service in app/config/services.yaml