Skip to content

Instantly share code, notes, and snippets.

@yusufbiberoglu
Created August 27, 2020 22:45
Show Gist options
  • Save yusufbiberoglu/cd0325f7d48feae097ac770d3d40b141 to your computer and use it in GitHub Desktop.
Save yusufbiberoglu/cd0325f7d48feae097ac770d3d40b141 to your computer and use it in GitHub Desktop.
<?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