api-docs-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAPI Docs Generator
API文档生成工具
Audits API endpoint documentation for completeness, generates enhanced docstrings with
proper parameter descriptions and examples, documents all response codes, and produces
Pydantic model examples — bridging the gap between auto-generated OpenAPI specs and
genuinely useful API documentation.
审核API端点文档的完整性,生成包含恰当参数描述和示例的增强型文档字符串,记录所有响应码,并生成Pydantic模型示例——填补自动生成的OpenAPI规范与真正实用的API文档之间的差距。
Reference Files
参考文件
| File | Contents | Load When |
|---|---|---|
| FastAPI-specific documentation patterns, Path/Query/Body parameter docs | FastAPI endpoint |
| Creating realistic field examples, model_config patterns | Example values needed |
| Standard HTTP response documentation, error response schemas | Response documentation needed |
| OpenAPI spec enrichment, tag organization, schema documentation | OpenAPI spec review |
| 文件路径 | 内容 | 加载时机 |
|---|---|---|
| FastAPI专属的文档模式、路径/查询/请求体参数文档 | 处理FastAPI端点时 |
| 创建真实字段示例、model_config模式 | 需要生成示例值时 |
| 标准HTTP响应文档、错误响应 schema | 需要编写响应文档时 |
| OpenAPI规范增强、标签组织、schema文档 | 审核OpenAPI规范时 |
Prerequisites
前提条件
- Access to the API source code (route definitions, models)
- Framework identification (FastAPI, Flask, Django REST, Express)
- 能够访问API源代码(路由定义、模型)
- 已识别框架类型(FastAPI、Flask、Django REST、Express)
Workflow
工作流程
Phase 1: Analyze Endpoints
第一阶段:分析端点
- Inventory endpoints — List all routes with HTTP method, path, handler function.
- Identify models — Request bodies (Pydantic models, dataclasses), response models, query parameters, path parameters.
- Map dependencies — Authentication requirements, middleware, shared dependencies.
- Read existing docs — Current docstrings, OpenAPI metadata, inline documentation.
- 盘点端点 —— 列出所有路由,包含HTTP方法、路径、处理函数。
- 识别模型 —— 请求体(Pydantic模型、数据类)、响应模型、查询参数、路径参数。
- 映射依赖项 —— 认证要求、中间件、共享依赖。
- 读取现有文档 —— 当前的文档字符串、OpenAPI元数据、内联文档。
Phase 2: Audit Documentation
第二阶段:审核文档
For each endpoint, check:
| Check | What to Verify | Common Gap |
|---|---|---|
| Endpoint description | Handler has a docstring | Missing or "TODO" |
| Parameter descriptions | Each param has | Path params undocumented |
| Request example | Body model has | No request example |
| Response model | | Returns raw dict |
| Error responses | 4xx/5xx documented with | Only 200 documented |
| Tags | Endpoint assigned to a tag group | Untagged endpoints |
针对每个端点,检查以下内容:
| 检查项 | 验证内容 | 常见缺失 |
|---|---|---|
| 端点描述 | 处理函数是否有文档字符串 | 缺失或标记为"TODO" |
| 参数描述 | 每个参数是否包含 | 路径参数未文档化 |
| 请求示例 | 请求体模型是否有 | 无请求示例 |
| 响应模型 | 是否指定了 | 返回原始字典 |
| 错误响应 | 是否通过 | 仅记录了200响应 |
| 标签 | 端点是否分配到标签组 | 端点未标记标签 |
Phase 3: Generate Enhancements
第三阶段:生成增强内容
- Docstrings — Write clear endpoint descriptions that explain purpose, not implementation. Include Raises section for documented errors.
- Parameter metadata — Add ,
description,example/ge/leto Path, Query, Body parameters.regex - Model examples — Add and
Field(example=...)withmodel_config.json_schema_extra - Error responses — Document every possible error status code with response schema.
- Tags — Group endpoints by resource or feature area.
- 文档字符串 —— 编写清晰的端点描述,说明用途而非实现细节。包含已记录错误的Raises章节。
- 参数元数据 —— 为路径、查询、请求体参数添加、
description、example/ge/le。regex - 模型示例 —— 添加和带有
Field(..., example=...)的json_schema_extra。model_config - 错误响应 —— 记录所有可能的错误状态码及对应的响应schema。
- 标签 —— 按资源或功能领域对端点进行分组。
Phase 4: Output
第四阶段:输出
Produce a coverage report and enhanced code.
生成覆盖率报告和增强后的代码。
Output Format
输出格式
undefinedundefinedAPI Documentation Audit
API文档审核报告
Coverage Summary
覆盖率摘要
| Metric | Count | Documented | Coverage |
|---|---|---|---|
| Endpoints | {N} | {M} | {%} |
| Parameters | {N} | {M} | {%} |
| Response codes | {N} | {M} | {%} |
| Models with examples | {N} | {M} | {%} |
| 指标 | 总数 | 已文档化 | 覆盖率 |
|---|---|---|---|
| 端点 | {N} | {M} | {%} |
| 参数 | {N} | {M} | {%} |
| 响应码 | {N} | {M} | {%} |
| 带示例的模型 | {N} | {M} | {%} |
Gaps Identified
识别出的缺失项
| # | Endpoint | Issue | Severity |
|---|---|---|---|
| 1 | | {issue} | {High/Medium/Low} |
| 序号 | 端点 | 问题 | 严重程度 |
|---|---|---|---|
| 1 | | {issue} | {高/中/低} |
Enhanced Code
增强后的代码
{METHOD} {path}
{METHOD} {path}{METHOD} {path}
{METHOD} {path}python
@router.{method}(
"{path}",
response_model={ResponseModel},
summary="{Short summary}",
responses={{
404: {{"description": "{Not found description}"}},
422: {{"description": "Validation error"}},
}},
tags=["{tag}"],
)
async def {handler}(
{param}: {type} = Path(..., description="{description}", example={example}),
) -> {ResponseModel}:
"""
{Full description of what this endpoint does.}
{Additional context about behavior, side effects, or important notes.}
Raises:
404: {Entity} not found
403: Insufficient permissions
"""python
@router.{method}(
"{path}",
response_model={ResponseModel},
summary="{简短摘要}",
responses={{
404: {{"description": "{资源未找到描述}"}},
422: {{"description": "验证错误"}},
}},
tags=["{tag}"],
)
async def {handler}(
{param}: {type} = Path(..., description="{描述}", example={example}),
) -> {ResponseModel}:
"""
{该端点的完整功能描述}
{关于行为、副作用或重要注意事项的额外说明}
Raises:
404: {实体}未找到
403: 权限不足
"""Model: {ModelName}
{ModelName}模型:{ModelName}
{ModelName}python
class {ModelName}(BaseModel):
{field}: {type} = Field(..., description="{description}", example={example})
model_config = ConfigDict(
json_schema_extra={{
"example": {{
"{field}": {example_value},
}}
}}
)text
undefinedpython
class {ModelName}(BaseModel):
{field}: {type} = Field(..., description="{描述}", example={example})
model_config = ConfigDict(
json_schema_extra={{
"example": {{
"{field}": {example_value},
}}
}}
)text
undefinedCalibration Rules
校准规则
- Describe behavior, not implementation. "Retrieves the user's profile" is good.
"Calls " is implementation leakage.
db.query(User).filter_by(id=id).first() - Realistic examples. not
"alice@example.com"."string"not42. Examples serve as documentation — they should look like real data.0 - Document every error code. If the endpoint can return 404, document it. Users should never encounter an undocumented error response.
- Consistent style. All endpoints in the same API should use the same documentation patterns — same tag naming, same description style, same example format.
- Don't duplicate the type system. If the parameter type is , don't write "An integer" as the description. Write what the integer represents: "Unique user identifier."
int
- 描述行为而非实现细节。"获取用户资料"是合适的描述,"调用"属于实现细节泄露,不应出现在文档中。
db.query(User).filter_by(id=id).first() - 使用真实示例。使用而非
"alice@example.com",使用"string"而非42。示例本身就是文档的一部分,应贴近真实数据。0 - 记录所有错误码。如果端点可能返回404,就必须记录它。用户不应遇到未文档化的错误响应。
- 保持风格一致。同一API中的所有端点应使用相同的文档模式——相同的标签命名、相同的描述风格、相同的示例格式。
- 不要重复类型系统信息。如果参数类型是,不要写"一个整数"作为描述,而是说明该整数代表什么:"唯一用户标识符"。
int
Error Handling
错误处理
| Problem | Resolution |
|---|---|
| Non-FastAPI framework | Adapt patterns. Document the HTTP contract regardless of framework. |
| No type hints on handlers | Infer types from usage, document uncertainty, suggest adding type hints. |
| Massive API (50+ endpoints) | Prioritize undocumented and public endpoints. Batch output by resource. |
| Generated API (OpenAPI → code) | Document at the spec level, not the generated code level. |
| Authentication varies by endpoint | Document auth requirements per endpoint group. |
| 问题 | 解决方法 |
|---|---|
| 非FastAPI框架 | 调整文档模式。无论使用何种框架,都要记录HTTP契约。 |
| 处理函数没有类型提示 | 从使用情况推断类型,记录不确定性,并建议添加类型提示。 |
| 大型API(50+端点) | 优先处理未文档化的公开端点。按资源分批输出结果。 |
| 生成式API(OpenAPI → 代码) | 在规范层面进行文档化,而非生成的代码层面。 |
| 端点间认证要求不同 | 按端点组记录认证要求。 |
When NOT to Generate
请勿生成的场景
Push back if:
- The API design itself is wrong (bad URL patterns, wrong HTTP methods) — fix the API first
- The user wants SDK generation from OpenAPI — different tool
- The code is a prototype that will change significantly — document after stabilization
undefined在以下情况下,应拒绝生成请求:
- API设计本身存在问题(不良URL模式、错误的HTTP方法)—— 先修复API设计
- 用户希望从OpenAPI生成SDK—— 这属于不同工具的功能范畴
- 代码是会大幅变更的原型—— 待代码稳定后再进行文档化
undefined