-
Notifications
You must be signed in to change notification settings - Fork 0
Added endpoint to retrieve static single swagger file, #PG-4593 #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c818089
Added API endpoint to retrieve matomo swagger file
lachiebol de425fa
refactored to be more testable and added tests
lachiebol 9285973
Changelog updated
lachiebol ee516ce
Validating format now and added view check
lachiebol 7b5afe3
fixed tests
lachiebol 6084529
Cleaned up files
lachiebol d4df93c
Added default param
lachiebol d899a15
Updated docs
lachiebol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -10,15 +10,70 @@ | |||||
| namespace Piwik\Plugins\OpenApiDocs; | ||||||
|
|
||||||
| use Piwik\Piwik; | ||||||
| use Piwik\Plugin\Manager; | ||||||
| use Piwik\Plugins\OpenApiDocs\Specs\SpecGenerator; | ||||||
|
|
||||||
| /** | ||||||
| * API for plugin OpenApiDocs | ||||||
| * | ||||||
| * Exposes endpoints to fetch pre-generated OpenAPI specs or generate plugin-specific | ||||||
| * OpenAPI docs on demand. | ||||||
| * | ||||||
| * @method static \Piwik\Plugins\OpenApiDocs\API getInstance() | ||||||
| */ | ||||||
| class API extends \Piwik\Plugin\API | ||||||
| { | ||||||
| /** | ||||||
| * Get the pre-generated single OpenAPI spec file if it exists. This endpoint only reads | ||||||
| * the generated JSON file and does not trigger spec generation. | ||||||
| * | ||||||
| * /index.php?module=API&method=OpenApiDocs.getMatomoOpenApiSpec | ||||||
| * | ||||||
| * @return array<string, mixed> The decoded OpenAPI specification payload. | ||||||
| * @throws \Exception If the file is missing, unreadable, or contains invalid JSON. | ||||||
| */ | ||||||
| public function getMatomoOpenApiSpec(): array | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| { | ||||||
|
lachiebol marked this conversation as resolved.
|
||||||
| $filePath = $this->getMatomoSpecFilePath(); | ||||||
|
lachiebol marked this conversation as resolved.
|
||||||
|
|
||||||
| if (!$this->isSpecFileReadable($filePath)) { | ||||||
| throw new \Exception('OpenAPI spec file was not found. Generate it first via openapidocs:generate-spec-file.'); | ||||||
| } | ||||||
|
|
||||||
| $specContents = $this->readSpecFile($filePath); | ||||||
| if ($specContents === false) { | ||||||
| throw new \Exception('OpenAPI spec file could not be read.'); | ||||||
| } | ||||||
|
|
||||||
| $decodedSpec = json_decode($specContents, true); | ||||||
| if (!is_array($decodedSpec) || json_last_error() !== JSON_ERROR_NONE) { | ||||||
| throw new \Exception('OpenAPI spec file contains invalid JSON.'); | ||||||
| } | ||||||
|
|
||||||
| return $decodedSpec; | ||||||
| } | ||||||
|
|
||||||
| protected function getMatomoSpecFilePath(): string | ||||||
| { | ||||||
| $currentPluginDir = Manager::getInstance()::getPluginDirectory('OpenApiDocs'); | ||||||
|
|
||||||
| return $currentPluginDir . OpenApiDocs::GENERATED_SPECS_PATH . 'matomo_openapi_spec_v' . OpenApiDocs::DEFAULT_SPEC_VERSION . '.json'; | ||||||
| } | ||||||
|
|
||||||
| protected function isSpecFileReadable(string $filePath): bool | ||||||
| { | ||||||
| return is_file($filePath) && is_readable($filePath); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @param string $filePath | ||||||
| * @return string|false | ||||||
| */ | ||||||
| protected function readSpecFile(string $filePath) | ||||||
| { | ||||||
| return file_get_contents($filePath); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Get the generated API documentation data for the specified plugin. | ||||||
| * | ||||||
|
|
||||||
This file contains hidden or 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 hidden or 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,75 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Matomo - free/libre analytics platform | ||
| * | ||
| * @link https://matomo.org | ||
| * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Piwik\Plugins\OpenApiDocs\tests\Unit; | ||
|
|
||
| require_once PIWIK_INCLUDE_PATH . '/plugins/OpenApiDocs/vendor/autoload.php'; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Piwik\Plugins\OpenApiDocs\API; | ||
|
|
||
| /** | ||
| * @group OpenApiDocs | ||
| * @group OpenApiDocs_Unit | ||
| * @group OpenApiDocs_APITest | ||
| */ | ||
| class APITest extends TestCase | ||
| { | ||
| public function testGetMatomoOpenApiSpecReturnsDecodedJson() | ||
| { | ||
| $expectedSpec = [ | ||
| 'openapi' => '3.1.0', | ||
| 'info' => [ | ||
| 'title' => 'Matomo Reporting API', | ||
| 'version' => '1.0.0', | ||
| ], | ||
| ]; | ||
|
|
||
| $api = $this->buildApiMock(true, json_encode($expectedSpec)); | ||
|
|
||
| $result = $api->getMatomoOpenApiSpec(); | ||
|
|
||
| $this->assertSame($expectedSpec, $result); | ||
| } | ||
|
|
||
| public function testGetMatomoOpenApiSpecThrowsExceptionWhenFileMissing() | ||
| { | ||
| $api = $this->buildApiMock(false); | ||
|
|
||
| $this->expectException(\Exception::class); | ||
| $this->expectExceptionMessage('OpenAPI spec file was not found'); | ||
|
|
||
| $api->getMatomoOpenApiSpec(); | ||
| } | ||
|
|
||
| public function testGetMatomoOpenApiSpecThrowsExceptionWhenJsonIsInvalid() | ||
| { | ||
| $api = $this->buildApiMock(true, '{invalid json}'); | ||
|
|
||
| $this->expectException(\Exception::class); | ||
| $this->expectExceptionMessage('OpenAPI spec file contains invalid JSON'); | ||
|
|
||
| $api->getMatomoOpenApiSpec(); | ||
| } | ||
|
|
||
| private function buildApiMock(bool $isReadable, $fileContents = false): API | ||
| { | ||
| $api = $this->getMockBuilder(API::class) | ||
| ->onlyMethods(['getMatomoSpecFilePath', 'isSpecFileReadable', 'readSpecFile']) | ||
| ->getMock(); | ||
|
|
||
| $api->method('getMatomoSpecFilePath')->willReturn('/tmp/matomo_openapi_spec_v1.0.0.json'); | ||
| $api->method('isSpecFileReadable')->willReturn($isReadable); | ||
| $api->method('readSpecFile')->willReturn($fileContents); | ||
|
|
||
| return $api; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.