-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce x-ibexa-example-file custom property
- Loading branch information
Showing
7 changed files
with
156 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,16 @@ | |
namespace Ibexa\Bundle\Rest\ApiPlatform; | ||
|
||
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; | ||
use ApiPlatform\OpenApi\Model\Response; | ||
use ApiPlatform\OpenApi\OpenApi; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
final class OpenApiFactory implements OpenApiFactoryInterface | ||
{ | ||
public function __construct( | ||
private readonly OpenApiFactoryInterface $decorated, | ||
private readonly SchemasCollectionFactory $schemaCollectionFactory, | ||
private readonly KernelInterface $kernel, | ||
) { | ||
} | ||
|
||
|
@@ -25,7 +28,15 @@ public function __construct( | |
public function __invoke(array $context = []): OpenApi | ||
{ | ||
$openApi = $this->decorated->__invoke($context); | ||
$openApi = $this->addSchemas($openApi); | ||
|
||
$this->insertExampleFilesContent($openApi); | ||
|
||
return $openApi; | ||
} | ||
|
||
private function addSchemas(OpenApi $openApi) | ||
{ | ||
$schemasCollection = $this->schemaCollectionFactory->create(); | ||
$schemas = iterator_to_array($schemasCollection); | ||
|
||
|
@@ -36,4 +47,95 @@ public function __invoke(array $context = []): OpenApi | |
|
||
return $openApi; | ||
} | ||
|
||
private function insertExampleFilesContent(OpenApi $openApi) | ||
{ | ||
$paths = $openApi->getPaths(); | ||
|
||
/** @var \ApiPlatform\OpenApi\Model\PathItem $pathItem */ | ||
foreach ($paths->getPaths() as $path => $pathItem) { | ||
$newPathItem = $pathItem; | ||
|
||
/** @var array<string, \ApiPlatform\OpenApi\Model\Operation|null> $methods */ | ||
$methods = [ | ||
'GET' => $pathItem->getGet(), | ||
'PUT' => $pathItem->getPut(), | ||
'POST' => $pathItem->getPost(), | ||
'DELETE' => $pathItem->getDelete(), | ||
'OPTIONS' => $pathItem->getOptions(), | ||
'HEAD' => $pathItem->getHead(), | ||
'PATCH' => $pathItem->getPatch(), | ||
'TRACE' => $pathItem->getTrace(), | ||
]; | ||
foreach ($methods as $method => $operation) { | ||
if (empty($operation)) { | ||
continue; | ||
} | ||
|
||
$newOperation = $operation; | ||
|
||
/** | ||
* @var int $responseCode | ||
* @var \ApiPlatform\OpenApi\Model\Response|array $response | ||
*/ | ||
foreach ($operation->getResponses() as $responseCode => $response) { | ||
Check failure on line 81 in src/bundle/ApiPlatform/OpenApiFactory.php GitHub Actions / Unit & integration tests (8.3)
Check failure on line 81 in src/bundle/ApiPlatform/OpenApiFactory.php GitHub Actions / Unit & integration tests (8.3)
|
||
if (!is_array($response) || !array_key_exists('content', $response)) { | ||
continue; | ||
} | ||
|
||
$content = $response['content']; | ||
$newContent = $content; | ||
|
||
foreach ($newContent as $mediaType => $responseContent) { | ||
if (array_key_exists('x-ibexa-example-file', $responseContent)) { | ||
$exampleFilePath = $this->kernel->locateResource($responseContent['x-ibexa-example-file']); | ||
$exampleFileContent = file_get_contents($exampleFilePath); | ||
$newContent[$mediaType]['example'] = $exampleFileContent; | ||
unset($newContent[$mediaType]['x-ibexa-example-file']); | ||
} | ||
} | ||
|
||
if ($newContent !== $content) { | ||
$newOperation = $newOperation->withResponse( | ||
$responseCode, | ||
new Response((string)$responseCode, new \ArrayObject($newContent)), | ||
); | ||
} | ||
} | ||
|
||
if ($newOperation !== $operation) { | ||
switch ($method) { | ||
case 'GET': | ||
$newPathItem = $newPathItem->withGet($newOperation); | ||
break; | ||
case 'PUT': | ||
$newPathItem = $newPathItem->withPut($newOperation); | ||
break; | ||
case 'POST': | ||
$newPathItem = $newPathItem->withPost($newOperation); | ||
break; | ||
case 'DELETE': | ||
$newPathItem = $newPathItem->withDelete($newOperation); | ||
break; | ||
case 'OPTIONS': | ||
$newPathItem = $newPathItem->withOptions($newOperation); | ||
break; | ||
case 'HEAD': | ||
$newPathItem = $newPathItem->withHead($newOperation); | ||
break; | ||
case 'PATCH': | ||
$newPathItem = $newPathItem->withPatch($newOperation); | ||
break; | ||
case 'TRACE': | ||
$newPathItem = $newPathItem->withTrace($newOperation); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if ($newPathItem !== $pathItem) { | ||
$paths->addPath($path, $newPathItem); | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/bundle/Resources/api_platform/examples/languages/GET/LanguageList.json.example
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,21 @@ | ||
{ | ||
"LanguageList": { | ||
"_media-type": "application/vnd.ibexa.api.LanguageList+json", | ||
"_href": "/api/ibexa/v2/languages", | ||
"Language": [ | ||
{ | ||
"_media-type": "application/vnd.ibexa.api.Language+json", | ||
"_href": "/api/ibexa/v2/languages/eng-GB", | ||
"languageId": 2, | ||
"languageCode": "eng-GB", | ||
"name": "English (United Kingdom)" | ||
}, { | ||
"_href": "/api/ibexa/v2/languages/pol-PL", | ||
"_media-type": "application/vnd.ibexa.api.Language+json", | ||
"languageCode": "pol-PL", | ||
"languageId": 4, | ||
"name": "Polish (polski)" | ||
} | ||
] | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/bundle/Resources/api_platform/examples/languages/GET/LanguageList.xml.example
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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<LanguageList media-type="application/vnd.ibexa.api.LanguageList+xml" href="/api/ibexa/v2/languages"> | ||
<Language media-type="application/vnd.ibexa.api.Language+xml" href="/api/ibexa/v2/languages/eng-GB"> | ||
<languageId>2</languageId> | ||
<languageCode>eng-GB</languageCode> | ||
<name>English (United Kingdom)</name> | ||
</Language> | ||
<Language href="/api/ibexa/v2/languages/pol-PL" media-type="application/vnd.ibexa.api.Language+xml"> | ||
<languageId>4</languageId> | ||
<languageCode>pol-PL</languageCode> | ||
<name>Polish (polski)</name> | ||
</Language> | ||
</LanguageList> |
9 changes: 9 additions & 0 deletions
9
src/bundle/Resources/api_platform/examples/languages/code/GET/Language.json.example
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,9 @@ | ||
{ | ||
"Language": { | ||
"_media-type": "application/vnd.ibexa.api.Language+json", | ||
"_href": "/api/ibexa/v2/languages/eng-GB", | ||
"languageId": 2, | ||
"languageCode": "eng-GB", | ||
"name": "English (United Kingdom)" | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/bundle/Resources/api_platform/examples/languages/code/GET/Language.xml.example
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Language media-type="application/vnd.ibexa.api.Language+xml" href="/api/ibexa/v2/languages/eng-GB"> | ||
<languageId>2</languageId> | ||
<languageCode>eng-GB</languageCode> | ||
<name>English (United Kingdom)</name> | ||
</Language> |
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