Created
August 27, 2020 22:45
-
-
Save yusufbiberoglu/cd0325f7d48feae097ac770d3d40b141 to your computer and use it in GitHub Desktop.
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 | |
// src/Swagger/SwaggerDecorator.php | |
declare(strict_types=1); | |
namespace App\Swagger; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |
final class SwaggerDecorator implements NormalizerInterface | |
{ | |
private NormalizerInterface $decorated; | |
public function __construct(NormalizerInterface $decorated) | |
{ | |
$this->decorated = $decorated; | |
} | |
public function supportsNormalization($data, string $format = null): bool | |
{ | |
return $this->decorated->supportsNormalization($data, $format); | |
} | |
public function normalize($object, string $format = null, array $context = []) | |
{ | |
$docs = $this->decorated->normalize($object, $format, $context); | |
$docs['components']['schemas']['Token'] = [ | |
'type' => 'object', | |
'properties' => [ | |
'token' => [ | |
'type' => 'string', | |
'readOnly' => true, | |
], | |
], | |
]; | |
$docs['components']['schemas']['Credentials'] = [ | |
'type' => 'object', | |
'properties' => [ | |
'username' => [ | |
'type' => 'string', | |
], | |
'password' => [ | |
'type' => 'string', | |
], | |
], | |
]; | |
$tokenDocumentation = [ | |
'paths' => [ | |
'/authentication' => [ | |
'post' => [ | |
'tags' => ['Token'], | |
'operationId' => 'postCredentialsItem', | |
'summary' => 'Get JWT token to login.', | |
'requestBody' => [ | |
'description' => 'Create new JWT Token', | |
'content' => [ | |
'application/json' => [ | |
'schema' => [ | |
'$ref' => '#/components/schemas/Credentials', | |
], | |
], | |
], | |
], | |
'responses' => [ | |
Response::HTTP_OK => [ | |
'description' => 'Get JWT token', | |
'content' => [ | |
'application/json' => [ | |
'schema' => [ | |
'$ref' => '#/components/schemas/Token', | |
], | |
], | |
], | |
], | |
], | |
], | |
], | |
], | |
]; | |
return array_merge_recursive($docs, $tokenDocumentation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment