Skip to content

Instantly share code, notes, and snippets.

@ilyamon
Last active January 15, 2024 13:51
Show Gist options
  • Save ilyamon/b8e9cffc71fdd607e31a6b8e20794956 to your computer and use it in GitHub Desktop.
Save ilyamon/b8e9cffc71fdd607e31a6b8e20794956 to your computer and use it in GitHub Desktop.
Swagger decorator for API Platform, adding refresh JWT endpoint in documentation
<?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;
}
}
@ilyamon
Copy link
Author

ilyamon commented Jan 12, 2024

Add service in app/config/services.yaml

App\User\Swagger\JwtRefreshDecorator:
    decorates: 'api_platform.openapi.factory'
    arguments: [ '@.inner' ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment