OpenAPI テンプレート
OpenAPI 3.1 の骨組みを生成:info、servers、tags、リソースごとに展開した CRUD パス、components.schemas、securitySchemes、よく使うレスポンスコードを含み、そのままリポジトリに置ける YAML を出力します。
ブラウザ内で動作計算はすべてブラウザ内で行われ、データが端末の外に出ることはありません。
ドキュメント情報
リソース
使用できる操作:list, create, get, update, delete
openapi.yaml
内容を openapi.yaml として保存すれば、Swagger UI、Redoc、openapi-generator からそのまま読み込めます。
openapi: 3.1.0
info:
title: Example API
version: 1.0.0
description: 由 UniKit 生成的 OpenAPI 骨架,按需增删字段即可。
servers:
- url: https://api.example.com/v1
tags:
- name: pets
description: Pet resources
paths:
/pets:
get:
tags:
- pets
summary: List pets
operationId: listPets
parameters:
- name: page
in: query
description: Page number
schema:
type: integer
minimum: 1
default: 1
- name: pageSize
in: query
description: Items per page
schema:
type: integer
minimum: 1
maximum: 100
default: 20
- name: sort
in: query
description: Sort expression, e.g. -createdAt
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/PetList'
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
post:
tags:
- pets
summary: Create a pet
operationId: createPet
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PetInput'
responses:
'201':
description: Created
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pets/{id}:
get:
tags:
- pets
summary: Get a pet
operationId: getPet
parameters:
- name: id
in: path
required: true
description: Pet identifier
schema:
type: integer
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: Not found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
put:
tags:
- pets
summary: Replace a pet
operationId: replacePet
parameters:
- name: id
in: path
required: true
description: Pet identifier
schema:
type: integer
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/PetInput'
responses:
'200':
description: Updated
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: Not found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
delete:
tags:
- pets
summary: Delete a pet
operationId: deletePet
parameters:
- name: id
in: path
required: true
description: Pet identifier
schema:
type: integer
responses:
'204':
description: Deleted
'400':
description: Bad request
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: Not found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
message:
type: string
details:
type: array
items:
type: string
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
name:
type: string
description: Display name
description:
type:
- string
- 'null'
description: Optional long text
createdAt:
type: string
format: date-time
readOnly: true
updatedAt:
type: string
format: date-time
readOnly: true
PetInput:
type: object
required:
- name
properties:
name:
type: string
description: Display name
description:
type:
- string
- 'null'
description: Optional long text
createdAt:
type: string
format: date-time
readOnly: true
updatedAt:
type: string
format: date-time
readOnly: true
PetList:
type: object
required:
- items
- total
properties:
items:
type: array
items:
$ref: '#/components/schemas/Pet'
total:
type: integer
description: Total number of records
page:
type: integer
minimum: 1
pageSize:
type: integer
minimum: 1
maximum: 100
securitySchemes:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
security:
- bearerAuth: []
統計
25410このツールでできること
- 新規プロジェクトの立ち上げ時に API の骨組みを先に固める:タイトル、servers、リソース名を埋めれば CRUD パスと schema が一度に生成され、チームはこの YAML を見ながら項目を議論できます。
- フロントエンド用の mock や SDK を作る:YAML を Swagger UI に渡してドキュメントを確認し、openapi-generator に渡してクライアントコードを生成と、両方が同じ定義を共有できます。
- チーム内のルールを統一:認証方式、ページングパラメータ、エラーレスポンスの構造をテンプレートが決めるので、エンドポイントごとに別々に書く必要がありません。
- ドキュメントを書くほうがコードより疲れるとき:モデル名と主キーのルールをリソース行に入れれば、残りのパス、operationId、$ref はすべて自動生成されます。
例
入力
タイトル:Pet Store API バージョン:1.0.0 servers:https://api.example.com/v1 認証:なし リソース:pets:list:id:integer:no 共通レスポンス:オフ
出力
openapi: 3.1.0
info:
title: Pet Store API
version: 1.0.0
servers:
- url: https://api.example.com/v1
tags:
- name: pets
description: Pet resources
paths:
/pets:
get:
tags:
- pets
summary: List pets
operationId: listPets
parameters:
- name: sort
in: query
description: Sort expression, e.g. -createdAt
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
components:
schemas:
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
message:
type: string
details:
type: array
items:
type: string
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
name:
type: string
description: Display name
description:
type:
- string
- 'null'
description: Optional long text
createdAt:
type: string
format: date-time
readOnly: true
updatedAt:
type: string
format: date-time
readOnly: true
PetInput:
type: object
required:
- name
properties:
name:
type: string
description: Display name
description:
type:
- string
- 'null'
description: Optional long text
createdAt:
type: string
format: date-time
readOnly: true
updatedAt:
type: string
format: date-time
readOnly: trueステータスコードは文字列キー('200')として出力されます。これは OpenAPI の要件です。モデル名はリソース名から導出され、pets から Pet になります。
よくある質問
生成された YAML はそのまま動きますか?
はい。OpenAPI 3.1 に準拠しているので、Swagger UI、Redoc、openapi-generator にそのまま渡せます。パス内の schema は $ref で components.schemas を指すため、参照切れは起きません。
リソース名はどう書けばよいですか?
pets や order-items のような小文字複数のパス断片を使います。モデル名は自動で導出され、order-items は OrderItem、categories は Category になります。期待どおりにならない場合はリソース行でモデル名を明示できます。
ページングパラメータはいつ現れますか?
list 操作があり、ページングを有効にしたときだけ page / pageSize のクエリパラメータが追加され、レスポンスは <モデル>List のラッパーを指します。ページングを切ると list はモデルの配列を直接参照し、ドキュメントに List schema も出ません。
認証方式を選ぶと何が生成されますか?
bearer は http + bearer + JWT の securityScheme、basic は http + basic、API Key は header 形式の X-API-Key を生成し、トップレベルに security の参照を追加します。「なし」を選ぶと securitySchemes も 401 レスポンスも出力しません。
operationId は衝突しませんか?
しません。list はリソースの複数名(listPets)を使い、単一リソースの操作はモデルの単数名(getPet、replacePet、deletePet)に統一されます。同じリソースは 1 回だけ生成され、リソース名の重複は検証の段階でエラーになります。
キーワード:openapiswaggerapi specrest apiswagger yaml