Rest openapi

From orangehrm/orangehrm · View interactive version

Rest openapi is an AI agent skill from the GitHub repository orangehrm/orangehrm. Reference for OrangeHRM's OpenAPI v3 annotations via zircote/swagger-php — what to add to Endpoint methods and Model classes, the project's shared component refs (RecordNotFound, ForbiddenResponse, sortOrder, limit, offset), how class constants are used inside annotations, and the `generate-open-api

How to use this skill

Skill file

OpenAPI annotations

OrangeHRM uses zircote/swagger-php annotations to generate build/orangehrm-v2.json (and an HTML viewer) from the source. The annotations sit in PHP docblocks on:

CI runs php devTools/core/console.php generate-open-api-doc --throw in .github/workflows/linting.yml. --throw causes the command to fail non-zero on any parse error or missing schema reference — your PR will be red until the annotations are right.

Verify locally before pushing:

php devTools/core/console.php generate-open-api-doc --throw

Output lands in build/index.html (Swagger UI viewer) and build/orangehrm-v2.json (raw spec). Both are uploaded as CI artifacts on failure.

Annotation namespace

All annotations are under OpenApi\Annotations (aliased to OA in tooling). You don't need an explicit use OpenApi\Annotations as OA; at the top of the file — the swagger-php scanner resolves @OA\… based on its own conventions. Match the existing file style of whatever plugin you're working in.

Endpoint method annotations

Every handler method (the ones the framework dispatches to: getOne, getAll, create, update, delete) needs a @OA\<Verb> block immediately above it. Shape:

/**
 * @OA\Get(
 *     path="/api/v2/pim/employees/{empNumber}",          # the URL (must match routes.yaml)
 *     tags={"PIM/Employee"},                             # used for grouping in Swagger UI
 *     summary="Get an Employee",                         # one-line title
 *     operationId="get-an-employee",                     # globally unique kebab-case ID
 *     description="Retrieve details for a specific employee.",
 *
 *     @OA\PathParameter(
 *         name="empNumber",
 *         description="Specify the numerical employee number",
 *         @OA\Schema(type="integer")
 *     ),
 *     @OA\Parameter(
 *         name="model",
 *         description="Specify default or detailed response",
 *         in="query",
 *         required=false,
 *         @OA\Schema(
 *             type="string",
 *             enum={OrangeHRM\Pim\Api\EmployeeAPI::MODEL_DEFAULT, OrangeHRM\Pim\Api\EmployeeAPI::MODEL_DETAILED},
 *             default=OrangeHRM\Pim\Api\EmployeeAPI::MODEL_DEFAULT
 *         )
 *     ),
 *
 *     @OA\Response(
 *         response="200",
 *         description="Success",
 *         @OA\JsonContent(
 *             @OA\Property(
 *                 property="data",
 *                 oneOf={
 *                     @OA\Schema(ref="#/components/schemas/Pim-EmployeeModel"),
 *                     @OA\Schema(ref="#/components/schemas/Pim-EmployeeDetailedModel"),
 *                 }
 *             ),
 *             @OA\Property(property="meta", type="object", additionalProperties=false)
 *         )
 *     ),
 *     @OA\Response(response="404", ref="#/components/responses/RecordNotFound")
 * )
 *
 * @inheritDoc
 */
public function getOne(): EndpointResourceResult { … }

Required pieces per method:

The handler method's @inheritDoc stays — it picks up the docstring from the interface.

Path parameters, query parameters, request bodies

| Annotation | Use for |

|---|---|

| @OA\PathParameter(name=…, @OA\Schema(...)) | URL placeholders like {empNumber}. The name matches the route placeholder. |

| @OA\Parameter(name=…, in="query", required=…, @OA\Schema(...)) | Query string params. in is required. required=false for optional. |

| @OA\RequestBody(@OA\JsonContent(@OA\Property...)) | POST/PUT bodies. |

For DELETE collection endpoints — the project ships a shared DeleteRequestBody ref:

* @OA\RequestBody(ref="#/components/requestBodies/DeleteRequestBody")

Sends { "ids": [1, 2, 3] }. Use this for any bulk-delete endpoint.

Response annotations

Always specify 200 (or 201 for create — though the codebase uses 200 consistently). Then layer on error responses by reference:

@OA\Response(response="200", description="Success", @OA\JsonContent(...))
@OA\Response(response="404", ref="#/components/responses/RecordNotFound")
@OA\Response(response="403", ref="#/components/responses/ForbiddenResponse")

For bulk-delete responses:

@OA\Response(response="200", ref="#/components/responses/DeleteResponse")

Common refs (`src/plugins/orangehrmCorePlugin/Controller/Rest/V2/OpenApi.php`)

The project defines these shared components — reference them rather than redefining:

Responses

| Ref | Use |

|---|---|

| #/components/responses/RecordNotFound | 404 — the standard {error:{status:"404", message:"Record Not Found"}} shape |

| #/components/responses/ForbiddenResponse | 403 — standard unauthorized shape |

| #/components/responses/DeleteResponse | 200 for bulk delete — {data: [int...], meta: {}} |

Request bodies

| Ref | Use |

|---|---|

| #/components/requestBodies/DeleteRequestBody | {ids: [int...]} for bulk delete |

Parameters

| Ref | Use |

|---|---|

| #/components/parameters/sortOrder | Query param sortOrder enum {ASC, DESC} |

| #/components/parameters/limit | Query param limit integer default 50 |

| #/components/parameters/offset | Query param offset integer default 0 |

So for any list endpoint with sort/page support:

* @OA\Parameter(ref="#/components/parameters/sortOrder"),
* @OA\Parameter(ref="#/components/parameters/limit"),
* @OA\Parameter(ref="#/components/parameters/offset"),
* @OA\Parameter(
*     name="sortField",
*     in="query",
*     required=false,
*     @OA\Schema(type="string", enum=EmployeeSearchFilterParams::ALLOWED_SORT_FIELDS)
* ),

(sortField isn't a shared ref because the allowed values are resource-specific — reference the DTO's ALLOWED_SORT_FIELDS constant.)

Using PHP constants inside annotations

swagger-php evaluates class constants written as FQCN or short name (if used) inside annotation expressions:

* @OA\Schema(
*     type="string",
*     maxLength=OrangeHRM\Pim\Api\EmployeeAPI::PARAM_RULE_FILTER_NAME_MAX_LENGTH,
*     enum={OrangeHRM\Pim\Api\EmployeeAPI::MODEL_DEFAULT, OrangeHRM\Pim\Api\EmployeeAPI::MODEL_DETAILED},
* )

Or with imports already in the file:

* @OA\Schema(type="string", enum=EmployeeSearchFilterParams::ALLOWED_SORT_FIELDS)

This is the right pattern — duplicating the same constant value as a literal in the annotation creates drift when the constant changes. The CI doc-gen evaluates the constants at scan time; if the constant disappears or is renamed, generation fails (caught by --throw).

Constants are commonly used for:

Model `@OA\Schema` annotations

Every Model class gets a @OA\Schema block above the class declaration. The schema name follows <Plugin>-<ModelName> convention — referenced from endpoint responses via #/components/schemas/<Plugin>-<ModelName>.

/**
 * @OA\Schema(
 *     schema="Pim-EmployeeModel",
 *     type="object",
 *     @OA\Property(property="empNumber", description="The employee number", type="integer"),
 *     @OA\Property(property="lastName", description="The last name", type="string"),
 *     @OA\Property(property="firstName", description="The first name", type="string"),
 *     @OA\Property(property="middleName", description="The middle name", type="string"),
 *     @OA\Property(property="employeeId", description="The employee ID", type="string"),
 *     @OA\Property(property="terminationId", description="ID of the termination record", type="integer"),
 * )
 */
class EmployeeModel implements Normalizable
{
    use ModelTrait;
    // ...
}

Property listing rules:

```php

