更新の設計
メモを書き換える操作(Update)を書き足します。
| メソッド | パス | 中身 |
|---|---|---|
PUT |
/notes/{id} |
送った中身でメモを置き換えて返す |
api/openapi.yaml を変更する
Section titled “api/openapi.yaml を変更する”111111 description: ID が UUID の形でない112112 '404':113113 description: メモが無い114put:115 summary: メモを置き換える(全部の項目を送る)116 parameters:117 - name: id118 in: path119 required: true120 schema:121 type: string122 format: uuid123 requestBody:124 required: true125 content:126 application/json:127 schema:128 type: object129 additionalProperties: false130 required: [title, body]131 properties:132 title:133 type: string134 minLength: 1135 maxLength: 100136 body:137 type: string138 maxLength: 1000139 responses:140 '200':141 description: 書き換えたメモ142 content:143 application/json:144 schema:145 type: object146 required: [id, title, body, createdAt, updatedAt]147 properties:148 id:149 type: string150 format: uuid151 title:152 type: string153 body:154 type: string155 createdAt:156 type: string157 format: date-time158 updatedAt:159 type: string160 format: date-time161 '400':162 description: 入力が決まりに合わない163 '404':164 description: メモが無いPUTは、送った中身でまるごと置き換えるメソッドですtitleとbodyの両方を必ず送ります。required: [title, body]で、どちらも省略できないことを書きます。片方だけ送ると400になります- Create と違って
defaultを書きません。全部の項目を送るので、補う値は要りません
部分更新にする場合
Section titled “部分更新にする場合”タイトルだけ・本文だけのように、送った項目だけを書き換えることを 部分更新 と呼びます。部分更新には PATCH を使うのが HTTP の決まりです。PATCH にするときは、メソッドを patch にし、本文の required を外して、どの項目も省略できるようにします。
プレビューで確かめる
Section titled “プレビューで確かめる”VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 PUT /notes/{id} が増え、本文の title と body が必須になっていれば大丈夫です。
参考にした公式ドキュメント
Section titled “参考にした公式ドキュメント”この節は、次の公式ドキュメントをもとに書いています。
- OpenAPI Specification v3.0.3 … Request Body Object
- RFC 9110: PUT …
PUTは送った中身で置き換えること - RFC 5789: PATCH Method for HTTP … 部分更新には
PATCHを使うこと