API 文檔神器 Swagger 介紹及在 PHP 項目中使用

Swagger 是我目前用過的最優(yōu)秀的 Api Doc 協(xié)議沒有之一。它與其他 Api Doc 協(xié)議(如apidocjs)最大的差別在于,Swagger 不僅僅可以定義 Api 的 Route / Request Param 和 Response,還可以定義 Definitions Object / Security Definitions Object 以及 Reference Object。

以一個電商項目為例,系統(tǒng)里有 商品(Product)和 訂單(Order)兩個 Model,其中 Order 有一個 product_id 字段用于關(guān)聯(lián)對應(yīng)的商品;有兩個接口,一個是獲取商品詳情 /product/{product},另一個是獲取訂單詳情 /order/{order},為了減少Api請求數(shù)量,在獲取訂單詳情時我們希望同時返回對應(yīng)的商品數(shù)據(jù)。

如果我們用 apidocjs 來寫的話,會是類似下面的注釋

/**
 * @api {get} /product/:product Request Product information
 * @apiName GetProduct
 * @apiParam {Number} product Products unique ID.
 * @apiSuccess {String} name Name of the Product.
 * @apiSuccess {Number} price  Price of the Product.
 * @apiSuccess ... Others fields
 */
 
 /**
 * @api {get} /order/:order Request Order information
 * @apiName GetOrder
 * @apiParam {Number} order Orders unique ID.
 * @apiHeader {String} Authorization Access Token.
 * @apiSuccess {String} flow_no FlowNo of the Order.
 * @apiSuccess {String} price  Price of the Order.
 * @apiSuccess {Object} product Product of the Order
 * @apiSuccess {String} product.name Name of the Product.
 * @apiSuccess {Number} product.price  Price of the Product.
 * @apiSuccess ... Others fields
 */

可以看到 Product 的字段在兩個接口的注釋里各寫了一遍,這就很繁瑣;另外一個更嚴(yán)重的問題是,如果未來 Product 表的字段發(fā)生了增減,我們需要去修改每一個會輸出 Product 的接口的注釋,更繁瑣而且容易遺漏。

所以我們需要有一個可以定義 Model 字段的功能,這就是 Swagger 的 Definitions Object,我們事先將 Product 和 Order 定義成 Definitions Object:

Product:
  type: object
  properties:
    id:
      type: integer
    name:
      type: string
      price:
          type: integer

Order:
  type: object
    properties:
      id:
          type: integer
      flow_no:
            type: string
        product:
            $ref: '#/definitions/Product'

在定義 Order 的 product 字段時,我們使用了 $ref,也就是 Reference Object,這就是告訴 Swagger,Order 的product 字段指向了 Product 的定義。

再來看看 Route / Request Param 和 Response 在Swagger 中怎么寫:

path:
    /product/{product}:
        get:
            summary: Request Product information
            parameters:
                name: product
                in: path
                required: true
                type: number
            response:
                '200':
                    description: Success
                    schema:
                        $ref: '#/definitions/Product'
    /order/{order}:
        get:
            summary: Request Order information
            parameters:
                name: order
                in: path
                required: true
                type: number
            response:
                '200':
                    description: Success
                    schema:
                        $ref: '#/definitions/Order'

通過 $ref 完美解決了增減字段需要修改多處的問題。

我在第一次使用 Swagger 的時候,雖然被其強大的定義功能所折服,卻又對寫 Swagger 文檔極其的厭惡,因為官方提供的 Swagger Editor 速度不僅慢,還時不時出錯,明明寫對了文檔格式非要告訴我有問題,只能刷新頁面重新加載。

直到最近發(fā)現(xiàn)了一個項目 swagger-php,通過注釋的方式來生成 JSON 格式的 Swagger 文檔,只需要通過 \Swagger\scan() 函數(shù)掃描一個目錄下所有 PHP 文件的注釋 (可以創(chuàng)建一個完全只有注釋的 php 文件來放一些與 request / response 并無直接關(guān)聯(lián)的定義,比如 Security Definitions Object)。

有了 JSON 格式的文檔之后,配合 Swagger UI 就可以展示出非常美觀的 Api 文檔了,可以看這個 Demo,還支持在文檔頁面直接發(fā)起 Api 請求,大大的方便!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容