単一取得の設計
メモを 1 件返す操作(Get)を書き足します。
| メソッド | パス | 中身 |
|---|---|---|
GET |
/notes/{id} |
メモを 1 件返す。無ければ 404 |
api/openapi.yaml を変更する
Section titled “api/openapi.yaml を変更する”4949 format: date-time5050 '400':5151 description: 入力が決まりに合わない52/notes/{id}:53 get:54 summary: メモを 1 件返す55 parameters:56 - name: id57 in: path58 required: true59 schema:60 type: string61 format: uuid62 responses:63 '200':64 description: メモ65 content:66 application/json:67 schema:68 type: object69 required: [id, title, body, createdAt, updatedAt]70 properties:71 id:72 type: string73 format: uuid74 title:75 type: string76 body:77 type: string78 createdAt:79 type: string80 format: date-time81 updatedAt:82 type: string83 format: date-time84 '400':85 description: ID が UUID の形でない86 '404':87 description: メモが無い- 応答のメモの形は、前の節の
201と同じものをもう一度書いています。同じ形をまとめるのは、この章の最後の節です 400は ID が UUID の形でないとき、404はメモが無いときです
パスパラメーターの書き方
Section titled “パスパラメーターの書き方”/notes/{id} の {id} のように、パスの中で変わる部分を パスパラメーター と呼びます。/notes/1b9d6bcd-... へのリクエストでは、id が 1b9d6bcd-... になります。
- パスの中では、名前を
{}で囲みます parametersに、同じ名前(name: id)で 1 つ書きますin: path… パスの中の値だという意味ですrequired: true… パスパラメーターでは必ず書きます(省略できる値にはできません)schema… 値の形です。ここでは UUID の形の文字列にします
プレビューで確かめる
Section titled “プレビューで確かめる”VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 GET /notes/{id} が増え、id のパラメーターと 200・400・404 の応答が見えれば大丈夫です。
参考にした公式ドキュメント
Section titled “参考にした公式ドキュメント”この節は、次の公式ドキュメントをもとに書いています。
- OpenAPI Specification v3.0.3 … Path Item・Parameter Object