Swagger2.0結(jié)構(gòu)說明

更新飛機(jī)票一張 Swagger 2 與 OpenAPI 3


元數(shù)據(jù)

  1. 每個(gè)Swagger規(guī)范都以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 中也可以支持其他信息對(duì)象,例如license, contact等

API調(diào)用的基礎(chǔ)URL

所有API調(diào)用的基礎(chǔ)URL由host , schemes , basePath(根級(jí)別)組成

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

基礎(chǔ)路徑的設(shè)置已經(jīng)完成,若是加上端點(diǎn)/users就可以形成完整的路徑了:
<scheme>://<host>/<basePath>/users


路徑

API路徑paths和操作在API規(guī)范的全局部分定義,GET /用戶可以用以下來描述:

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

可以理解為是相對(duì)路徑,完整路徑如下:
scheme://host/basePath/path

  • 支持路徑模版
    意味著您可以使用花括號(hào){}將URL的部分標(biāo)記為路徑參數(shù):
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:指定處理請(qǐng)求的提交內(nèi)容類型(Content-Type)
  • produces:指定返回的內(nèi)容類型,僅當(dāng)request請(qǐng)求頭中的(Accept)中包含指定類型才可以
consumes:
    - application/json
    - application/xml
produces:
    - application/json
    - application/xml
  • 這里的類型均為MIME類型
  • consumes只影響與POST,PUT和PATCH等請(qǐng)求主體的操作。對(duì)于像GET這樣的無(wú)人機(jī)操作,它會(huì)被忽略

響應(yīng)

對(duì)于每個(gè)操作,可以定義可能的狀態(tài)代碼,以及schema響應(yīng)主體。schema可以通過內(nèi)聯(lián)定義或從外部定義引用$ref(如果多個(gè)響應(yīng)使用相同的模式,可以在根級(jí)定義并通過引用$ref)。還可以為不同的內(nèi)容類型提供示例響應(yīng)

 paths:
  /users/{userId}:
    get:
        summary: 根據(jù)用戶ID返回一個(gè)對(duì)象 user.
    parameters:
     - in: path
       name: userId
       required: true
       type: integer
       minimum: 1
       description: 根據(jù)ID返回對(duì)象
   responses:
    200:
      description: User成功獲取
      schema:
        type: object
        properties:
          id:
            type: integer
            example: 1
          name:
            type: string
            example: BlingBlingY
    400:
      description:輸入值不合法,不是數(shù)字.
      schema:
          $ref: "#/definitions/User"
    404:
      description: 不存在該用戶.
    default:
      description: 未知錯(cuò)誤
definitions:
  User:
  type: object
  properties:
    id:
      type: integer
      description: The user ID.
    username:
      type: string
      description: The user name.

更多信息可以參考,響應(yīng)詳情


輸入和輸出模型

全局的definitions允許定義通用數(shù)據(jù)結(jié)構(gòu)。任何時(shí)候無(wú)論是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

在請(qǐng)求主體模式和響應(yīng)主體模式中引用:

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

認(rèn)證

在API中使用的身份驗(yàn)證關(guān)鍵詞:securityDefinitions和security

securityDefinitions:
  BasicAuth:
    type: basic

security:
  - BasicAuth: []
  1. 目前API支持的三種認(rèn)證方法
  • 基本認(rèn)證 - BasicAuth
  • API密鑰 - ApiKey
  • OAuth2 公共流程
  1. securityDefinitions來定義該API支持的所有身份驗(yàn)證類型
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

在定義了安全機(jī)制后securityDefinitions,您可以security分別在根級(jí)別或操作級(jí)別上添加該部分,將它們應(yīng)用于整個(gè)API或單個(gè)操作。

在根級(jí)別上使用時(shí),security將指定的安全機(jī)制全局應(yīng)用于所有API操作,除非在操作級(jí)別上被覆蓋。在以下示例中,可以使用API??密鑰或OAuth 2對(duì)API調(diào)用進(jìn)行身份驗(yàn)證.ApiKeyAuth和OAuth2名稱是指上文定義過的安全機(jī)制securityDefinitions

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

全局security可以在個(gè)別操作中被覆蓋,從而可以使用不同的認(rèn)證類型,有的根本不認(rèn)證,如下例:

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.多種驗(yàn)證類型

security模塊可使用邏輯OR和AND組合安全要求,來實(shí)現(xiàn)所需的結(jié)果

security:    # A OR B
  - A
  - B
security:    # A AND B
  - A
    B
security:    # (A AND B) OR (C AND D)
  - A
    B
  - C
    D
  • 使用基本身份驗(yàn)證或API密鑰:
security:
  - basicAuth: []
  - apiKey: []

security:
  - apiKey1: []
    apiKey2: []
  • API需要在請(qǐng)求中包含一對(duì)API密鑰:
security:
  - apiKey1: []
    apiKey2: []
  • 使用OAuth 2或一對(duì)API密鑰:
security:
  - oauth2: [scope1, scope2]
  - apiKey1: []
    apiKey2: []

參考資料:
https://swagger.io/docs/specification/about/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,224評(píng)論 6 529
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 97,916評(píng)論 3 413
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,014評(píng)論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,466評(píng)論 1 308
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,245評(píng)論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,795評(píng)論 1 320
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,869評(píng)論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,010評(píng)論 0 285
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,524評(píng)論 1 331
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,487評(píng)論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,634評(píng)論 1 366
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,173評(píng)論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 43,884評(píng)論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,282評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,541評(píng)論 1 281
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,236評(píng)論 3 388
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,623評(píng)論 2 370

推薦閱讀更多精彩內(nèi)容