openapi
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOpenAPI
OpenAPI
Overview
概述
OpenAPI Specification (OAS) 3.1 is the industry standard for describing HTTP APIs. It defines a machine-readable contract covering endpoints, request/response schemas, authentication, and error formats. OpenAPI 3.1 is a strict superset of JSON Schema Draft 2020-12, enabling full JSON Schema compatibility for data validation and type generation.
When to use: Designing REST APIs, generating typed clients (TypeScript, Python, Go), producing interactive documentation, validating request/response payloads, contract-first API development, API gateway configuration.
When NOT to use: GraphQL APIs (use the GraphQL schema), gRPC services (use Protocol Buffers), WebSocket-only protocols, internal function calls that never cross a network boundary.
OpenAPI 规范(OAS)3.1 是描述 HTTP API 的行业标准。它定义了一种机器可读的契约,涵盖端点、请求/响应 Schema、认证和错误格式。OpenAPI 3.1 是 JSON Schema Draft 2020-12 的严格超集,支持完全兼容 JSON Schema 的数据验证和类型生成。
适用场景: 设计 REST API、生成类型化客户端(TypeScript、Python、Go)、制作交互式文档、验证请求/响应负载、契约优先的 API 开发、API 网关配置。
不适用场景: GraphQL API(请使用 GraphQL Schema)、gRPC 服务(请使用 Protocol Buffers)、仅 WebSocket 协议、不跨网络边界的内部函数调用。
Quick Reference
快速参考
| Pattern | Element | Key Points |
|---|---|---|
| Document root | | |
| Path item | | Curly braces for path parameters |
| Operation | | Each operation needs |
| Parameters | | Path params are always |
| Request body | | Keyed by media type ( |
| Response | | At least one response required per operation |
| Component ref | | Reuse schemas, parameters, responses |
| Schema types | | Arrays support |
| Composition | | Model polymorphism and intersection types |
| Discriminator | | Hint for code generators with |
| Security | | Bearer, API key, OAuth2, OpenID Connect |
| Tags | | Group operations for documentation |
| Type generation | | Zero-runtime TypeScript types from spec |
| Typed fetch | | Type-safe HTTP client using generated types |
| React Query | | Type-safe React Query hooks from spec |
| Schema-first | | Generate OpenAPI documents from Zod schemas |
| 模式 | 元素 | 核心要点 |
|---|---|---|
| 文档根节点 | | 顶层必须包含 |
| 路径项 | | 路径参数使用大括号包裹 |
| 操作方法 | | 每个操作需包含 |
| 参数 | | 路径参数始终需要设置 |
| 请求体 | | 按媒体类型(如 |
| 响应 | | 每个操作至少需要定义一个响应 |
| 组件引用 | | 复用 Schema、参数、响应等组件 |
| Schema 类型 | | 3.1 版本中数组支持 |
| 组合类型 | | 实现多态模型和交叉类型 |
| 鉴别器 | | 为使用 |
| 安全机制 | | 支持 Bearer、API Key、OAuth2、OpenID Connect 等方式 |
| 标签 | 操作上的 | 为文档中的操作进行分组 |
| 类型生成 | | 从规范生成零运行时的 TypeScript 类型 |
| 类型化请求 | | 使用生成的类型创建类型安全的 HTTP 客户端 |
| React Query 集成 | | 从规范生成类型安全的 React Query 钩子 |
| Schema 优先开发 | | 从 Zod Schema 生成 OpenAPI 文档 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Using | Use |
Missing | Always set unique |
Path parameter not in | Path parameters are always required ( |
| Inline schemas everywhere | Extract to |
| Merge |
| Discriminator without shared property | All schemas in |
Empty | Every response needs a meaningful |
Using | Always define |
Circular | Break cycles with lazy resolution or restructure schemas |
| Mixing 3.0 and 3.1 syntax | Choose one version; 3.1 drops |
| 错误内容 | 正确做法 |
|---|---|
在3.1版本中使用 | 使用 |
操作中缺失 | 始终设置唯一的 |
路径参数未设置 | 路径参数始终需要设置 |
| 随处使用内联Schema | 提取到 |
| 合并 |
| 鉴别器未关联共享属性 | |
响应中 | 每个响应都需要设置有意义的 |
使用 | 始终定义 |
| 通过延迟解析或重构Schema来打破循环依赖 |
| 混合使用3.0和3.1版本语法 | 选择单一版本;3.1版本移除了 |
Delegation
任务委托
- API design review: Use agent to audit spec completeness and consistency
Task - Type generation: Use agent to find project-specific OpenAPI tooling config
Explore - Code review: Delegate to agent for generated client usage patterns
code-reviewer
If theskill is available, delegate advanced TypeScript typing questions to it.typescript-patterns
- API 设计评审:使用代理审核规范的完整性和一致性
Task - 类型生成:使用代理查找项目专属的 OpenAPI 工具配置
Explore - 代码评审:将生成的客户端使用模式相关问题委托给代理
code-reviewer
若技能可用,可将高级 TypeScript 类型相关问题委托给它处理。typescript-patterns
References
参考资料
- Schema design: paths, operations, parameters, components, and $ref
- Data types: formats, composition, discriminators, and nullable
- Code generation: openapi-typescript, openapi-fetch, openapi-react-query, and Zod OpenAPI
- Documentation: Swagger UI, Redoc, and API docs best practices
- Schema 设计:路径、操作、参数、组件与$ref
- 数据类型:格式、组合、鉴别器与可空类型
- 代码生成:openapi-typescript、openapi-fetch、openapi-react-query 与 Zod OpenAPI
- 文档:Swagger UI、Redoc 与 API 文档最佳实践