Skip to content
Merged
5 changes: 3 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

return [
// Every unversioned API route is also served under /api/v1/; see ApiPathAliases.
return \OCA\OpenRegister\Middleware\Support\ApiPathAliases::withVersionedAliases([
'resources' => [
'Registers' => ['url' => 'api/registers'],
'Schemas' => ['url' => 'api/schemas'],
Expand Down Expand Up @@ -2328,4 +2329,4 @@
['name' => 'dashboard#catchAll', 'url' => '/{path}', 'verb' => 'GET',
'requirements' => ['path' => '(?!api/).+'], 'defaults' => ['path' => '']],
],
];
]);
13 changes: 12 additions & 1 deletion docs/features/api-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,21 @@ All generated REST endpoints comply with the NL API Design Rules:
- Resource-based URL structure (`/api/objects/{register}/{schema}`)
- HTTP methods follow REST semantics (GET, POST, PUT, PATCH, DELETE)
- Pagination via `_start`/`_limit` and `Link` headers
- Versioning via URL path (`/api/v1/...`) or `API-Version` header
- Versioning via URL path (`/api/v1/...`) or `API-Version` header (see below)
- Error responses follow Problem Details (RFC 7807)
- Dutch field names supported alongside English aliases

### Versioned paths

Every API path is served twice: under `/api/v1/` and at its unversioned path.

- `/index.php/apps/openregister/api/v1/<path>` is the current form. The root `openapi.json` describes it.
- `/index.php/apps/openregister/api/<path>` keeps working and answers the same, but it is deprecated. Its responses carry `Deprecation: @1794441600` (RFC 9745, 12 November 2026) and `Link: <.../api/v1/<path>>; rel="successor-version"`. No sunset date is set yet.
- `filesMultipart` is the deprecated name of `files-multipart`. Its successor is `/api/v1/objects/{register}/{schema}/{id}/files-multipart`. It still answers, but `openapi.json` does not list it, because its spelling breaks the kebab-case path rule.
- Paths that already carry a version (`/api/v2/audit`, `/api/mcp/v1/...`) and paths that speak another standard (MCP, GraphQL, WFS, iCalendar feeds, metrics, health, federation) get no alias and are not deprecated.

`ApiPathAliases` in `lib/Middleware/Support/` adds the aliases to `appinfo/routes.php`, so a new route gets its `/api/v1/` alias automatically.

## Standards

