Skip to content

Commit 9cf3e88

Browse files
committed
Adjust: openapi documentation creating & storing
1 parent a749e68 commit 9cf3e88

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

library/Notifications/Api/V1/OpenApi.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
)]
6666
class OpenApi extends ApiV1
6767
{
68+
public const OPENAPI_PATH = __DIR__ . '/docs/openapi.json';
6869
/**
6970
* Generate OpenAPI documentation for the Notifications API
7071
*
@@ -73,23 +74,36 @@ class OpenApi extends ApiV1
7374
*/
7475
public function get(): array
7576
{
76-
$files = $this->getFilesIncludingDocs();
77+
// TODO: Create the documentation during CI and not on request
78+
if (file_exists(self::OPENAPI_PATH)) {
79+
$oad = file_get_contents(self::OPENAPI_PATH);
80+
} else {
81+
$files = $this->getFilesIncludingDocs();
7782

78-
try {
79-
$openapi = (new Generator(new PsrLogger()))
80-
->setVersion(\OpenApi\Annotations\OpenApi::VERSION_3_1_0)
81-
->generate($files);
82-
} catch (\RuntimeException $e) {
83-
$errorBody = json_encode([
84-
'error' => 'Failed to generate OpenAPI documentation',
85-
'message' => $e->getMessage(),
86-
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE);
83+
try {
84+
$openapi = (new Generator(new PsrLogger()))
85+
->setVersion(\OpenApi\Annotations\OpenApi::VERSION_3_1_0)
86+
->generate($files);
87+
} catch (\RuntimeException $e) {
88+
$errorBody = json_encode([
89+
'error' => 'Failed to generate OpenAPI documentation',
90+
'message' => $e->getMessage(),
91+
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE);
8792

88-
return $this->createArrayOfResponseData(500, $errorBody);
89-
}
93+
return $this->createArrayOfResponseData(500, $errorBody);
94+
}
95+
96+
$oad = $openapi->toJson(
97+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE | JSON_PRETTY_PRINT
98+
);
9099

91-
$body = $openapi->toJson(JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE);
100+
if (! is_dir(dirname(self::OPENAPI_PATH))) {
101+
mkdir(dirname(self::OPENAPI_PATH), 0755, true);
102+
}
103+
104+
file_put_contents(self::OPENAPI_PATH, $oad);
105+
}
92106

93-
return $this->createArrayOfResponseData(body: $body);
107+
return $this->createArrayOfResponseData(body: $oad);
94108
}
95109
}

0 commit comments

Comments
 (0)