OpenAPI仕様(旧Swagger)は、RESTful APIを設計、構築、文書化、利用するための標準仕様です。この記事では、OpenAPI仕様を使用する際のベストプラクティスとよくある間違いについて解説します。
1. enum型の命名規則
ポイント: enum型の値は通常、小文字で定義されることが多い
# 良い例
status:
type: string
enum:
- pending
- completed
- failed
# 避けるべき例
status:
type: string
enum:
- PENDING
- COMPLETED
- FAILED
小文字のスネークケースを使用することで、JSONの一般的な命名規則と整合性が取れます。ただし、システムによっては大文字の enum 値が標準とされる場合もあるため、プロジェクトの規約に従いましょう。
2. descriptionの複数行記述
ポイント: descriptionが複数行にわたる場合は、|
または|-
記法を使用する
# 良い例 (|- を使用して末尾の改行を削除)
description: |-
これは複数行にわたる説明文です。
このように記述することで読みやすくなります。
末尾の改行は削除されます。
# 良い例 (| を使用して末尾の改行を保持)
description: |
これは複数行にわたる説明文です。
このように記述することで読みやすくなります。
末尾の改行も保持されます。
# 避けるべき例1
description: "これは長い説明文です。このように一行で書くと非常に読みにくくなります。特に文章が長くなるほど可読性が下がります。"
# 避けるべき例2
description:
"これは長い説明文です。"
"このように一行で書くと非常に読みにくくなります。"
"特に文章が長くなるほど可読性が下がります。"
3. コンポーネントの再利用
ポイント: 共通で使われるスキーマやレスポンスはコンポーネントとして定義し再利用する
components:
schemas:
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
paths:
/users:
get:
responses:
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
4. パスパラメータの統一
ポイント: 同じリソースに対するパスパラメータは一貫性を持たせる
# 良い例
paths:
/users/{userId}:
get:
parameters:
- name: userId
in: path
required: true
schema:
type: string
/users/{userId}/orders:
get:
parameters:
- name: userId
in: path
required: true
schema:
type: string
# 避けるべき例
paths:
/users/{id}:
# ...
/users/{user_id}/orders:
# ...
5. レスポンスの適切な定義
ポイント: すべての可能性のあるレスポンスステータスコードを定義する
# 良い例
responses:
'200':
description: 成功
content:
application/json:
schema:
$ref: '#/components/schemas/SuccessResponse'
'400':
description: リクエストエラー
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'401':
description: 認証エラー
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'500':
description: サーバーエラー
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
6. スキーマ定義での必須フィールド
ポイント: required
フィールドは配列内にプロパティ名を列挙する
# 良い例
User:
type: object
required:
- id
- name
- email
properties:
id:
type: string
name:
type: string
email:
type: string
age:
type: integer
# 避けるべき例(個別プロパティに required: true は使えない)
User:
type: object
properties:
id:
type: string
required: true # これは誤り
name:
type: string
required: true # これは誤り
7. 適切なデータ型とフォーマットの使用
ポイント: OpenAPIで定義された型とフォーマットを適切に使用する
properties:
id:
type: string
format: uuid
created_at:
type: string
format: date-time
count:
type: integer
format: int32
amount:
type: number
format: float
is_active:
type: boolean
8. セキュリティ定義
ポイント: API全体またはパスごとにセキュリティ要件を適切に定義する
components:
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
apiKeyAuth:
type: apiKey
in: header
name: X-API-KEY
security:
- bearerAuth: [] # デフォルトセキュリティ要件
paths:
/public/status:
get:
security: [] # セキュリティ要件なし(オーバーライド)
/admin/settings:
put:
security:
- bearerAuth: []
- apiKeyAuth: [] # 両方の認証が必要
9. 一貫性のあるレスポンスフォーマット
ポイント: APIレスポンスには一貫性のあるフォーマットを使用する
# 良い例
components:
schemas:
SuccessResponse:
type: object
required:
- data
properties:
data:
type: object
# データの具体的な構造
ErrorResponse:
type: object
required:
- error
properties:
error:
type: object
required:
- code
- message
properties:
code:
type: string
message:
type: string
details:
type: array
items:
type: object
10. バージョニング
ポイント: APIのバージョン管理戦略を明確に定義する
openapi: 3.1.0
info:
title: サンプルAPI
version: 1.0.0
description: |-
APIのバージョン管理戦略:
- メジャーバージョン: URLパスに含める (/v1/users)
- マイナーバージョン: リクエストヘッダーで指定 (X-API-Version: 1.2)
よくある間違いと注意点
1. 異なるレスポンス形式の混在
異なるエンドポイントで一貫性のないレスポンス形式を使用すると、クライアント側で処理が複雑になります。
2. 過剰なネスト
JSONスキーマを過度にネストすると、複雑さが増し、クライアントでの扱いが難しくなります。可能な限りフラットな構造を心がけましょう。
3. 不完全なドキュメント
各エンドポイント、パラメータ、レスポンスに適切な説明(description)を提供しないと、APIの使用者は意図を理解できません。
4. セマンティックバージョニングの無視
APIのバージョニングにおいては、セマンティックバージョニング(SemVer)の原則に従い、互換性の変更を明確に示すことが重要です。
5. 例示の欠如
example
やexamples
フィールドを活用して、各パラメータやレスポンスの例を提供することで、API理解が促進されます。
components:
schemas:
User:
type: object
properties:
id:
type: string
example: "12345"
name:
type: string
example: "山田太郎"
example:
id: "12345"
name: "山田太郎"
まとめ
OpenAPI仕様は、APIの設計と文書化において非常に強力なツールです。適切な規則とベストプラクティスに従うことで、開発者にとって使いやすく、理解しやすいAPIを提供することができます。
特に重要なのは一貫性です。命名規則、レスポンス形式、エラーハンドリングなどで一貫したアプローチを取ることで、APIの品質と使いやすさが大幅に向上します。