Swagger 简介

Swagger 的来历、从 2.0 到 OpenAPI 3.x 的演进,以及它在接口全生命周期里的位置。

Beginner
Key PointsSwagger 2.0 是旧规范名,2015 年捐给 OpenAPI Initiative 后改叫 OpenAPI,3.0 起支持 oneOf/anyOf、多服务器与更完整的请求体描述。

来历

Tony Tam created Swagger in 2010 while documenting Wordnik's API. In 2015 SmartBear donated the specification to the OpenAPI Initiative under the Linux Foundation; the spec became the OpenAPI Specification while Swagger remained the tooling brand.

版本演进

VersionYearHighlights
Swagger 1.x2011JSON only, minimal structure
Swagger 2.02014Single file, definitions, widely supported
OpenAPI 3.02017components, requestBody, oneOf/anyOf, multiple servers
OpenAPI 3.12021Full JSON Schema 2020-12 alignment, webhooks

在接口生命周期里的位置

  • 设计先行 — 先定 openapi.yaml,前后端并行开发
  • Mock — 由规范直接生成假数据服务,前端不必等后端
  • 实现 — 服务端按规范实现,或用代码注解生成规范
  • 测试 — 用规范做契约测试,防止实现漂移
  • 发布 — Swagger UI / Redoc 渲染成对外文档

和其他文档方式的对比

ApproachStays in syncTry itCodegen
Markdown / WikiNoNoNo
Comments in codePartlyNoNo
OpenAPI specYes, it is the sourceYesYes

Friends & Partners