コンテンツにスキップ

単一取得の設計

03 章 単一取得の設計

メモを 1 件返す操作(Get)を書き足します。

メソッド パス 中身
GET /notes/{id} メモを 1 件返す。無ければ 404
api/openapi.yaml
49
49
format: date-time
50
50
'400':
51
51
description: 入力が決まりに合わない
52
/notes/{id}:
53
get:
54
summary: メモを 1 件返す
55
parameters:
56
- name: id
57
in: path
58
required: true
59
schema:
60
type: string
61
format: uuid
62
responses:
63
'200':
64
description: メモ
65
content:
66
application/json:
67
schema:
68
type: object
69
required: [id, title, body, createdAt, updatedAt]
70
properties:
71
id:
72
type: string
73
format: uuid
74
title:
75
type: string
76
body:
77
type: string
78
createdAt:
79
type: string
80
format: date-time
81
updatedAt:
82
type: string
83
format: date-time
84
'400':
85
description: ID が UUID の形でない
86
'404':
87
description: メモが無い
  • 応答のメモの形は、前の節の 201 と同じものをもう一度書いています。同じ形をまとめるのは、この章の最後の節です
  • 400 は ID が UUID の形でないとき、404 はメモが無いときです

/notes/{id} の {id} のように、パスの中で変わる部分を パスパラメーター と呼びます。/notes/1b9d6bcd-... へのリクエストでは、id が 1b9d6bcd-... になります。

  • パスの中では、名前を {} で囲みます
  • parameters に、同じ名前(name: id)で 1 つ書きます
  • in: path … パスの中の値だという意味です
  • required: true … パスパラメーターでは必ず書きます(省略できる値にはできません)
  • schema … 値の形です。ここでは UUID の形の文字列にします

VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 GET /notes/{id} が増え、id のパラメーターと 200・400・404 の応答が見えれば大丈夫です。

この節は、次の公式ドキュメントをもとに書いています。

この節を終えたときのディレクトリ

Section titled “この節を終えたときのディレクトリ”
この節を終えた状態をダウンロード