作成の設計
メモを作る操作(Create)を書き足します。
| メソッド | パス | 中身 |
|---|---|---|
POST |
/notes |
本文(JSON)からメモを作り、201 で返す |
api/openapi.yaml を変更する
Section titled “api/openapi.yaml を変更する”44 version: 1.0.055servers:66 - url: http://localhost:30007paths: {}7paths:8 /notes:9 post:10 summary: メモを作る11 requestBody:12 required: true13 content:14 application/json:15 schema:16 type: object17 additionalProperties: false18 required: [title]19 properties:20 title:21 type: string22 minLength: 123 maxLength: 10024 body:25 type: string26 maxLength: 100027 default: ''28 responses:29 '201':30 description: 作ったメモ31 content:32 application/json:33 schema:34 type: object35 required: [id, title, body, createdAt, updatedAt]36 properties:37 id:38 type: string39 format: uuid40 title:41 type: string42 body:43 type: string44 createdAt:45 type: string46 format: date-time47 updatedAt:48 type: string49 format: date-time50 '400':51 description: 入力が決まりに合わないpaths→/notes→post…POST /notesの操作ですrequestBody… 送る本文の形です。requiredに書いたtitleは必ず送り、bodyは省略すると''になります。additionalProperties: falseは、ほかの項目を受け付けないという意味ですidは送る本文に書きません。ID はサーバーが作ります。additionalProperties: falseがあるので、idを送っても受け付けませんresponses… ステータスコードごとの応答です。キーは'201'のように文字列で書きます。400は入力が決まりに合わないときです
プレビューで確かめる
Section titled “プレビューで確かめる”VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 POST /notes が出て、送る本文と返るメモの形が見えれば大丈夫です。
参考にした公式ドキュメント
Section titled “参考にした公式ドキュメント”この節は、次の公式ドキュメントをもとに書いています。
- OpenAPI Specification v3.0.3 … Operation Object・Request Body Object・Responses Object
- RFC 9110: 201 Created …
201の意味 - RFC 9110: Location …
201の応答のLocationが指すもの - 42Crunch: OpenAPI (Swagger) Editor(README) … プレビュー