Skip to content

Commit

Permalink
introduce x-ibexa-example-file custom property
Browse files Browse the repository at this point in the history
  • Loading branch information
tischsoic committed Aug 29, 2024
1 parent d860b11 commit b161278
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 65 deletions.
102 changes: 102 additions & 0 deletions src/bundle/ApiPlatform/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand All @@ -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)

Check failure on line 38 in src/bundle/ApiPlatform/OpenApiFactory.php

View workflow job for this annotation

GitHub Actions / Unit & integration tests (8.3)

Method Ibexa\Bundle\Rest\ApiPlatform\OpenApiFactory::addSchemas() has no return type specified.
{
$schemasCollection = $this->schemaCollectionFactory->create();
$schemas = iterator_to_array($schemasCollection);

Expand All @@ -36,4 +47,95 @@ public function __invoke(array $context = []): OpenApi

return $openApi;
}

private function insertExampleFilesContent(OpenApi $openApi)

Check failure on line 51 in src/bundle/ApiPlatform/OpenApiFactory.php

View workflow job for this annotation

GitHub Actions / Unit & integration tests (8.3)

Method Ibexa\Bundle\Rest\ApiPlatform\OpenApiFactory::insertExampleFilesContent() has no return type specified.
{
$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

View workflow job for this annotation

GitHub Actions / Unit & integration tests (8.3)

Argument of an invalid type array|null supplied for foreach, only iterables are supported.

Check failure on line 81 in src/bundle/ApiPlatform/OpenApiFactory.php

View workflow job for this annotation

GitHub Actions / Unit & integration tests (8.3)

PHPDoc tag @var for variable $response has no value type specified in iterable type array.
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);
}
}
}
}
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)"
}
]
}
}
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>
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)"
}
}
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>
1 change: 1 addition & 0 deletions src/bundle/Resources/config/api_platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
arguments:
- '@.inner'
- '@Ibexa\Bundle\Rest\ApiPlatform\SchemasCollectionFactory'
- '@Symfony\Component\HttpKernel\KernelInterface'

ibexa.api_platform.openapi.factory:
parent: api_platform.openapi.factory
Expand Down
69 changes: 4 additions & 65 deletions src/lib/Server/Controller/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
'schema' => [
'$ref' => '#/components/schemas/LanguageList',
],
'example' => LANGUAGE_LIST_XML_EXAMPLE,
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/languages/GET/LanguageList.xml.example',
],
'application/vnd.ibexa.api.LanguageList+json' => [
'schema' => [
'$ref' => '#/components/schemas/LanguageListWrapper',
],
'example' => LANGUAGE_LIST_JSON_EXAMPLE,
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/languages/GET/LanguageList.json.example',
],
],
],
Expand Down Expand Up @@ -89,13 +89,13 @@
'schema' => [
'$ref' => '#/components/schemas/Language',
],
'example' => LANGUAGE_XML_EXAMPLE,
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/languages/code/GET/Language.xml.example',
],
'application/vnd.ibexa.api.Language+json' => [
'schema' => [
'$ref' => '#/components/schemas/LanguageWrapper',
],
'example' => LANGUAGE_JSON_EXAMPLE,
'x-ibexa-example-file' => '@IbexaRestBundle/Resources/api_platform/examples/languages/code/GET/Language.json.example',
],
],
],
Expand Down Expand Up @@ -127,64 +127,3 @@ public function loadLanguage(string $languageCode): ApiLanguage
return $this->languageService->loadLanguage($languageCode);
}
}

const LANGUAGE_LIST_XML_EXAMPLE = <<<EXAMPLE
<?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>
EXAMPLE;

const LANGUAGE_LIST_JSON_EXAMPLE = <<<EXAMPLE
{
"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)"
}
]
}
}
EXAMPLE;

const LANGUAGE_XML_EXAMPLE = <<<EXAMPLE
<?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>
EXAMPLE;

const LANGUAGE_JSON_EXAMPLE = <<<EXAMPLE
{
"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)"
}
}
EXAMPLE;

0 comments on commit b161278

Please sign in to comment.