Swagger2.0结构说明

更新飞机票一张 Swagger 2 与 OpenAPI 3


元数据

  1. 每个Swagger规范都以Swagger版本开始
swagger: "2.0"
  1. 需要指定API的info-title,description(可选),version(API版本,不是文件版本/Swagger版本)
info:
  title: Instruction API
  description:this is the API's description
  version:1.0.0
  • version 可以是自定义字符串,eg:1.0-beta,2018-02-02等
  • description 可以使多行的
  • info 中也可以支持其他信息对象,例如license, contact等

API调用的基础URL

所有API调用的基础URL由host , schemes , basePath(根级别)组成

 host: api.instruction.com
 basePath: /example
 schemes: 
      - https

基础路径的设置已经完成,若是加上端点/users就可以形成完整的路径了:
<scheme>://<host>/<basePath>/users


路径

API路径paths和操作在API规范的全局部分定义,GET /用户可以用以下来描述:

paths:
  /users:
     get:
        summary:Returns a list of users.
     description: Optional extended description in Markdown.
      produces:
          - application/json
      responses:
          200:
            description: OK

可以理解为是相对路径,完整路径如下:
scheme://host/basePath/path

  • 支持路径模版
    意味着您可以使用花括号{}将URL的部分标记为路径参数:
paths:
  /users/{userId}:
      get:
          summary: Returns a user by ID.
          parameters:
                - in: path
                  name: userId
                  required: true
                  type: integer
                  minimum: 1
                  description: Parameter description in Markdown.
          responses:
                200:
                  description: OK

eg: /users/1


Consumes和Produces定义

  • consumes:指定处理请求的提交内容类型(Content-Type)
  • produces:指定返回的内容类型,仅当request请求头中的(Accept)中包含指定类型才可以
consumes:
    - application/json
    - application/xml
produces:
    - application/json
    - application/xml
  • 这里的类型均为MIME类型
  • consumes只影响与POST,PUT和PATCH等请求主体的操作。对于像GET这样的无人机操作,它会被忽略

响应

对于每个操作,可以定义可能的状态代码,以及schema响应主体。schema可以通过内联定义或从外部定义引用$ref(如果多个响应使用相同的模式,可以在根级定义并通过引用$ref)。还可以为不同的内容类型提供示例响应

 paths:
  /users/{userId}:
    get:
        summary: 根据用户ID返回一个对象 user.
    parameters:
     - in: path
       name: userId
       required: true
       type: integer
       minimum: 1
       description: 根据ID返回对象
   responses:
    200:
      description: User成功获取
      schema:
        type: object
        properties:
          id:
            type: integer
            example: 1
          name:
            type: string
            example: BlingBlingY
    400:
      description:输入值不合法,不是数字.
      schema:
          $ref: "#/definitions/User"
    404:
      description: 不存在该用户.
    default:
      description: 未知错误
definitions:
  User:
  type: object
  properties:
    id:
      type: integer
      description: The user ID.
    username:
      type: string
      description: The user name.

更多信息可以参考,响应详情


输入和输出模型

全局的definitions允许定义通用数据结构。任何时候无论是request还是response,schema里需要用到,都可以通过$ref 来引用它们
例如:

{
  "id": 4,
  "name": "Arthur Dent"
}

可以表示为:

 definitions:
    User:
      properties:
        id:
            type: integer
        name:
            type: string
      # Both properties are required
      required:  
          - id
          - name

在请求主体模式和响应主体模式中引用:

paths:
  /users/{userId}:
    get:
        summary: Returns a user by ID.
        parameters:
            - in: path
              name: userId
              required: true
              type: integer
         responses:
              200:
                  description: OK
                  schema:
                    $ref: '#/definitions/User'
 /users:
    post:
         summary: Creates a new user.
         parameters:
            - in: body
              name: user
              schema:
                  $ref: '#/definitions/User'
        responses:
            200:
              description: OK

认证

在API中使用的身份验证关键词:securityDefinitions和security

securityDefinitions:
  BasicAuth:
    type: basic

security:
  - BasicAuth: []
  1. 目前API支持的三种认证方法
  • 基本认证 - BasicAuth
  • API密钥 - ApiKey
  • OAuth2 公共流程
  1. securityDefinitions来定义该API支持的所有身份验证类型
securityDefinitions:
  BasicAuth:
    type: basic
  ApiKeyAuth:
    type: apiKey
    in: header
    name: X-API-Key
  OAuth2:
    type: oauth2
    flow: accessCode
    authorizationUrl:     https://example.com/oauth/authorize
    tokenUrl: https://example.com/oauth/token
    scopes:
      read: Grants read access
      write: Grants write access
      admin: Grants read and write access to administrative information

在定义了安全机制后securityDefinitions,您可以security分别在根级别或操作级别上添加该部分,将它们应用于整个API或单个操作。

在根级别上使用时,security将指定的安全机制全局应用于所有API操作,除非在操作级别上被覆盖。在以下示例中,可以使用API​​密钥或OAuth 2对API调用进行身份验证.ApiKeyAuth和OAuth2名称是指上文定义过的安全机制securityDefinitions

security:
  - ApiKeyAuth: []
  - OAuth2: [read, write]

全局security可以在个别操作中被覆盖,从而可以使用不同的认证类型,有的根本不认证,如下例:

paths:
  /billing_info:
    get:
      summary: Gets the account billing info
      security:
        - OAuth2: [admin]   # Use OAuth with a different scope
      responses:
        200:
          description: OK
        401:
          description: Not authenticated
        403:
          description: Access token does not have the required scope

  /ping:
    get:
      summary: Checks if the server is running
      security: []   # No security
      responses:
        200:
          description: Server is up and running
        default:
          description: Something is wrong

3.多种验证类型

security模块可使用逻辑OR和AND组合安全要求,来实现所需的结果

security:    # A OR B
  - A
  - B
security:    # A AND B
  - A
    B
security:    # (A AND B) OR (C AND D)
  - A
    B
  - C
    D
  • 使用基本身份验证或API密钥:
security:
  - basicAuth: []
  - apiKey: []

security:
  - apiKey1: []
    apiKey2: []
  • API需要在请求中包含一对API密钥:
security:
  - apiKey1: []
    apiKey2: []
  • 使用OAuth 2或一对API密钥:
security:
  - oauth2: [scope1, scope2]
  - apiKey1: []
    apiKey2: []

参考资料:
https://swagger.io/docs/specification/about/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容