Skip to content

Commit

Permalink
Refactoring OpenApi decorator for api-platform (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
acassan authored Jul 29, 2021
1 parent d232bc6 commit 037580e
Showing 1 changed file with 115 additions and 111 deletions.
226 changes: 115 additions & 111 deletions Bridge/ApiPlatform/OpenApi/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace CoopTilleuls\ForgotPasswordBundle\Bridge\ApiPlatform\OpenApi;

use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\Core\OpenApi\Model\Components;
use ApiPlatform\Core\OpenApi\Model\Operation;
use ApiPlatform\Core\OpenApi\Model\PathItem;
use ApiPlatform\Core\OpenApi\Model\RequestBody;
Expand All @@ -38,139 +37,144 @@ public function __construct(OpenApiFactoryInterface $decorated)
public function __invoke(array $context = []): OpenApi
{
$openApi = ($this->decorated)($context);
$paths = $openApi->withTags(['name' => 'Forgot password'])->getPaths();
$schemas = $openApi->getComponents()->getSchemas();

// Add POST /forgot-password/ path
/** @var PathItem $path */
$path = $paths->getPath('/forgot-password/') ?: new PathItem();
$paths->addPath('/forgot-password/', $path->withPost(new Operation(
'postForgotPassword',
['Forgot password'],
[
204 => [
'description' => 'Valid email address, no matter if user exists or not',
$schemas['ForgotPassword:reset'] = new \ArrayObject([
'type' => 'object',
'required' => ['password'],
'properties' => [
'password' => [
'type' => 'string',
],
400 => [
'description' => 'Missing email parameter or invalid format',
],
]);

$schemas['ForgotPassword:validate'] = new \ArrayObject([
'type' => 'object',
]);

$schemas['ForgotPassword:request'] = new \ArrayObject([
'type' => 'object',
'required' => ['email'],
'properties' => [
'email' => [
'type' => 'string',
],
],
'Generates a token and send email',
'',
]);

$pathItem = new PathItem(
'ForgotPassword',
null,
null,
[],
new RequestBody('Request a new password', new \ArrayObject([
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/ForgotPassword:request',
null,
null,
new Operation(
'postForgotPassword',
['Forgot password'],
[
204 => [
'description' => 'Valid email address, no matter if user exists or not',
],
],
]))
)));
$openApi->withComponents(new Components(new \ArrayObject([
'ForgotPassword:request' => [
'type' => 'object',
'description' => '',
'required' => ['email'],
'properties' => [
'email' => [
'type' => 'string',
400 => [
'description' => 'Missing email parameter or invalid format',
],
],
],
])));

// Add GET /forgot-password/{token} path
/** @var PathItem $path */
$path = $paths->getPath('/forgot-password/{token}') ?: new PathItem();
$paths->addPath('/forgot-password/', $path->withGet(new Operation(
'getForgotPassword',
['Forgot password'],
[
200 => [
'description' => 'Authenticated user',
'content' => [
'Generates a token and send email',
'',
null,
[],
new RequestBody(
'Request a new password',
new \ArrayObject([
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/ForgotPassword:validate',
'$ref' => '#/components/schemas/ForgotPassword:request',
],
],
],
],
404 => [
'description' => 'Token not found or expired',
],
],
'Validates token',
'',
])
)
)
);
$openApi->getPaths()->addPath('/forgot_password/', $pathItem);

$pathItem = new PathItem(
'ForgotPassword',
null,
[
'',
new Operation(
'getForgotPassword',
['Forgot password'],
[
'name' => 'token',
'in' => 'path',
'required' => true,
'schema' => [
'type' => 'string',
200 => [
'description' => 'Authenticated user',
'content' => [
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/ForgotPassword:validate',
],
],
],
],
404 => [
'description' => 'Token not found or expired',
],
],
]
)));
$openApi->withComponents(new Components(new \ArrayObject([
'ForgotPassword:validate' => [
'type' => 'object',
'description' => '',
],
])));

// Add POST /forgot-password/{token} path
/** @var PathItem $path */
$path = $paths->getPath('/forgot-password/{token}') ?: new PathItem();
$paths->addPath('/forgot-password/', $path->withPost(new Operation(
'postForgotPasswordToken',
['Forgot password'],
[
204 => [
'description' => 'Email address format valid, no matter if user exists or not',
],
400 => [
'description' => 'Missing password parameter',
],
404 => [
'description' => 'Token not found',
],
],
'Resets user password from token',
'',
'Validates token',
'',
null,
[
[
'name' => 'token',
'in' => 'path',
'required' => true,
'schema' => [
'type' => 'string',
],
],
]
),
null,
[
new Operation(
'postForgotPasswordToken',
['Forgot password'],
[
'name' => 'token',
'in' => 'path',
'required' => true,
'schema' => [
'type' => 'string',
204 => [
'description' => 'Email address format valid, no matter if user exists or not',
],
],
],
new RequestBody('Reset password', new \ArrayObject([
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/ForgotPassword:reset',
400 => [
'description' => 'Missing password parameter',
],
404 => [
'description' => 'Token not found',
],
],
]))
)));
$openApi->withComponents(new Components(new \ArrayObject([
'ForgotPassword:reset' => [
'type' => 'object',
'description' => '',
'required' => ['password'],
'properties' => [
'password' => [
'type' => 'string',
'Resets user password from token',
'',
null,
[
[
'name' => 'token',
'in' => 'path',
'required' => true,
'schema' => [
'type' => 'string',
],
],
],
],
])));
new RequestBody(
'Reset password',
new \ArrayObject([
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/ForgotPassword:reset',
],
],
])
)
)
);
$openApi->getPaths()->addPath('/forgot_password/{token}', $pathItem);

return $openApi;
}
Expand Down

0 comments on commit 037580e

Please sign in to comment.