LoginSignup
4
0

OpenAPI仕様なAPIの更新監視について

Last updated at Posted at 2023-12-22

背景

私達のプロダクトでは、外部サービスとの連携機能を提供しています。
これらの機能は、外部サービスが公開しているOpenAPI仕様のAPIドキュメントに基づいて実装されています。連携先サービスの仕様変更は私達がコントロールできるものではなく、連携先の都合により実施されます。
その中には私達の提供機能に直接的な影響を与える破壊的な変更も含まれます。これらの変更を迅速に察知し、適切に対応できるようにしたい、というのが発端です。

監視方法の探索

最初のアプローチとして、API仕様書の変更を検知するために古典的なdiffコマンドの利用を検討しました。
しかし、仕様書がJSONやYAML形式で記述されているため、単なるテキスト比較では変更の影響を正確に判断するのが難しいことは明らかです。
例えば、レスポンスのスキーマ定義が変更された場合、その影響範囲を正しく把握するには、各使用箇所を詳細に検証する必要があります。

そこで、OpenAPI仕様に準拠したドキュメントを解析することで、エンドポイント別の変更を容易に把握できる可能性があると考えました。
しかし、そうしたツールを1から開発するのは非常に労力がかかります。
幸いなことに、この問題は一般的なものであり。既に利用可能なツールが存在する可能性が高いと考えました。

検証観点の整理

検証に先立って、以下の要件を設定しました。

  1. 特定のAPIに限定した変更検出: 使用しているAPIに関連する変更のみを検出すること。
  2. 多様な変更パターンへの対応: パラメータの増減、デフォルト値の変更など、さまざまな変更パターンに対応できること。
  3. 非破壊的な変更の無視: descriptionなど破壊的でない要素の変更を無視できること。
  4. Markdown形式での出力: Slackなどのチャットツールへの通知を容易にするために、Markdown形式で出力できること。

検証対象ツール

この目的のために選択したツールは、Tufin/oasdiffです。
OpenAPITools/openapi-diff も検討しましたが、上記要件を満たす設定が可能か不明瞭であったため、詳細な検証は行いませんでした。

  1. 特定のAPIの変更検出:

    • --match-path オプションを使用して、正規表現を用いて関連するAPIの変更のみを検出することが可能でした。
  2. 多様な変更パターンへの対応:

    • エンドポイントの追加・削除やリクエストとレスポンス内容の変更など、幅広い変更を検出する機能がありました。
  3. 非破壊的な変更の無視:

    • --exclude-elements オプションを利用して、examples, description, summary, title, endpoints などの要素を除外することができました。

具体的な検証結果は skaji18/openapi-diff-tool-examination にまとめてみましたので、ご参照ください。

一例だけ記載すると下記のように単純なdiffコマンドの結果ではまともに変更内容を理解することができなかったものが、ツールを利用することで非常に簡単に理解できるようになります

