API Documentation Generator
Document APIs from authoritative artifacts and distinguish confirmed facts from inference.
Source priority
Use the strongest available source in this order:
- Checked-in OpenAPI/Swagger contract and generated schema.
- Framework route definitions and annotations.
- Request/response models and validation rules.
- Authentication, middleware, exception handling, and error catalogs.
- Integration and contract tests.
- Runtime observations explicitly authorized by the user.
When sources disagree, report the mismatch instead of silently choosing one.
Framework routing
Read only the relevant reference:
- OpenAPI-first:
references/openapi-first.md
- Java/Kotlin Spring:
references/spring.md
- Python FastAPI:
references/fastapi.md
- TypeScript NestJS:
references/nestjs.md
- JavaScript/TypeScript Express:
references/express.md
- Go Gin:
references/gin.md
Use references/scan-and-generate-example.md only when a complete worked example is useful.
For a checked-in OpenAPI document, generate a deterministic inventory before drafting prose:
python3 scripts/inventory_openapi.py path/to/openapi.yaml --output docs/api-inventory.md
The script refuses to overwrite an existing output unless --force is explicitly provided.
Workflow
1. Inspect
- Detect the project framework, modules, existing API contracts, and documentation convention.
- Determine the requested scope from the user's wording; infer the whole API only when no narrower scope exists.
- Locate route registration, base paths, version prefixes, security middleware, models, validators, error handling, and tests.
- Identify generated files so they are not mistaken for the source of truth.
2. Build an endpoint inventory
For each endpoint capture:
| Field |
Requirement |
| Method and path |
Resolve all prefixes and route groups |
| Operation identity |
Handler/controller and stable operation name |
| Purpose |
Derive from code/comments; mark inference |
| Authentication |
Scheme, scopes/roles, and public exceptions |
| Request |
Path/query/header/cookie/body fields and validation |
| Response |
Status codes, content types, schema, and wrappers |
| Errors |
Only errors supported by handlers, middleware, tests, or catalogs |
| Evidence |
Contract or source location used |
Do not fabricate example values, undocumented error codes, required flags, or response fields.
3. Resolve schemas
- Follow nested model references and generic response wrappers.
- Preserve nullable, optional, enum, format, range, and length constraints.
- Detect pagination and list-envelope conventions.
- Show recursive or polymorphic schemas explicitly.
- Redact secrets and personal data from examples.
4. Generate the document
Use the repository's existing convention. If none exists, copy one template from assets/templates/ and write to a proposed path under docs/. Before writing, report the target and whether it already exists.
For every endpoint include:
- Summary and evidence status
- Method and full path
- Authentication/authorization
- Parameters and request body
- Responses and supported errors
- Minimal valid examples
- Source location
5. Validate
- Compare documented routes with the discovered inventory.
- Verify path variables and request fields against validators/models.
- Verify response and error status codes against code or contract.
- Check internal links and anchors.
- Mark unresolved conflicts and inferred fields.
- Never claim runtime verification unless the API was actually run.
Evidence labels
Use these labels when certainty matters:
已确认:契约 — present in the checked-in API contract.
已确认:源码 — present in route/model/handler source.
已确认:测试 — exercised by a test.
推断 — reasonable but not explicitly specified.
待确认 — conflicting or missing evidence.
Completion contract
Report the generated/updated file, documented endpoint count, authoritative sources used, contract/source mismatches, inferred fields, and endpoints skipped because evidence was insufficient.
1---2name: api-doc-generator3description: Generate or update evidence-backed API documentation from OpenAPI specifications, framework routes, controllers, request/response models, validation rules, and tests. Use when the user explicitly asks for API or interface documentation, route inventory, endpoint reference, OpenAPI-derived docs, or synchronization between code and API docs. Supports OpenAPI-first projects and framework-specific discovery for Spring, FastAPI, NestJS, Express, and Gin; do not use for generic product documentation.4license: Apache-2.05---67# API Documentation Generator89Document APIs from authoritative artifacts and distinguish confirmed facts from inference.1011## Source priority1213Use the strongest available source in this order:14151. Checked-in OpenAPI/Swagger contract and generated schema.162. Framework route definitions and annotations.173. Request/response models and validation rules.184. Authentication, middleware, exception handling, and error catalogs.195. Integration and contract tests.206. Runtime observations explicitly authorized by the user.2122When sources disagree, report the mismatch instead of silently choosing one.2324## Framework routing2526Read only the relevant reference:2728- OpenAPI-first: [`references/openapi-first.md`](references/openapi-first.md)29- Java/Kotlin Spring: [`references/spring.md`](references/spring.md)30- Python FastAPI: [`references/fastapi.md`](references/fastapi.md)31- TypeScript NestJS: [`references/nestjs.md`](references/nestjs.md)32- JavaScript/TypeScript Express: [`references/express.md`](references/express.md)33- Go Gin: [`references/gin.md`](references/gin.md)3435Use [`references/scan-and-generate-example.md`](references/scan-and-generate-example.md) only when a complete worked example is useful.3637For a checked-in OpenAPI document, generate a deterministic inventory before drafting prose:3839```bash40python3 scripts/inventory_openapi.py path/to/openapi.yaml --output docs/api-inventory.md41```4243The script refuses to overwrite an existing output unless `--force` is explicitly provided.4445## Workflow4647### 1. Inspect48491. Detect the project framework, modules, existing API contracts, and documentation convention.502. Determine the requested scope from the user's wording; infer the whole API only when no narrower scope exists.513. Locate route registration, base paths, version prefixes, security middleware, models, validators, error handling, and tests.524. Identify generated files so they are not mistaken for the source of truth.5354### 2. Build an endpoint inventory5556For each endpoint capture:5758| Field | Requirement |59|---|---|60| Method and path | Resolve all prefixes and route groups |61| Operation identity | Handler/controller and stable operation name |62| Purpose | Derive from code/comments; mark inference |63| Authentication | Scheme, scopes/roles, and public exceptions |64| Request | Path/query/header/cookie/body fields and validation |65| Response | Status codes, content types, schema, and wrappers |66| Errors | Only errors supported by handlers, middleware, tests, or catalogs |67| Evidence | Contract or source location used |6869Do not fabricate example values, undocumented error codes, required flags, or response fields.7071### 3. Resolve schemas7273- Follow nested model references and generic response wrappers.74- Preserve nullable, optional, enum, format, range, and length constraints.75- Detect pagination and list-envelope conventions.76- Show recursive or polymorphic schemas explicitly.77- Redact secrets and personal data from examples.7879### 4. Generate the document8081Use the repository's existing convention. If none exists, copy one template from `assets/templates/` and write to a proposed path under `docs/`. Before writing, report the target and whether it already exists.8283For every endpoint include:84851. Summary and evidence status862. Method and full path873. Authentication/authorization884. Parameters and request body895. Responses and supported errors906. Minimal valid examples917. Source location9293### 5. Validate9495- Compare documented routes with the discovered inventory.96- Verify path variables and request fields against validators/models.97- Verify response and error status codes against code or contract.98- Check internal links and anchors.99- Mark unresolved conflicts and inferred fields.100- Never claim runtime verification unless the API was actually run.101102## Evidence labels103104Use these labels when certainty matters:105106- `已确认:契约` — present in the checked-in API contract.107- `已确认:源码` — present in route/model/handler source.108- `已确认:测试` — exercised by a test.109- `推断` — reasonable but not explicitly specified.110- `待确认` — conflicting or missing evidence.111112## Completion contract113114Report the generated/updated file, documented endpoint count, authoritative sources used, contract/source mismatches, inferred fields, and endpoints skipped because evidence was insufficient.