Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 87 additions & 3 deletions src/bundle/ApiPlatform/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\OpenApi\Model\Info;
use ApiPlatform\OpenApi\Model\Operation;
use ApiPlatform\OpenApi\Model\RequestBody;
use ApiPlatform\OpenApi\Model\Response;
use ApiPlatform\OpenApi\Model\Server;
use ApiPlatform\OpenApi\OpenApi;
Expand Down Expand Up @@ -101,6 +102,7 @@
private function processOperations(Operation $operation): Operation
{
$newOperation = $operation;
$newOperation = $this->insertIbexaRequestExample($newOperation);
$newOperation = $this->insertIbexaResponseExample($newOperation);

return $this->insertIbexaEditionBadges($newOperation);
Expand All @@ -120,6 +122,54 @@
return $operation;
}

private function insertIbexaRequestExample(Operation $operation): Operation

Check warning on line 125 in src/bundle/ApiPlatform/OpenApiFactory.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This method has 4 returns, which is more than the 3 allowed.

See more on https://sonarcloud.io/project/issues?id=ibexa_rest&issues=AZz7aSobbKr9pOSEF-L6&open=AZz7aSobbKr9pOSEF-L6&pullRequest=212
{
$requestBody = $operation->getRequestBody();

if ($requestBody === null) {
return $operation;
}

$content = $requestBody->getContent();

if ($content === null) {
return $operation;
}

/** @var ArrayObject<string, mixed> $newContent */
$newContent = new ArrayObject();
$hasChanges = false;

foreach ($content as $mediaType => $requestContent) {
if (!array_key_exists('x-ibexa-example-file', $requestContent)) {
$newContent[$mediaType] = $requestContent;
continue;
}

$exampleFilePath = $this->kernel->locateResource($requestContent['x-ibexa-example-file']);
$example = $this->loadExampleFromFile($exampleFilePath);

$newRequestContent = $requestContent;
$newRequestContent['example'] = $example;
unset($newRequestContent['x-ibexa-example-file']);

$newContent[$mediaType] = $newRequestContent;
$hasChanges = true;
}

if ($hasChanges === true) {
$newRequestBody = new RequestBody(
$requestBody->getDescription(),
$newContent,
$requestBody->getRequired()
);

return $operation->withRequestBody($newRequestBody);
}

return $operation;
}

/**
* @throws \JsonException
*/
Expand All @@ -140,9 +190,8 @@
}

$exampleFilePath = $this->kernel->locateResource($responseContent['x-ibexa-example-file']);
$exampleFileContent = file_get_contents($exampleFilePath);
$isJson = 'json' === array_slice(explode('.', pathinfo($exampleFilePath, PATHINFO_FILENAME)), -1, 1)[0];
$newContent[$mediaType]['example'] = $isJson ? json_decode($exampleFileContent ?: '', true, 512, JSON_THROW_ON_ERROR) : $exampleFileContent;
$example = $this->loadExampleFromFile($exampleFilePath);
$newContent[$mediaType]['example'] = $example;
unset($newContent[$mediaType]['x-ibexa-example-file']);
}

Expand All @@ -156,4 +205,39 @@

return $newOperation;
}

/**
* @return array<string, mixed>|string
*
* @throws \JsonException
*/
private function loadExampleFromFile(string $filePath): array|string
{
$fileContent = file_get_contents($filePath);

if ($fileContent === false) {
throw new \RuntimeException("Failed to read example file: {$filePath}");

Check warning on line 219 in src/bundle/ApiPlatform/OpenApiFactory.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define and throw a dedicated exception instead of using a generic one.

See more on https://sonarcloud.io/project/issues?id=ibexa_rest&issues=AZz7aSobbKr9pOSEF-L7&open=AZz7aSobbKr9pOSEF-L7&pullRequest=212
}

return $this->isJson($filePath)
? $this->parseJsonFile($fileContent, $filePath)
: $fileContent;
}

private function isJson(string $filePath): bool
{
return in_array('json', explode('.', pathinfo($filePath, PATHINFO_FILENAME)));
}

/**
* @return array<string, mixed>
*/
private function parseJsonFile(string $fileContent, string $filePath): array
{
try {
return json_decode($fileContent, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
throw new \RuntimeException("Failed to parse JSON example file: {$filePath}", 0, $e);

Check warning on line 240 in src/bundle/ApiPlatform/OpenApiFactory.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define and throw a dedicated exception instead of using a generic one.

See more on https://sonarcloud.io/project/issues?id=ibexa_rest&issues=AZz7aSobbKr9pOSEF-L8&open=AZz7aSobbKr9pOSEF-L8&pullRequest=212
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"username": "admin",
"password": "secret"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<LoginRequest>
<username>admin</username>
<password>secret</password>
</LoginRequest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9",
"user": {
"id": 14,
"login": "admin"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Session>
<identifier>go327ij2cirpo59pb6rrv2a4el2</identifier>
<name>eZSESSID</name>
<csrfToken>23lkneri34ijajedfw39orj3j93</csrfToken>
</Session>
Loading
Loading