.. include:: /Includes.rst.txt .. _extending-api: === API === The HTTP API is described in OpenAPI 3.1. Every package adds its operations in ``openapi/openapi.yaml``. The core merges the contracts of all installed packages and serves them as ``/api/v1/openapi.json``. In the merged document the path items are written out, and every schema, parameter, response, header and security scheme a ``$ref`` names is kept once under ``components`` and referenced from there (``#/components/schemas/Uuid``). When two packages use the same name, the later package's part gets the package as prefix, for example ``LeaveAttendanceDay``. Contract layout =============== A contract consists of a root file, path files and component files: .. code-block:: text packages/web-vision/adsum-working-time/openapi/ openapi.yaml root: metadata, tags and the list of paths paths/working-time.yaml one entry per path item components/working-time.yaml schemas The root file lists the paths and refers to the entries of the path files: .. code-block:: yaml openapi: 3.1.0 info: title: Time Tracking API, working-time package version: 1.0.0 servers: - url: /api/v1 security: - bearerAuth: [] - sessionCookie: [] paths: /working-time-models: $ref: 'paths/working-time.yaml#/workingTimeModels' /working-time-models/{id}: $ref: 'paths/working-time.yaml#/workingTimeModelById' A path file has one top-level key per path item, with its operations. A component file has a top-level ``schemas`` key. Shared parts come from ``adsum-core/openapi/components/common.yaml``. Operations ========== Every operation has an ``operationId`` and an ``x-access`` list (see :ref:`extending-navigation-access`). The list operation of the working-time models, shortened: .. code-block:: yaml workingTimeModels: get: operationId: listWorkingTimeModels summary: List working-time models x-access: - anyEmployee - hr - admin parameters: - $ref: ../../../adsum-core/openapi/components/common.yaml#/parameters/CompanyId - $ref: ../../../adsum-core/openapi/components/common.yaml#/parameters/Page - $ref: ../../../adsum-core/openapi/components/common.yaml#/parameters/PerPage - $ref: ../../../adsum-core/openapi/components/common.yaml#/parameters/Sort responses: '200': description: OK content: application/json: schema: $ref: ../components/working-time.yaml#/schemas/WorkingTimeModelList '403': $ref: ../../../adsum-core/openapi/components/common.yaml#/responses/Forbidden Schemas ======= Reusable schemas of the core in ``common.yaml`` include: - ``Uuid``, ``Date``, ``DateTime``, ``TimeOfDay`` and ``DurationSeconds`` for identifiers, dates, instants, wall-clock times and durations - ``Days`` for quantities of days in steps of 0.5 - ``Locale`` and ``LocalisedText``, an object with the properties ``en`` and ``de`` - ``DayReason`` and ``AbsenceCategory``, the shared vocabularies of working-day calendar and absences - ``ListScope`` (``self``, ``team``, ``company``) - ``EmployeeReference`` with ``id``, ``displayName`` and ``avatarUrl`` - ``PageMeta`` for paging and ``Problem`` and ``Violation`` for errors ``common.yaml`` also holds the shared parameters (``CompanyId``, ``IdempotencyKey``, ``IfMatch``, ``Page``, ``PerPage``, ``Sort``, ``Facets``, ``IdPath``, ``DateFrom``, ``DateTo``) and responses (``BadRequest``, ``Unauthorized``, ``Forbidden``, ``NotFound``, ``Conflict``, ``PreconditionFailed``, ``PreconditionRequired``, ``UnprocessableEntity`` and others). Conventions =========== All packages follow these conventions: - JSON property names are camelCase. - Identifiers are UUIDs (version 7) as strings. - Calendar dates are ISO 8601 (``2026-10-01``). Instants are RFC 3339 with an offset (``2026-10-01T08:15:00+02:00``). - Durations are integer seconds in properties whose name ends in ``Seconds``. - Quantities of days are numbers in steps of 0.5. - Errors are RFC 9457 problem details (``application/problem+json``). Collections ----------- A list operation takes ``page`` (from 1), ``perPage`` (default 50, at most 200) and ``sort``, a comma-separated list of properties; a leading ``-`` sorts descending (``sort=name,-createdAt``). Filters are plain query parameters. The response has the rows in ``items`` and the paging data in ``meta``: .. code-block:: json { "items": [], "meta": {"page": 1, "perPage": 50, "totalItems": 127} } A controller rejects undocumented query parameters with ``AllowedQueryParameters::assert($request, ['scope', 'status'])``. For filters named in the ``facets`` parameter, ``meta.facets`` holds the number of rows per value. Optimistic locking ------------------ Updatable resources answer with an ``ETag`` header. A change (``PUT``, ``PATCH`` or an action on the resource) sends it in ``If-Match``; without it the answer is 428, with an outdated value 412. A controller checks it with ``EntityTag::forVersion($id, $version)->assertMatches($request)``. Idempotent creation ------------------- A ``POST`` marked with ``#[Idempotent]`` can be retried with an ``Idempotency-Key`` header. The same key and body within 24 hours return the original response; the same key with another body is rejected. Errors ------ An exception becomes a problem response when its class carries ``#[AsProblem]``: .. code-block:: php use WebVision\Adsum\Core\Shared\Api\Problem\AsProblem; #[AsProblem(status: 409, type: 'working-time-model-code-taken', title: 'The company already has a working-time model with this code.')] final class WorkingTimeModelCodeTakenException extends \DomainException {} The response contains ``type`` (``/problems/``), ``title``, ``status``, ``detail``, ``code`` (the exception code, for support and logs) and ``violations`` (field errors with ``propertyPath`` and ``message``). Controller ========== An API controller lives below ``src//UI/Api/``. It sets the route, declares the access and dispatches to the command bus: .. code-block:: php #[AsController] #[Route('/api/v1/working-time-models')] final readonly class WorkingTimeModelController { #[Route('', name: 'api_create_working_time_model', methods: ['POST'])] #[Access('hr', 'admin')] #[Idempotent] public function create(Request $request, #[CurrentUser] User $author): JsonResponse { // validate the payload, dispatch the command, respond with 201 and Location } } The ``#[Access]`` of each action equals the ``x-access`` of its operation.