2
Swagger 2 和 Open API 3 的区别
source link: https://liqiang.io/post/openapi-30-vs-swagger-20?lang=ZH_CN
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
在写 API 文档的时候,可能你有其他选择,但是你很可能会选择或者考虑过 Swagger,但是它现在已经不叫 Swagger 了,而是捐赠给了 Linux 基金会称为了 Open API,所以本文就对比一下他们做了哪些改变。
其实从概述上看,其实 3.0 比 2.0 来说,重要的特点就是变得更抽象了,然后更加地组件化了,一张图来看是这样的:
所以下面就具体看下变化吧。
元数据的变化
所有对 swagger 的引用都被改为 openapi,这个是最直观的,第一行就需要改变。因此版本必须要有如下变化:
[[email protected]]# cat docs.yaml
"swagger": "2.0"
-->
"openapi": "3.0.0"
Server 定义变化
以前旧版本的长这样:
[[email protected]]# cat v2-docs.yaml
host: example.com
basePath: /v1
schemes: ['http', 'https']
新版本可以长这样:
[[email protected]]# cat v3-docs.yaml
servers:
- url: https://{subdomain}.site.com/{version}
description: The main prod server
variables:
subdomain:
default: production
version:
enum:
- v1
- v2
default: v2
旧版本的请求无论是在 path 还是在 header 上,都是混在一起的,在 openapi 3 上将他们分开了:
[[email protected]]# cat v2-docs.yaml
"/pets/{petId}":
post:
parameters:
- name: petId
in: path
description: ID of pet to update
required: true
type: string
- name: user
in: body
description: user to add to the system
required: true
schema:
type: array
items:
type: string
在 v3 的时候更加明确了:
[[email protected]]# cat v3-docs.yaml
"/pets/{petId}":
post:
requestBody:
description: user to add to the system
required: true
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
examples:
- name: Fluffy
petType: Cat
- http://example.com/pet.json
parameters:
- name: petId
in: path
description: ID of pet to update
required: true
type: string
Security 变化
其实这个我还是没法直接看懂怎么用:
[[email protected]]# cat v2-docs.yaml
securityDefinitions:
UserSecurity:
type: basic
APIKey:
type: apiKey
name: Authorization
in: header
security:
- UserSecurity: []
- APIKey: []
—> 新版本:
[[email protected]]# cat v3-docs.yaml
components:
securitySchemes:
UserSecurity:
type: http
scheme: basic
APIKey:
type: http
scheme: bearer
bearerFormat: TOKEN
security:
- UserSecurity: []
- APIKey: []
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK