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
7 changes: 5 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

declare(strict_types=1);

return [
// Every unversioned API route is also served under /api/v1/; see ApiPathAliases.
return \OCA\Thematiq\Middleware\Support\ApiPathAliases::withVersionedAliases([
'routes' => [
// The OpenAPI document of thematiq's own API, public, served under /api/v1/ too.
['name' => 'openApi#show', 'url' => '/api/openapi.json', 'verb' => 'GET'],
// Metrics — served by nldesign's own MetricsController (domain theme
// metrics: token sets, custom overrides, theming syncs). Admin-only:
// no #[PublicPage]/#[NoAdminRequired], so the SecurityMiddleware
Expand Down Expand Up @@ -199,4 +202,4 @@
['name' => 'documentStyle#upload', 'url' => '/settings/document-style/{kind}', 'verb' => 'POST'],
['name' => 'documentStyle#remove', 'url' => '/settings/document-style/{kind}', 'verb' => 'DELETE'],
],
];
]);
5 changes: 5 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCA\Thematiq\Capabilities;
use OCA\Thematiq\Listener\ThemeInjectionListener;
use OCA\Thematiq\Listener\WorkspaceIconListener;
use OCA\Thematiq\Middleware\ApiContractMiddleware;
use OCA\Thematiq\Middleware\ConfigSourceLockMiddleware;
use OCA\Thematiq\Service\RuntimeFile\AppDataRuntimeFileStore;
use OCA\Thematiq\Service\RuntimeFile\RuntimeFileStore;
Expand Down Expand Up @@ -109,6 +110,10 @@ public function __construct() {
* @spec openspec/specs/mobile-logo-and-touch-icon/spec.md#requirement-the-mobile-logo-and-the-touch-icon-reach-every-page
*/
public function register(IRegistrationContext $context): void {
// The API contract (version header, deprecation of unversioned paths,
// problem details). Registered first: Nextcloud runs afterController in
// reverse order, so it sees the final response of every other middleware.
$context->registerMiddleware(class: ApiContractMiddleware::class);
// Load the app's composer autoloader so this app's classes are resolvable
// process-wide, not only inside this app's own container — required so a
// cross-app event listener (e.g. the OpenRegister federated-config type)
Expand Down
118 changes: 118 additions & 0 deletions lib/Controller/OpenApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/**
* Thematiq OpenAPI document controller.
*
* Publishes the app's OpenAPI document at `/api/openapi.json`, as the NL API
* Design Rules require (/core/publish-openapi).
*
* @category Controller
* @package OCA\Thematiq\Controller
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
* @link https://conduction.nl
*
* @spec openspec/specs/api-contract/spec.md
*
* SPDX-FileCopyrightText: 2026 Conduction B.V. <info@conduction.nl>
* SPDX-License-Identifier: EUPL-1.2
*/

declare(strict_types=1);

namespace OCA\Thematiq\Controller;

use OCA\Thematiq\AppInfo\Application;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use stdClass;

/**
* GET /api/openapi.json.
*
* @category Controller
* @package OCA\Thematiq\Controller
* @author Conduction Development Team <info@conduction.nl>
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
* @link https://conduction.nl
*
* @spec openspec/specs/api-contract/spec.md
*/
class OpenApiController extends Controller {

/**
* Constructor.
*
* @param IRequest $request The request.
* @param IAppManager $appManager Resolves the app path and the installed version.
*
* @psalm-suppress PossiblyUnusedMethod Instantiated via Nextcloud dependency injection.
*/
public function __construct(
IRequest $request,
private readonly IAppManager $appManager,
) {
parent::__construct(appName: Application::APP_ID, request: $request);
}//end __construct()

/**
* The OpenAPI document of this app, readable from any origin.
*
* The document describes the API, not its data, so it is public. Its
* `info.version` is the installed app version, which is also what every
* API response carries in `API-Version`.
*
* @return JSONResponse The document, or 404 when it is not installed.
*
* @PublicPage
* @NoCSRFRequired
*
* @spec openspec/specs/api-contract/spec.md
*/
#[PublicPage]
#[NoCSRFRequired]
#[AnonRateLimit(limit: 120, period: 60)]
public function show(): JSONResponse {
$document = $this->document();
$response = new JSONResponse(
data: ['error' => 'The OpenAPI document is not installed'],
statusCode: Http::STATUS_NOT_FOUND
);
if ($document !== null) {
$document->info->version = $this->appManager->getAppVersion(appId: Application::APP_ID);
$response = new JSONResponse(data: $document);
}

$response->addHeader(name: 'Access-Control-Allow-Origin', value: '*');

return $response;
}//end show()

/**
* The installed OpenAPI document, decoded with empty JSON objects kept as objects.
*
* @return stdClass|null The document, or null when it is missing or not a document.
*
* @spec openspec/specs/api-contract/spec.md
*/
private function document(): ?stdClass {
$path = $this->appManager->getAppPath(appId: Application::APP_ID) . '/openapi.json';
if (is_readable($path) === false) {
return null;
}

$document = json_decode((string)file_get_contents($path), false);
if ($document instanceof stdClass && ($document->info ?? null) instanceof stdClass) {
return $document;
}

return null;
}//end document()
}//end class
Loading
Loading