OpenAPI 设计辅助
让 Agent 按资源建模、统一错误体与分页游标生成可校验的 OpenAPI 3.x 草稿,便于评审与代码生成;本页按「流程 → 元数据 → 路径 → Schema → 错误 → 版本 → 校验 → 骨架实验室」展开。
SKILL 固定 info.version、servers 与 securitySchemes;路径命名使用复数资源与嵌套深度上限,查询参数与请求体 schema 复用 components。
错误响应采用 Problem Details(RFC 7807)或团队统一 envelope,并列举典型 4xx/5xx;成功响应附带 request/response 示例与 discriminator 说明。
破坏性变更通过路径或 header 版本共存描述清楚;生成 YAML 后要求通过 spectral 等规则集,Agent 输出修复列表而非静默忽略告警。
- 必填字段、可空与默认值在 schema 层显式声明。
- 分页优先 cursor,offset 需注明大数据集风险。
- Webhook 与回调单独 tag,附签名校验说明占位。
$ref 与 components 复用示例
# OpenAPI 3.0 $ref 和 components 复用——避免内联重复定义
components:
schemas:
# 基础实体
User:
type: object
required: [id, email]
properties:
id: { type: string, format: uuid }
email: { type: string, format: email }
name: { type: string, nullable: true }
# 游标分页响应(复用)
UserPage:
type: object
required: [data, pagination]
properties:
data:
type: array
items:
$ref: '#/components/schemas/User' # 引用而非内联
pagination:
$ref: '#/components/schemas/CursorPage'
# 游标分页 vs offset 分页的 OpenAPI 定义对比
CursorPage: # cursor-based:适合大数据集,稳定翻页
type: object
properties:
nextCursor: { type: string, nullable: true, description: null 表示最后一页 }
hasNextPage: { type: boolean }
limit: { type: integer, minimum: 1, maximum: 100 }
OffsetPage: # offset-based:简单但大偏移性能差,需注明风险
type: object
description: "⚠ 大数据集时 offset 性能下降,超过10万条记录建议改用 cursor"
properties:
total: { type: integer }
page: { type: integer, minimum: 1 }
limit: { type: integer, minimum: 1, maximum: 100 }
# oneOf + discriminator:多态类型
Notification:
oneOf:
- $ref: '#/components/schemas/EmailNotification'
- $ref: '#/components/schemas/SmsNotification'
discriminator:
propertyName: type # 客户端用此字段判断具体类型
mapping:
email: '#/components/schemas/EmailNotification'
sms: '#/components/schemas/SmsNotification'
从需求到可合并草稿(流程)
[ 资源与动词清单 ]
│
▼
┌─────────────┐ 固定:info / servers / security / tags
│ 元数据骨架 │──── 约定:版本号语义、环境 URL、鉴权方式
└─────────────┘
│
▼
┌─────────────┐ 复数路径、嵌套深度、查询与分页参数
│ paths 草案 │──── 每个 operation:summary、operationId、参数引用
└─────────────┘
│
▼
┌─────────────┐ 请求/响应 schema、示例、错误引用
│ components │──── discriminator / oneOf 写清多态规则
└─────────────┘
│
▼
┌─────────────┐ Spectral / CI;破坏性变更说明与迁移
│ 校验与评审 │──── 输出告警修复项,不吞规则
└─────────────┘
合流顺序:先对齐团队模板(错误体、分页、鉴权),再填路径与示例,最后跑规则集;避免在缺少 security 与错误 schema 时大量复制粘贴内联对象。
info、servers 与 securitySchemes
info.title / description 面向人类读者;contact 与 license 按仓库策略填写。servers 列出 dev/stage/prod 或占位变量,避免硬编码仅本地 URL。
# 安全方案(security schemes)完整定义示例
components:
securitySchemes:
BearerJWT:
type: http
scheme: bearer
bearerFormat: JWT
description: "RS256 签名的 JWT,issuer=https://auth.example.com"
OAuth2:
type: oauth2
flows:
authorizationCode:
authorizationUrl: https://auth.example.com/authorize
tokenUrl: https://auth.example.com/token
scopes:
read:items: "读取条目列表"
write:items: "创建和更新条目"
ApiKey:
type: apiKey
in: header
name: X-API-Key
# 全局 security(默认要求)+ per-operation 覆盖例外
security:
- BearerJWT: []
paths:
/public/status:
get:
security: [] # 覆盖全局:此接口无需认证(需注释说明原因)
- 多租户或区域前缀放在 server variable 或 path 前缀中二选一,全文档统一。
路径与资源建模
使用复数集合名(/users、/users/{userId});子资源嵌套不超过团队约定层数(常见 2~3)。operationId 稳定且可用于代码生成。
推荐
- 过滤、排序、字段裁剪用查询参数,并在 description 写清默认与上限
- cursor +
limit;可选page时标注适用场景 - 批量操作用单独 path 或明确
Prefer语义(若采用)
避免
- 动词塞进 path(除 RPC 风格已团队批准)
- 同一资源多种 URL 形状无 migration 说明
- 未文档化的 magic query 开关
components 复用与约束
公共类型放入 components.schemas;请求体与响应用 $ref。required、nullable、default、enum、format(uuid、date-time 等)显式写出。
- 分页响应、错误体、空成功体各一个 schema,避免每处重写。
- 多态用
discriminator并在 description 说明扩展策略。 - 示例与 schema 同步更新;CI 可校验 example 通过 schema。
错误体与典型状态码
统一 4xx/5xx 的 response content:RFC 7807 application/problem+json 或团队 envelope(含 code、message、details 等字段约定)。
- 列举
400、401、403、404、409、422、429、500等与业务相关的含义。 429附Retry-After说明(文档层描述即可)。
版本策略与破坏性变更
路径前缀(/v1)、header(Accept-Version)或子域名择一;文档中写清「何时 bump 主版本」与共存期。
破坏性变更示例:删除字段、改类型、改枚举语义、改鉴权要求。须在 description 或独立 migration 段落列出,并在同一文档内保留旧版 path 或标注 sunset。
Spectral 与规则集
使用团队 Spectral 规则集或 OWASP/API 规范扩展;PR 或 CI 中执行,失败时输出规则 id 与定位路径。
- Agent 对每条告警给出「改契约 / 改规则 / 豁免理由」三选一,禁止静默
ignore而不留记录。 - 与 JSON Schema draft 版本(OpenAPI 3.0 vs 3.1)保持一致,避免混用特性。
OpenAPI 骨架实验室
选择 OpenAPI 版本、鉴权与错误体风格,填写 API 标题后生成可粘贴的 YAML 骨架;再按需补 paths 与 components。
生成内容不含具体 paths;将 components.schemas.Problem 或 ErrorEnvelope 用 $ref 挂到各 operation 的 responses。Bearer 与 OAuth2 勿同时当作唯一方案 unless 网关支持。
---
name: openapi-design-cn
description: 起草 OpenAPI 3.0/3.1 契约、错误模型与可评审示例
---
# Schema 与 components
1. 公共类型放入 components.schemas,路径用 $ref 引用,禁止内联重复
2. 分页:cursor-based 优先(nextCursor + hasNextPage),offset 注明性能风险
3. 多态:oneOf/anyOf + discriminator.propertyName,映射表完整
4. 必填字段显式 required,可空字段 nullable: true,枚举值完整列出
# 安全方案
5. securitySchemes 与网关配置一致:Bearer/OAuth2/ApiKey 选一并文档化
6. 全局 security 默认要求;例外接口 per-operation 覆盖并注释原因
7. OAuth2 scopes 与内部 RBAC 映射表写进 description
# 版本迁移与弃用
8. deprecated: true 标注弃用字段/操作,reason 说明替代方案
9. Sunset / Deprecation 响应头与文档日期对齐
10. 破坏性变更走新路径前缀(/v2/)并存,旧路径保留迁移周期
# 错误体
11. components.responses 集中定义:400/401/403/404/409/422/429/500
12. 采用 application/problem+json(RFC 7807),type 字段为文档 URL
13. 429 附 Retry-After 说明,500 不泄露内部 stack trace
# 校验
14. Spectral 规则集 CI 强制执行,告警逐条处理:改契约/改规则/豁免+注释
15. JSON Schema draft 版本与 OpenAPI 版本一致(3.0 vs 3.1 不混用特性)