-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Preview): Added OpenAPI decorator to document
_preview
query p…
…aram and JWT security
- Loading branch information
1 parent
70b60c9
commit 449c9f9
Showing
3 changed files
with
75 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Api\OpenApi; | ||
|
||
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; | ||
use ApiPlatform\OpenApi\Model; | ||
use ApiPlatform\OpenApi\Model\PathItem; | ||
use ApiPlatform\OpenApi\OpenApi; | ||
|
||
final class PreviewDecorator implements OpenApiFactoryInterface | ||
{ | ||
private OpenApiFactoryInterface $decorated; | ||
|
||
public function __construct( | ||
OpenApiFactoryInterface $decorated | ||
) { | ||
$this->decorated = $decorated; | ||
} | ||
|
||
public function __invoke(array $context = []): OpenApi | ||
{ | ||
$openApi = ($this->decorated)($context); | ||
/** @var PathItem[] $paths */ | ||
$paths = $openApi->getPaths()->getPaths(); | ||
// For each GET path, add a new query parameter `_preview` to force preview mode | ||
foreach ($paths as $path => $pathItem) { | ||
$operation = $pathItem->getGet(); | ||
if (null !== $operation) { | ||
$responses = $operation->getResponses(); | ||
$responses['401'] = new Model\Response( | ||
description: 'Invalid JWT Token' | ||
); | ||
|
||
$newOperation = $operation->withParameters([ | ||
...$operation->getParameters(), | ||
(new Model\Parameter( | ||
'_preview', | ||
'query', | ||
'Enables preview mode (requires a valid bearer JWT token)', | ||
false | ||
))->withSchema(['type' => 'boolean'])->withExample('1') | ||
])->withSecurity([ | ||
...$operation->getSecurity() ?? [], | ||
['JWT' => []] | ||
])->withResponses($responses); | ||
$openApi->getPaths()->addPath($path, $pathItem->withGet($newOperation)); | ||
} | ||
} | ||
|
||
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