diff コマンドによる差分
4a5,10
>   description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
>   termsOfService: http://swagger.io/terms/
>   contact:
>     name: Swagger API Team
>     email: apiteam@swagger.io
>     url: http://swagger.io
6c12,13
<     name: MIT
---
>     name: Apache 2.0
>     url: https://www.apache.org/licenses/LICENSE-2.0.html
8c15
<   - url: http://petstore.swagger.io/v1
---
>   - url: https://petstore.swagger.io/v2
12,15c19,24
<       summary: List all pets
<       operationId: listPets
<       tags:
<         - pets
---
>       description: |
>         Returns all pets from the system that the user has access to
>         Nam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.
> 
>         Sed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.
>       operationId: findPets
16a26,34
>         - name: tags
>           in: query
>           description: tags to filter by
>           required: false
>           style: form
>           schema:
>             type: array
>             items:
>               type: string
19c37
<           description: How many items to return at one time (max 100)
---
>           description: maximum number of results to return
23d40
<             maximum: 100
27,32c44
<           description: A paged array of pets
<           headers:
<             x-next:
<               description: A link to the next page of responses
<               schema:
<                 type: string
---
>           description: pet response
34c46
<             application/json:    
---
>             application/json:
36c48,50
<                 $ref: "#/components/schemas/Pets"
---
>                 type: array
>                 items:
>                   $ref: '#/components/schemas/Pet'
42c56
<                 $ref: "#/components/schemas/Error"
---
>                 $ref: '#/components/schemas/Error'
44,47c58,66
<       summary: Create a pet
<       operationId: createPets
<       tags:
<         - pets
---
>       description: Creates a new pet in the store. Duplicates are allowed
>       operationId: addPet
>       requestBody:
>         description: Pet to add to the store
>         required: true
>         content:
>           application/json:
>             schema:
>               $ref: '#/components/schemas/NewPet'
49,50c68,73
<         '201':
<           description: Null response
---
>         '200':
>           description: pet response
>           content:
>             application/json:
>               schema:
>                 $ref: '#/components/schemas/Pet'
56,57c79,80
<                 $ref: "#/components/schemas/Error"
<   /pets/{petId}:
---
>                 $ref: '#/components/schemas/Error'
>   /pets/{id}:
59,62c82,83
<       summary: Info for a specific pet
<       operationId: showPetById
<       tags:
<         - pets
---
>       description: Returns a user based on a single ID, if the user does not have access to the pet
>       operationId: find pet by id
64c85
<         - name: petId
---
>         - name: id
65a87
>           description: ID of pet to fetch
67d88
<           description: The id of the pet to retrieve
69c90,91
<             type: string
---
>             type: integer
>             format: int64
72c94
<           description: Expected response to a valid request
---
>           description: pet response
76c98
<                 $ref: "#/components/schemas/Pet"
---
>                 $ref: '#/components/schemas/Pet'
82c104,124
<                 $ref: "#/components/schemas/Error"
---
>                 $ref: '#/components/schemas/Error'
>     delete:
>       description: deletes a single pet based on the ID supplied
>       operationId: deletePet
>       parameters:
>         - name: id
>           in: path
>           description: ID of pet to delete
>           required: true
>           schema:
>             type: integer
>             format: int64
>       responses:
>         '204':
>           description: pet deleted
>         default:
>           description: unexpected error
>           content:
>             application/json:
>               schema:
>                 $ref: '#/components/schemas/Error'
85a128,138
>       allOf:
>         - $ref: '#/components/schemas/NewPet'
>         - type: object
>           required:
>           - id
>           properties:
>             id:
>               type: integer
>               format: int64
> 
>     NewPet:
88,89c141
<         - id
<         - name
---
>         - name  
91,93d142
<         id:
<           type: integer
<           format: int64
97,102c146,147
<           type: string
<     Pets:
<       type: array
<       maxItems: 100
<       items:
<         $ref: "#/components/schemas/Pet"
---
>           type: string    
> 
Tufin/oasdiff による差分
### Modified Endpoints: 1
-------------------------
GET /pets/{petId}
- Modified path param: petId
  - Schema changed
    - Type changed from 'string' to 'integer'
    - Format changed from '' to 'int64'
- Responses changed
  - Modified response: 200
    - Content changed
      - Modified media type: application/json
        - Schema changed
          - Property 'AllOf' changed
            - Schemas added: [NewPet RevisionSchema[1]]
          - Type changed from 'object' to ''
          - Required changed
            - Deleted required property: id
            - Deleted required property: name
          - Properties changed
            - Deleted property: id
            - Deleted property: name
            - Deleted property: tag

Servers changed
- New server: https://petstore.swagger.io/v2
- Deleted server: http://petstore.swagger.io/v1

まとめ

現時点では検証のみを実施していますが、将来的にはこのプロセスを自動化し、Slackなどのチャットツールへの自動通知を組み込むことを検討しています。
このようなシステムを実装すれば、重要な変更に限定して通知を受け取ることができ、更新の監視が効率的になることが期待されます。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0