| Standard | Role |
Expand Down
5 changes: 5 additions & 0 deletions lib/Middleware/MiddlewareWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public static function register(IRegistrationContext $context): void {
// deprecated version keeps exactly the behaviour it had before.
$context->registerMiddleware(\OCA\OpenRegister\Middleware\ApiVersionMiddleware::class);

// Marks the unversioned API paths deprecated in favour of their
// `/api/v1/` aliases. After ApiVersionMiddleware, so a deprecated
// contract version's own Deprecation and Link headers win.
$context->registerMiddleware(UnversionedPathDeprecationMiddleware::class);

// Register the ApiCallerMiddleware (api-as-a-versioned-surface): binds a
// caller to its administered source addresses, bounds it to its
// administered ceiling, and records which route and version it called.
Expand Down
203 changes: 203 additions & 0 deletions lib/Middleware/Support/ApiPathAliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

/**
* The versioned `/api/v1/` aliases of OpenRegister's API routes.
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Middleware
* @package OCA\OpenRegister\Middleware\Support
*
* @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://www.OpenRegister.nl
*
* @spec openspec/specs/openapi-generation/spec.md
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Middleware\Support;

/**
* Serves every API route under `/api/v1/` as well as at its unversioned path,
* which stays working and is deprecated.
*
* The scheme: `/api/<path>` is also served as `/api/v1/<path>`, with the same
* controller, requirements and defaults. Routes that already carry a version
* (`/api/v2/audit`, `/api/mcp/v1/...`) and standard protocol paths
* ({@see StandardProtocolPaths}) get no alias and are not deprecated. A route
* listed in {@see self::RENAMED} gets no alias of its own: its new name has one.
*
* @spec openspec/specs/openapi-generation/spec.md
*/
final class ApiPathAliases {

/**
* The prefix of the versioned paths.
*
* @var string
*/
public const VERSION_PREFIX = '/api/v1';

/**
* The date the unversioned paths were deprecated, as an RFC 9745 `Deprecation` value.
*
* @var string
*/
public const DEPRECATED_SINCE = '@1794441600';

/**
* Paths whose name changed, as a pattern over the old path and its replacement.
*
* @var array<string, string>
*/
public const RENAMED = [
'#^(/api/objects/[^/]+/[^/]+/[^/]+)/filesMultipart$#' => '$1/files-multipart',
];

/**
* The actions of a Nextcloud resource, as in its RouteParser.
*
* @var array<int, array{0: string, 1: string, 2: bool}>
*/
private const RESOURCE_ACTIONS = [
['index', 'GET', true],
['show', 'GET', false],
['create', 'POST', true],
['update', 'PUT', false],
['destroy', 'DELETE', false],
];

/**
* The pattern of a path that already names a version.
*
* @var string
*/
private const VERSIONED = '#^/api/(?:[a-z0-9-]+/)?v[0-9]{1,3}(?:/|$)#i';

/**
* Whether a path is an unversioned API path, which is deprecated in favour of its `/api/v1/` alias.
*
* @param string $path An app-relative path or a request path under the app root.
*
* @return bool True for a deprecated unversioned API path.
*
* @spec openspec/specs/openapi-generation/spec.md
*/
public static function isDeprecated(string $path): bool {
$relative = StandardProtocolPaths::appRelative(path: $path);
if (str_starts_with($relative, '/api/') === false || preg_match(self::VERSIONED, $relative) === 1) {
return false;
}

return StandardProtocolPaths::matches(path: $relative) === false;
}//end isDeprecated()

/**
* The `/api/v1/` path that replaces a deprecated path, keeping whatever precedes the app root.
*
* @param string $path A request path, or an app-relative path.
*
* @return string|null The successor path, or null when the path is not deprecated.
*
* @spec openspec/specs/openapi-generation/spec.md
*/
public static function successorOf(string $path): ?string {
if (self::isDeprecated(path: $path) === false) {
return null;
}

$relative = StandardProtocolPaths::appRelative(path: $path);
$prefix = substr($path, 0, strlen($path) - strlen($relative));

return $prefix . self::versioned(path: self::renamed(path: $relative));
}//end successorOf()

/**
* A routes.php declaration with an `/api/v1/` alias of every unversioned API route and resource.
*
* The aliases come after the original routes, so the originals keep their order and precedence.
*
* @param array<string, array<array-key, mixed>> $routes The declaration.
*
* @return array<string, array<array-key, mixed>> The declaration with the aliases added to `routes`.
*
* @spec openspec/specs/openapi-generation/spec.md
*/
public static function withVersionedAliases(array $routes): array {
$aliases = [];
foreach (($routes['routes'] ?? []) as $route) {
$url = '/' . ltrim((string)($route['url'] ?? ''), '/');
if (self::isDeprecated(path: $url) === true && self::renamed(path: $url) === $url) {
$aliases[] = array_merge($route, ['url' => self::versioned(path: $url), 'postfix' => ($route['postfix'] ?? '') . 'v1']);
}
}

foreach (($routes['resources'] ?? []) as $resource => $config) {
$aliases = array_merge($aliases, self::resourceAliases(resource: (string)$resource, url: '/' . ltrim((string)$config['url'], '/')));
}

$routes['routes'] = array_merge(($routes['routes'] ?? []), $aliases);

return $routes;
}//end withVersionedAliases()

/**
* The `/api/v1/` routes of a resource's five actions.
*
* @param string $resource The resource name, which is also its controller.
* @param string $url The resource's collection path.
*
* @return array<int, array<string, string>> The routes, or none when the resource is not deprecated.
*/
private static function resourceAliases(string $resource, string $url): array {
if (self::isDeprecated(path: $url) === false) {
return [];
}

$aliases = [];
foreach (self::RESOURCE_ACTIONS as [$action, $verb, $onCollection]) {
$path = self::versioned(path: $url);
if ($onCollection === false) {
$path .= '/{id}';
}

$aliases[] = ['name' => lcfirst($resource) . '#' . $action, 'url' => $path, 'verb' => $verb, 'postfix' => 'v1'];
}

return $aliases;
}//end resourceAliases()

/**
* The new name of a renamed path, or the path itself.
*
* @param string $path An app-relative path.
*
* @return string The current name of the path.
*/
private static function renamed(string $path): string {
foreach (self::RENAMED as $pattern => $replacement) {
$current = preg_replace($pattern, $replacement, $path);
if (is_string($current) === true && $current !== $path) {
return $current;
}
}

return $path;
}//end renamed()

/**
* The `/api/v1/` form of an unversioned API path.
*
* @param string $path An app-relative path starting with `/api/`.
*
* @return string The versioned path.
*/
private static function versioned(string $path): string {
return self::VERSION_PREFIX . substr($path, strlen('/api'));
}//end versioned()
}//end class
4 changes: 3 additions & 1 deletion lib/Middleware/Support/StandardProtocolPaths.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public static function matches(string $path): bool {
* @param string $path An app-relative path or a request path under the app root.
*
* @return string The app-relative path.
*
* @spec openspec/specs/openapi-generation/spec.md
*/
private static function appRelative(string $path): string {
public static function appRelative(string $path): string {
$candidate = '/' . ltrim($path, '/');
$rootAt = strpos($candidate, self::APP_ROOT . '/');
if ($rootAt === false) {
Expand Down
114 changes: 114 additions & 0 deletions lib/Middleware/UnversionedPathDeprecationMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/**
* Announces on every unversioned API path that it is deprecated in favour of
* its `/api/v1/` alias.
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Middleware
* @package OCA\OpenRegister\Middleware
*
* @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://www.OpenRegister.nl
*
* @spec openspec/specs/openapi-generation/spec.md
*/

declare(strict_types=1);

namespace OCA\OpenRegister\Middleware;

use OCA\OpenRegister\Middleware\Support\ApiPathAliases;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\IRequest;

/**
* Adds the RFC 9745 `Deprecation` header and a `Link` to the successor path
* to responses on a deprecated unversioned API path. It only decorates: the
* path keeps answering exactly as before.
*
* Registered after ApiVersionMiddleware, so it runs before it in
* afterController and a deprecated contract version's own headers win.
* A response the controller already marked deprecated keeps its own headers.
*
* @spec openspec/specs/openapi-generation/spec.md
*/
class UnversionedPathDeprecationMiddleware extends Middleware {

/**
* Constructor.
*
* @param IRequest $request The incoming request.
*
* @return void
*/
public function __construct(
private readonly IRequest $request,
) {
}//end __construct()

/**
* Mark a response on a deprecated path.
*
* @param mixed $controller The controller that ran.
* @param string $methodName The method that ran.
* @param Response $response The response.
*
* @return Response The same response, with the deprecation headers when the path is deprecated.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter) The Middleware contract fixes this signature.
*
* @spec openspec/specs/openapi-generation/spec.md
*/
public function afterController($controller, $methodName, Response $response): Response {
$path = $this->request->getPathInfo();
if (is_string($path) === false || $path === '') {
return $response;
}

$successor = ApiPathAliases::successorOf(path: $path);
if ($successor === null || $this->isAlreadyDeprecated(response: $response) === true) {
return $response;
}

$response->addHeader('Deprecation', ApiPathAliases::DEPRECATED_SINCE);
$response->addHeader('Link', '<' . $this->scriptPrefix() . $successor . '>; rel="successor-version"');

return $response;
}//end afterController()

/**
* Whether the controller already marked the response deprecated, naming its own successor.
*
* @param Response $response The response.
*
* @return bool True when the response carries a `Deprecation` or `Link` header.
*/
private function isAlreadyDeprecated(Response $response): bool {
$names = array_map('strtolower', array_keys($response->getHeaders()));

return in_array('deprecation', $names, true) === true || in_array('link', $names, true) === true;
}//end isAlreadyDeprecated()

/**
* The front controller that precedes the path info in the request URI, such as `/index.php`.
*
* @return string The prefix, or '' when the URI carries none.
*/
private function scriptPrefix(): string {
$uri = (string)$this->request->getRequestUri();
$pathInfo = (string)$this->request->getPathInfo();
$at = strpos($uri, $pathInfo);
if ($pathInfo === '' || $at === false) {
return '';
}

return substr($uri, 0, $at);
}//end scriptPrefix()
}//end class
Loading
Loading