首页 文章 精选 留言 我的

精选列表

搜索[对象],共10000篇文章
优秀的个人博客,低调大师

OpenAPI演进录 | 03-通用对象放哪儿?API中的component重用

> 标签:`#OpenAPI` `#components` `#复用` `#definitions` `#API治理` --- ## 实践问题 很多公司的后台,技术架构上采用 SaaS 模式:几十个微服务共享同一套「400 参数错误 / 401 未授权 / 500 内部错误」结构。 Swagger 2.0 文档里,`definitions`、`parameters`、`responses`、`securityDefinitions` 散落顶层,新人第一次找公共错误体,经常问:「咱家的 `ErrorResponse` 到底在哪个文件里?」 更糟的是,同一错误结构在三个服务里各抄一份。某天产品要求错误体增加 `traceId`,你得改三处,漏改一处,SDK 生成出来就不一致。文档越写越长,维护成本指数级上升。 --- ## API思想进化 **复用是 API 治理的复利:把公共定义收进统一命名空间,文档越长大越省维护成本。** API 规范不仅是给前端看的文档,更是全链路的「单一事实来源」。公共模型、公共错误、公共参数必须有一处统一归口,否则微服务一多,契约必然分裂。 --- ## 旧写法 vs 新写法 ![03-components-tree.png](https://developer.qcloudimg.com/http-save/yehe-11563618/cc2d9fd08f9e0a367a11682f159216fc.png) *图:Swagger 2.0 顶层散点 vs OpenAPI 3.0+ components 树* ### 2.0:顶层散点 Swagger 2.0 把可复用定义平铺在根级别。 问题: - 四种定义分类不清,容易混乱; - 没有统一命名空间,跨文件复用靠 `$ref` 硬路径; - 不支持 `requestBodies`、`headers`、`examples` 等高级复用。 ```yaml swagger: '2.0' host: api.example.com basePath: /v1 definitions: ErrorResponse: type: object properties: code: type: integer message: type: string parameters: PageParam: name: page in: query type: integer responses: NotFound: description: 未找到 securityDefinitions: bearerAuth: type: apiKey in: header name: Authorization paths: /users: ... ``` ### 3.0+:统一 `components` 命名空间 OpenAPI 3.0 引入 `components`,把可复用定义按类型分类。 所有公共定义归到 `components` 下,查找路径统一、分类清晰、工具链解析一致。 ```yaml openapi: '3.0.3' components: schemas: ErrorResponse: type: object properties: code: type: integer message: type: string traceId: type: string responses: NotFound: description: 未找到 content: application/json: schema: $ref: '#/components/schemas/ErrorResponse' parameters: PageParam: name: page in: query schema: type: integer default: 1 securitySchemes: bearerAuth: type: http scheme: bearer ``` ### 3.2:再扩 `pathItems` 与 `mediaTypes` OpenAPI 3.2 在 `components` 中新增两类复用: ```yaml openapi: '3.2.0' components: pathItems: userBase: get: summary: 查询用户 post: summary: 创建用户 mediaTypes: JsonProblem: schema: $ref: '#/components/schemas/Problem' ``` `pathItems` 可以复用整段 Path Item,`mediaTypes` 可以复用 Media Type Object。对于上百个接口的大型 API,这意味着整段 CRUD 路径都可以复用,而不是复制粘贴。 ![03-reuse-cost-curve.png](https://developer.qcloudimg.com/http-save/yehe-11563618/c86db80986201e16a30a95e28d485884.png) *图:复用让 API 文档越长大越省成本* ### 案例:GlobalPay OpenApi Common Schemas(3.0.3) 这是一个 Domain(可复用组件库),本身就是为复用而生,来源:`https://app.swaggerhub.com/domains/globalsolutions-184/CommonSchemas/2.4.0` 这种「公共 Schema Domain」正是 `components` 思想的工程化落地:错误体只定义一次,所有业务 API 通过 `$ref` 引用。 ```yaml openapi: 3.0.3 info: title: GlobalPay OpenApi Common Schemas version: 2.4.0 components: schemas: Error: type: object properties: error_code: type: string detailed_error_code: type: string detailed_error_description: type: string ``` ### 案例:Acme Components(3.1.0) ```yaml openapi: 3.1.0 info: title: Acme Components version: 1.0.0 components: schemas: Pet: type: object properties: id: type: integer name: type: string ``` 来源:`https://app.swaggerhub.com/domains/smartbear-playground/TestExternal3.1Domain/1.0.0` --- ## 真实案例佐证 (优先 SwaggerHub) - **GlobalPay OpenApi Common Schemas(3.0.3)**:`https://app.swaggerhub.com/domains/globalsolutions-184/CommonSchemas/2.4.0` - 专门放公共 schemas,是 `components` 思想的最佳示范。 - **Acme Components(3.1.0)**:`https://app.swaggerhub.com/domains/smartbear-playground/TestExternal3.1Domain/1.0.0` - 轻量 components 示例。 - **Azure AI Foundry Models Service(3.2.0)**:`https://app.swaggerhub.com/apis/westriverconsulting/azure-ai-foundry-models-service/v1` - 大型 API 中 `components` 的复杂用法。 --- ## 落地 checklist 1. **按复用频率分层**:高频复用(公共错误、公共模型)进 `components`,一次性 schema 就近内联。 2. **错误响应抽成 `components.responses`**:比如 `NotFound`、`BadRequest`、`Unauthorized`,全站统一引用。 3. **多服务共享同一 Domain**:像 GlobalPay 那样建一个 Common Schemas Domain,业务 API 用 `$ref` 引用。 4. **3.2 用 `components.pathItems`**:把 CRUD 整段路径复用,避免复制粘贴。 5. **老项目迁移**:把顶层 `definitions` → `components.schemas`,`parameters` → `components.parameters`,`responses` → `components.responses`,`securityDefinitions` → `components.securitySchemes`。 6. **命名规范**:`components.schemas` 用 PascalCase,`components.parameters` 用 camelCase,团队统一。 --- ## 下一篇预告 OpenAPI演进录 | 04-Cookie 参数和整串查询,旧版 OpenAPI 写不了?

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码