manage-openapi-overlays
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesemanage-openapi-overlays
管理OpenAPI Overlay
Overlays let you customize an OpenAPI spec for SDK generation without modifying the source. This skill covers creating overlay files, applying them to specs, and using them to fix validation errors.
Overlay允许你在不修改源文件的情况下,针对SDK生成需求定制OpenAPI规范。本技能涵盖创建Overlay文件、将其应用到规范中,以及使用它们修复验证错误。
Content Guides
内容指南
| Topic | Guide |
|---|---|
| OpenAPI Validation | content/validation.md |
| Security Schemes | content/security-schemes.md |
These guides cover validating specs, fixing common issues, and configuring authentication methods.
| 主题 | 指南 |
|---|---|
| OpenAPI Validation | content/validation.md |
| Security Schemes | content/security-schemes.md |
这些指南涵盖规范验证、常见问题修复以及认证方法配置。
Authentication
认证
Set env var or run .
SPEAKEASY_API_KEYspeakeasy auth login设置环境变量,或运行命令。
SPEAKEASY_API_KEYspeakeasy auth loginWhen to Use
适用场景
Use this skill when you need to manually work with overlay files:
- Creating an overlay file from scratch with specific JSONPath targets
- Applying an existing overlay file to a spec
- Validating overlay syntax and structure
- Comparing two specs to generate an overlay
- Understanding overlay mechanics (actions, targets, update/remove)
- Fixing lint issues via manual overlay creation
- User says: "create overlay", "apply overlay", "overlay file", "manual overlay", "overlay syntax", "JSONPath targeting", "validate overlay"
NOT for: AI-powered naming suggestions (see instead)
improve-sdk-naming当你需要手动处理Overlay文件时使用本技能:
- 基于特定JSONPath目标从头创建Overlay文件
- 将现有Overlay文件应用到规范中
- 验证Overlay的语法和结构
- 对比两个规范以生成Overlay
- 理解Overlay的工作机制(动作、目标、更新/删除)
- 通过手动创建Overlay修复Lint问题
- 用户提及以下关键词时触发:"create overlay"、"apply overlay"、"overlay file"、"manual overlay"、"overlay syntax"、"JSONPath targeting"、"validate overlay"
不适用场景:AI驱动的命名建议(请使用技能)
improve-sdk-namingInputs
输入项
| Input | Required | Description |
|---|---|---|
| Target spec | Yes | OpenAPI spec to customize or fix |
| Customizations | Depends | Changes to apply (groups, names, retries, descriptions) |
| Overlay file | Depends | Existing overlay to apply (for apply workflow) |
| Lint output | Helpful | Validation errors to fix (for fix workflow) |
| 输入项 | 是否必填 | 描述 |
|---|---|---|
| Target spec | 是 | 需要定制或修复的OpenAPI规范 |
| Customizations | 可选 | 需要应用的修改(分组、命名、重试、描述等) |
| Overlay file | 可选 | 待应用的现有Overlay文件(适用于应用流程) |
| Lint output | 辅助 | 需要修复的验证错误(适用于修复流程) |
Outputs
输出项
| Output | Description |
|---|---|
| Overlay file | YAML file with JSONPath-targeted changes |
| Modified spec | Transformed OpenAPI spec (when applying) |
| 输出项 | 描述 |
|---|---|
| Overlay file | 包含JSONPath目标修改的YAML文件 |
| Modified spec | 经过转换的OpenAPI规范(应用Overlay后生成) |
Commands
命令
Generate an Overlay by Comparing Specs
通过对比规范生成Overlay
bash
speakeasy overlay compare -b <before-spec> -a <after-spec> -o <output-overlay>Use this when you have a modified version of a spec and want to capture the differences as a reusable overlay.
bash
speakeasy overlay compare -b <before-spec> -a <after-spec> -o <output-overlay>当你有一个规范的修改版本,希望将差异捕获为可复用的Overlay时,使用此命令。
Apply an Overlay to a Spec
将Overlay应用到规范
bash
speakeasy overlay apply -s <spec-path> -o <overlay-path> --out <output-path>bash
speakeasy overlay apply -s <spec-path> -o <overlay-path> --out <output-path>Validate an Overlay
验证Overlay
bash
speakeasy overlay validate -o <overlay-path>bash
speakeasy overlay validate -o <overlay-path>Creating an Overlay Manually
手动创建Overlay
Create an overlay file with this structure:
yaml
overlay: 1.0.0
info:
title: My Overlay
version: 1.0.0
actions:
- target: "$.paths['/example'].get"
update:
x-speakeasy-group: example
x-speakeasy-name-override: getExampleEach action has a (JSONPath expression) and an (object to merge) or (boolean to delete the target).
targetupdateremove按照以下结构创建Overlay文件:
yaml
overlay: 1.0.0
info:
title: My Overlay
version: 1.0.0
actions:
- target: "$.paths['/example'].get"
update:
x-speakeasy-group: example
x-speakeasy-name-override: getExample每个动作包含一个(JSONPath表达式),以及(待合并的对象)或(用于删除目标的布尔值)。
targetupdateremoveExample: SDK Method Naming and Grouping
示例:SDK方法命名与分组
yaml
overlay: 1.0.0
info:
title: SDK Customizations
version: 1.0.0
actions:
- target: "$.paths['/users'].get"
update:
x-speakeasy-group: users
x-speakeasy-name-override: list
- target: "$.paths['/users'].post"
update:
x-speakeasy-group: users
x-speakeasy-name-override: create
- target: "$.paths['/users/{id}'].get"
update:
x-speakeasy-group: users
x-speakeasy-name-override: get
- target: "$.paths['/users/{id}'].delete"
update:
x-speakeasy-group: users
x-speakeasy-name-override: delete
deprecated: trueThis produces SDK methods: , , , .
sdk.users.list()sdk.users.create()sdk.users.get()sdk.users.delete()yaml
overlay: 1.0.0
info:
title: SDK Customizations
version: 1.0.0
actions:
- target: "$.paths['/users'].get"
update:
x-speakeasy-group: users
x-speakeasy-name-override: list
- target: "$.paths['/users'].post"
update:
x-speakeasy-group: users
x-speakeasy-name-override: create
- target: "$.paths['/users/{id}'].get"
update:
x-speakeasy-group: users
x-speakeasy-name-override: get
- target: "$.paths['/users/{id}'].delete"
update:
x-speakeasy-group: users
x-speakeasy-name-override: delete
deprecated: true这将生成以下SDK方法:、、、。
sdk.users.list()sdk.users.create()sdk.users.get()sdk.users.delete()Example: Apply Overlay
示例:应用Overlay
bash
undefinedbash
undefinedApply overlay and write merged spec
应用Overlay并写入合并后的规范
speakeasy overlay apply -s openapi.yaml -o sdk-overlay.yaml --out openapi-modified.yaml
speakeasy overlay apply -s openapi.yaml -o sdk-overlay.yaml --out openapi-modified.yaml
Compare two specs to generate an overlay
对比两个规范以生成Overlay
speakeasy overlay compare -b original.yaml -a modified.yaml -o changes-overlay.yaml
undefinedspeakeasy overlay compare -b original.yaml -a modified.yaml -o changes-overlay.yaml
undefinedUsing in Workflow (Recommended)
推荐工作流用法
Instead of applying overlays manually, add them to :
.speakeasy/workflow.yamlyaml
sources:
my-api:
inputs:
- location: ./openapi.yaml
overlays:
- location: ./naming-overlay.yaml
- location: ./grouping-overlay.yamlOverlays are applied in order. Later overlays can override earlier ones. This approach ensures overlays are always applied during .
speakeasy run建议不要手动应用Overlay,而是将它们添加到中:
.speakeasy/workflow.yamlyaml
sources:
my-api:
inputs:
- location: ./openapi.yaml
overlays:
- location: ./naming-overlay.yaml
- location: ./grouping-overlay.yamlOverlay会按顺序应用,后续的Overlay可以覆盖之前的设置。这种方式确保在运行时,Overlay始终会被应用。
speakeasy runCommon Fix Patterns
常见修复模式
Use overlays to fix validation issues when you cannot edit the source spec.
| Issue | Overlay Fix |
|---|---|
| Poor operation names | Add |
| Missing descriptions | Add |
| Missing tags | Add |
| Need operation grouping | Add |
| Need retry config | Add |
| Deprecate an endpoint | Add |
| Add SDK-specific metadata | Add any |
当你无法编辑源规范时,可使用Overlay修复验证问题。
| 问题 | Overlay修复方案 |
|---|---|
| 操作名称不佳 | 为操作添加 |
| 缺少描述 | 为操作添加 |
| 缺少标签 | 为操作添加 |
| 需要操作分组 | 为操作添加 |
| 需要重试配置 | 为操作或全局添加 |
| 弃用端点 | 为操作添加 |
| 添加SDK专属元数据 | 添加任意 |
Fix Workflow
修复工作流
bash
undefinedbash
undefined1. Validate the spec to identify issues
1. 验证规范以识别问题
speakeasy lint openapi --non-interactive -s openapi.yaml
speakeasy lint openapi --non-interactive -s openapi.yaml
2. Create an overlay file targeting each issue (see patterns above)
2. 创建Overlay文件,针对每个问题进行修复(参考上述模式)
3. Add overlay to workflow.yaml under sources.overlays
3. 在workflow.yaml的sources.overlays中添加Overlay
4. Regenerate the SDK
4. 重新生成SDK
speakeasy run --output console
undefinedspeakeasy run --output console
undefinedSpeakeasy Extensions Reference
Speakeasy扩展参考
Extensions () customize SDK generation. Apply them via overlays.
x-speakeasy-*| Extension | Applies To | Purpose |
|---|---|---|
| Operation or root | Configure retry behavior |
| Operation | Enable automatic pagination |
| Operation | Override SDK method name |
| Operation | Group methods under namespace |
| Schema with enum | Allow unknown enum values |
| Root | Define SDK-wide parameters |
| Security scheme | Multi-part custom auth |
扩展()用于定制SDK生成,可通过Overlay应用。
x-speakeasy-*| 扩展 | 适用对象 | 用途 |
|---|---|---|
| 操作或根节点 | 配置重试行为 |
| 操作 | 启用自动分页 |
| 操作 | 覆盖SDK方法名称 |
| 操作 | 将方法分组到命名空间下 |
| 带枚举的Schema | 允许未知枚举值 |
| 根节点 | 定义SDK级别的参数 |
| 安全方案 | 多部分自定义认证 |
Retries
重试
yaml
actions:
- target: "$.paths['/resources'].get" # Or "$" for global
update:
x-speakeasy-retries:
strategy: backoff
backoff:
initialInterval: 500 # ms
maxInterval: 60000 # ms
maxElapsedTime: 3600000 # ms
exponent: 1.5
statusCodes: ["5XX", "429"]
retryConnectionErrors: trueyaml
actions:
- target: "$.paths['/resources'].get" # 或使用"$"设置全局
update:
x-speakeasy-retries:
strategy: backoff
backoff:
initialInterval: 500 # 毫秒
maxInterval: 60000 # 毫秒
maxElapsedTime: 3600000 # 毫秒
exponent: 1.5
statusCodes: ["5XX", "429"]
retryConnectionErrors: truePagination
分页
Offset/Limit:
yaml
actions:
- target: "$.paths['/users'].get"
update:
x-speakeasy-pagination:
type: offsetLimit
inputs:
- name: offset
in: parameters
type: offset
- name: limit
in: parameters
type: limit
outputs:
results: $.data
numPages: $.meta.total_pagesCursor:
yaml
actions:
- target: "$.paths['/events'].get"
update:
x-speakeasy-pagination:
type: cursor
inputs:
- name: cursor
in: parameters
type: cursor
outputs:
results: $.events
nextCursor: $.next_cursor偏移量/限制:
yaml
actions:
- target: "$.paths['/users'].get"
update:
x-speakeasy-pagination:
type: offsetLimit
inputs:
- name: offset
in: parameters
type: offset
- name: limit
in: parameters
type: limit
outputs:
results: $.data
numPages: $.meta.total_pages游标:
yaml
actions:
- target: "$.paths['/events'].get"
update:
x-speakeasy-pagination:
type: cursor
inputs:
- name: cursor
in: parameters
type: cursor
outputs:
results: $.events
nextCursor: $.next_cursorOpen Enums (Anti-Fragility)
开放枚举(抗脆弱性)
Prevent SDK breakage when APIs return new enum values:
yaml
actions:
- target: "$.components.schemas.Status"
update:
x-speakeasy-unknown-values: allowFor all enums (add at overlay root):
x-speakeasy-jsonpath: rfc9535yaml
actions:
- target: $..[?length(@.enum) > 1]
update:
x-speakeasy-unknown-values: allow防止API返回新枚举值时导致SDK崩溃:
yaml
actions:
- target: "$.components.schemas.Status"
update:
x-speakeasy-unknown-values: allow针对所有枚举(在Overlay根节点添加):
x-speakeasy-jsonpath: rfc9535yaml
actions:
- target: $..[?length(@.enum) > 1]
update:
x-speakeasy-unknown-values: allowGlobal Headers
全局请求头
Add SDK-wide headers as constructor options:
yaml
actions:
- target: $
update:
x-speakeasy-globals:
parameters:
- $ref: "#/components/parameters/TenantId"
- target: $.components
update:
parameters:
TenantId:
name: X-Tenant-Id
in: header
schema:
type: stringResult:
client = SDK(api_key="...", tenant_id="tenant-123")添加SDK级别的请求头作为构造函数选项:
yaml
actions:
- target: $
update:
x-speakeasy-globals:
parameters:
- $ref: "#/components/parameters/TenantId"
- target: $.components
update:
parameters:
TenantId:
name: X-Tenant-Id
in: header
schema:
type: string效果:
client = SDK(api_key="...", tenant_id="tenant-123")Custom Security Schemes
自定义安全方案
For complex auth (HMAC, multi-part credentials):
yaml
actions:
- target: $.components
update:
securitySchemes:
hmacAuth:
type: http
scheme: custom
x-speakeasy-custom-security-scheme:
schema:
type: object
properties:
keyId:
type: string
keySecret:
type: string
- target: $
update:
security:
- hmacAuth: []With in gen.yaml, generates env var support for , .
envVarPrefix: MYAPIMYAPI_KEY_IDMYAPI_KEY_SECRET针对复杂认证(HMAC、多部分凭证):
yaml
actions:
- target: $.components
update:
securitySchemes:
hmacAuth:
type: http
scheme: custom
x-speakeasy-custom-security-scheme:
schema:
type: object
properties:
keyId:
type: string
keySecret:
type: string
- target: $
update:
security:
- hmacAuth: []若在gen.yaml中设置,将生成对、环境变量的支持。
envVarPrefix: MYAPIMYAPI_KEY_IDMYAPI_KEY_SECRETJSONPath Targeting Reference
JSONPath定位参考
| Target | Selects |
|---|---|
| GET /users operation |
| All operations on /users/{id} |
| First parameter of GET /users |
| User schema definition |
| Name property of User schema |
| API info object |
| API title |
| First server entry |
| 目标路径 | 选中对象 |
|---|---|
| GET /users 操作 |
| /users/{id} 下的所有操作 |
| GET /users 的第一个参数 |
| User Schema定义 |
| User Schema的name属性 |
| API信息对象 |
| API标题 |
| 第一个服务器条目 |
What NOT to Do
注意事项
- Do NOT use overlays for invalid YAML/JSON syntax errors -- fix the source file
- Do NOT try to fix broken paths with overlays -- fix the source spec
$ref - Do NOT use overlays to fix wrong data types -- this is an API design issue
- Do NOT try to deduplicate schemas with overlays -- requires structural analysis
- Do NOT ignore errors that require source spec fixes -- overlays cannot solve everything
- Do NOT modify source OpenAPI specs directly if they are externally managed
- Do NOT use a command -- it does not exist
speakeasy overlay create
- 请勿使用Overlay修复无效的YAML/JSON语法错误——请直接修复源文件
- 请勿尝试使用Overlay修复损坏的路径——请直接修复源规范
$ref - 请勿使用Overlay修复错误的数据类型——这属于API设计问题
- 请勿尝试使用Overlay去重Schema——这需要结构分析
- 请勿忽略需要修复源规范的错误——Overlay无法解决所有问题
- 请勿直接修改外部管理的OpenAPI源规范
- 请勿使用命令——该命令不存在
speakeasy overlay create
Troubleshooting
故障排除
| Error | Cause | Solution |
|---|---|---|
| "target not found" | JSONPath does not match spec structure | Verify exact path and casing by inspecting the spec |
| Changes not applied | Overlay not in workflow | Add overlay to |
| "invalid overlay" | Malformed YAML | Check overlay structure: needs |
| YAML parse error | Invalid overlay syntax | Check YAML indentation and quoting |
| No changes visible | Wrong target path | Use |
| Errors persist after overlay | Issue not overlay-appropriate | Check if the issue requires a source spec fix instead |
| Overlay order conflict | Later overlay overrides earlier | Reorder overlays in |
| 错误 | 原因 | 解决方案 |
|---|---|---|
| "target not found" | JSONPath与规范结构不匹配 | 检查规范结构,确认路径和大小写完全一致 |
| 修改未生效 | Overlay未加入工作流 | 在 |
| "invalid overlay" | YAML格式错误 | 检查Overlay结构:必须包含 |
| YAML解析错误 | Overlay语法无效 | 检查YAML缩进和引号使用 |
| 无可见修改 | 目标路径错误 | 使用 |
| 应用Overlay后错误仍存在 | 问题不适用于Overlay修复 | 检查该问题是否需要直接修复源规范 |
| Overlay顺序冲突 | 后续Overlay覆盖了之前的设置 | 在 |