-
-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add API Platform compatibility
- Loading branch information
1 parent
59dd54a
commit 0a87c57
Showing
10 changed files
with
302 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\OpenApi; | ||
|
||
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; | ||
use ApiPlatform\OpenApi\Model\MediaType; | ||
use ApiPlatform\OpenApi\Model\Operation; | ||
use ApiPlatform\OpenApi\Model\PathItem; | ||
use ApiPlatform\OpenApi\Model\RequestBody; | ||
use ApiPlatform\OpenApi\OpenApi; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Decorates API Platform OpenApiFactory. | ||
* | ||
* @author Vincent Chalamon <[email protected]> | ||
* | ||
* @final | ||
*/ | ||
class OpenApiFactory implements OpenApiFactoryInterface | ||
{ | ||
/** | ||
* @var OpenApiFactoryInterface | ||
*/ | ||
private $decorated; | ||
|
||
private $checkPath; | ||
|
||
public function __construct(OpenApiFactoryInterface $decorated, string $checkPath) | ||
{ | ||
$this->decorated = $decorated; | ||
$this->checkPath = $checkPath; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __invoke(array $context = []): OpenApi | ||
{ | ||
$openApi = ($this->decorated)($context); | ||
|
||
$openApi | ||
->getPaths() | ||
->addPath($this->checkPath, (new PathItem())->withPost((new Operation()) | ||
->withOperationId('login_check_post') | ||
->withTags(['Login Check']) | ||
->withResponses([ | ||
Response::HTTP_OK => [ | ||
'description' => 'User token created', | ||
'content' => [ | ||
'application/json' => [ | ||
'schema' => [ | ||
'type' => 'object', | ||
'properties' => [ | ||
'token' => [ | ||
'readOnly' => true, | ||
'type' => 'string', | ||
'nullable' => false, | ||
], | ||
], | ||
'required' => ['token'], | ||
], | ||
], | ||
], | ||
], | ||
]) | ||
->withSummary('Creates a user token.') | ||
->withRequestBody((new RequestBody()) | ||
->withDescription('The login data') | ||
->withContent(new \ArrayObject([ | ||
'application/json' => new MediaType(new \ArrayObject(new \ArrayObject([ | ||
'type' => 'object', | ||
'properties' => [ | ||
'_username' => [ | ||
'type' => 'string', | ||
'nullable' => false, | ||
], | ||
'_password' => [ | ||
'type' => 'string', | ||
'nullable' => false, | ||
], | ||
], | ||
'required' => ['_username', '_password'], | ||
]))), | ||
])) | ||
->withRequired(true) | ||
) | ||
)); | ||
|
||
return $openApi; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="lexik_jwt_authentication.api_platform.openapi.factory" class="Lexik\Bundle\JWTAuthenticationBundle\OpenApi\OpenApiFactory" decorates="api_platform.openapi.factory" public="false"> | ||
<argument type="service" id="lexik_jwt_authentication.api_platform.openapi.factory.inner"/> | ||
<argument /><!-- check path --> | ||
</service> | ||
</services> | ||
|
||
</container> |
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
120 changes: 120 additions & 0 deletions
120
Tests/Functional/Command/ApiPlatformOpenApiExportCommandTest.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Functional\Command; | ||
|
||
use ApiPlatform\Symfony\Bundle\ApiPlatformBundle; | ||
use Lexik\Bundle\JWTAuthenticationBundle\Tests\Functional\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
/** | ||
* Check API Platform compatibility. | ||
* | ||
* @author Vincent Chalamon <[email protected]> | ||
* | ||
* @requires function ApiPlatformBundle::build | ||
*/ | ||
class ApiPlatformOpenApiExportCommandTest extends TestCase | ||
{ | ||
/** | ||
* Test command. | ||
*/ | ||
public function testCheckOpenSSLCommand() | ||
{ | ||
$kernel = $this->bootKernel(); | ||
$app = new Application($kernel); | ||
$tester = new CommandTester($app->find('api:openapi:export')); | ||
|
||
$this->assertSame(0, $tester->execute([])); | ||
$this->assertJsonStringEqualsJsonString(<<<JSON | ||
{ | ||
"openapi": "3.0.0", | ||
"info": { | ||
"description": "API Platform integration in LexikJWTAuthenticationBundle", | ||
"title": "LexikJWTAuthenticationBundle", | ||
"version": "1.0.0" | ||
}, | ||
"servers": [ | ||
{ | ||
"description": "", | ||
"url": "/" | ||
} | ||
], | ||
"paths": { | ||
"/login_check": { | ||
"post": { | ||
"deprecated": false, | ||
"description": "", | ||
"operationId": "login_check_post", | ||
"parameters": [], | ||
"requestBody": { | ||
"description": "The login data", | ||
"required": true, | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"_username": { | ||
"nullable": false, | ||
"type": "string" | ||
}, | ||
"_password": { | ||
"nullable": false, | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"_username", | ||
"_password" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
"responses": { | ||
"200": { | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"properties": { | ||
"token": { | ||
"nullable": false, | ||
"readOnly": true, | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"token" | ||
], | ||
"type": "object" | ||
} | ||
} | ||
}, | ||
"description": "User token created" | ||
} | ||
}, | ||
"summary": "Creates a user token.", | ||
"tags": [ | ||
"Login Check" | ||
] | ||
}, | ||
"parameters": [] | ||
} | ||
}, | ||
"components": { | ||
"examples": {}, | ||
"headers": {}, | ||
"parameters": {}, | ||
"requestBodies": {}, | ||
"responses": {}, | ||
"schemas": {}, | ||
"securitySchemes": {} | ||
}, | ||
"security": [], | ||
"tags": [] | ||
} | ||
JSON | ||
, $tester->getDisplay()); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
default: | ||
resource: routing.yml | ||
|
||
api_platform: | ||
resource: . | ||
type: api_platform |