```

```php

```

Models with variants — `oneOf`

When an endpoint can return either default or detailed (the MODEL_MAP pattern from rest-serialization):

@OA\Property(
    property="data",
    oneOf={
        @OA\Schema(ref="#/components/schemas/Pim-EmployeeModel"),
        @OA\Schema(ref="#/components/schemas/Pim-EmployeeDetailedModel"),
    }
)

Both schemas must be declared (each on its respective Model class).


Recipes

Recipe 1 — Annotating a list (collection) endpoint

/**
 * @OA\Get(
 *     path="/api/v2/x/widgets",
 *     tags={"X/Widget"},
 *     summary="List All Widgets",
 *     operationId="list-all-widgets",
 *     @OA\Parameter(
 *         name="name",
 *         description="Filter by widget name",
 *         in="query",
 *         required=false,
 *         @OA\Schema(type="string", maxLength=OrangeHRM\X\Api\WidgetAPI::PARAM_RULE_NAME_MAX_LENGTH)
 *     ),
 *     @OA\Parameter(
 *         name="sortField",
 *         in="query",
 *         required=false,
 *         @OA\Schema(type="string", enum=WidgetSearchFilterParams::ALLOWED_SORT_FIELDS)
 *     ),
 *     @OA\Parameter(ref="#/components/parameters/sortOrder"),
 *     @OA\Parameter(ref="#/components/parameters/limit"),
 *     @OA\Parameter(ref="#/components/parameters/offset"),
 *     @OA\Response(
 *         response="200",
 *         description="Success",
 *         @OA\JsonContent(
 *             @OA\Property(
 *                 property="data",
 *                 type="array",
 *                 @OA\Items(ref="#/components/schemas/X-WidgetModel")
 *             ),
 *             @OA\Property(property="meta", type="object",
 *                 @OA\Property(property="total", description="Total widget count", type="integer")
 *             )
 *         )
 *     ),
 * )
 */
public function getAll(): EndpointCollectionResult { … }

Recipe 2 — Annotating a resource (single) endpoint

/**
 * @OA\Get(
 *     path="/api/v2/x/widgets/{id}",
 *     tags={"X/Widget"},
 *     summary="Get a Widget",
 *     operationId="get-a-widget",
 *     @OA\PathParameter(name="id", @OA\Schema(type="integer")),
 *     @OA\Response(
 *         response="200",
 *         description="Success",
 *         @OA\JsonContent(
 *             @OA\Property(property="data", ref="#/components/schemas/X-WidgetModel"),
 *             @OA\Property(property="meta", type="object", additionalProperties=false)
 *         )
 *     ),
 *     @OA\Response(response="404", ref="#/components/responses/RecordNotFound")
 * )
 */
public function getOne(): EndpointResourceResult { … }

Recipe 3 — Annotating a POST (create)

/**
 * @OA\Post(
 *     path="/api/v2/x/widgets",
 *     tags={"X/Widget"},
 *     summary="Create a Widget",
 *     operationId="create-a-widget",
 *     @OA\RequestBody(
 *         @OA\JsonContent(
 *             @OA\Property(
 *                 property="name",
 *                 type="string",
 *                 maxLength=OrangeHRM\X\Api\WidgetAPI::PARAM_RULE_NAME_MAX_LENGTH
 *             ),
 *             required={"name"}
 *         )
 *     ),
 *     @OA\Response(
 *         response="200",
 *         description="Success",
 *         @OA\JsonContent(
 *             @OA\Property(property="data", ref="#/components/schemas/X-WidgetModel"),
 *             @OA\Property(property="meta", type="object", additionalProperties=false)
 *         )
 *     ),
 * )
 */
public function create(): EndpointResourceResult { … }

Recipe 4 — Annotating a bulk DELETE

/**
 * @OA\Delete(
 *     path="/api/v2/x/widgets",
 *     tags={"X/Widget"},
 *     summary="Delete Widgets",
 *     operationId="delete-widgets",
 *     @OA\RequestBody(ref="#/components/requestBodies/DeleteRequestBody"),
 *     @OA\Response(response="200", ref="#/components/responses/DeleteResponse")
 * )
 */
public function delete(): EndpointResourceResult { … }

Recipe 5 — A new Model with nested object

/**
 * @OA\Schema(
 *     schema="X-WidgetModel",
 *     type="object",
 *     @OA\Property(property="id", type="integer"),
 *     @OA\Property(property="name", type="string"),
 *     @OA\Property(property="owner", type="object",
 *         @OA\Property(property="empNumber", type="integer"),
 *         @OA\Property(property="lastName", type="string")
 *     ),
 * )
 */
class WidgetModel implements Normalizable { use ModelTrait; … }

Aligns with a setAttributeNames(['id', 'name', ['owner', 'empNumber'], ['owner', 'lastName']]) in the model constructor.


Checklists

Add OpenAPI annotations to a new endpoint

Add OpenAPI schema to a new Model

Debug a `generate-open-api-doc --throw` failure

Things that bite

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.