|
| 1 | +--- |
| 2 | +title: OpenAPI Specification and Implementation |
| 3 | +description: Guidelines for maintaining OpenAPI specifications in sync with API implementation |
| 4 | +globs: |
| 5 | + - "docs/openapi/**" |
| 6 | + - "src/**" |
| 7 | +alwaysApply: false |
| 8 | +version: "1.0.0" |
| 9 | +--- |
| 10 | + |
| 11 | +# OpenAPI Specification and Implementation |
| 12 | + |
| 13 | +## Context |
| 14 | +When changes touch API specification files under `docs/openapi` **or** implementation files under `src`, this rule applies. |
| 15 | + |
| 16 | +## Core Principles |
| 17 | + |
| 18 | +### Specification-Implementation Sync |
| 19 | +- Any new endpoint, modification of existing endpoint, or removal must include **both** the OpenAPI spec change **and** the matching implementation in `src` |
| 20 | +- If the implementation is deferred, the spec's endpoint description **must start with** `Not implemented yet` |
| 21 | +and the actual handler must return HTTP **501** with a clear JSON message: |
| 22 | +```json |
| 23 | +{ "error": "Not implemented yet: <feature-name>" } |
| 24 | +``` |
| 25 | +- Keep examples in sync with actual API behavior |
| 26 | + |
| 27 | +### Schema Management |
| 28 | +- All request/response bodies and parameters **must reference** schemas in `schemas.yaml` |
| 29 | +- Reuse existing schemas wherever possible to avoid duplication |
| 30 | +- Avoid redefining data structures inline unless absolutely necessary |
| 31 | +- Use **composition, inheritance, polymorphism** (as supported by OpenAPI 3.1.1) to reuse common parts |
| 32 | +- When introducing new audit types, update: |
| 33 | + - Audit type enum |
| 34 | + - Audit result schema |
| 35 | + - At least one complete example |
| 36 | + |
| 37 | +### Examples |
| 38 | +- Add examples to `examples.yaml` and reference them (don't inline) |
| 39 | +- Include at least one request and response example per endpoint |
| 40 | +- Examples must validate against the schema and represent realistic, production-like data |
| 41 | + |
| 42 | +### Precision & Consistency |
| 43 | +- Keep definitions as precise as possible (correct types, constraints, formats, etc.) |
| 44 | +- Endpoints under the same tag must follow consistent naming, path structure, response format, etc. |
| 45 | + |
| 46 | +### Validation & Documentation Build |
| 47 | +- After modifying OpenAPI specs, **always run** `npm run docs:lint` to validate the specification |
| 48 | +- Before completing implementation, **must run** `npm run docs:build` to generate documentation |
| 49 | +- Fix any linting errors or build failures before considering the task complete |
| 50 | + |
| 51 | +## API Design Patterns |
| 52 | + |
| 53 | +### Pagination |
| 54 | +- Set strict limits on collection resource items returned |
| 55 | +- Support optional `limit` query parameter with documented defaults, min/max values |
| 56 | +- Use cursor-based pagination with `cursor` query parameter |
| 57 | +- Response format: `{ "cursor": "next-token", "items": [...] }` |
| 58 | +- Omit `cursor` property when no more items exist |
| 59 | +- Use consistent naming: `limit` and `cursor` across all paginated endpoints |
| 60 | + |
| 61 | +### Bulk Operations |
| 62 | +- **Prefer bulk endpoints over separate single/multi-item endpoints** |
| 63 | +- Accept arrays containing one or more items |
| 64 | +- Document explicitly in description that single items can be passed in arrays |
| 65 | +- Set strict limits on bulk POST/PUT/PATCH/DELETE request sizes |
| 66 | +- Response format: `{ "metadata": { "total": N, "success": N, "failure": N }, "failures": [...], "items": [...] }` |
| 67 | +- Specify atomicity behavior and per-item error reporting |
| 68 | + |
| 69 | +### Parameters |
| 70 | +- For URLs passed as path parameters, use {base64Url} and encode as URL-safe base64 without padding (RFC 4648 §5) to avoid +/= issues in paths |
| 71 | +- Maintain consistent naming across all endpoints |
| 72 | + |
| 73 | +### POST Requests |
| 74 | +- Document idempotency behavior explicitly |
| 75 | +- Specify duplicate detection mechanism if applicable |
| 76 | +- Indicate whether upserts are supported |
| 77 | + |
| 78 | +### PATCH Requests |
| 79 | +- Implementation must update only fields provided in request. If other fields are modified or reset, document this behavior clearly in the endpoint description |
0 commit comments