共通部分の設計
ここまでで、メモの形を 4 回、id のパラメーターを 3 回、同じように書きました。同じものは components にまとめ、$ref で参照します。片方だけ直し忘れることがなくなります。
api/openapi.yaml を変更する
Section titled “api/openapi.yaml を変更する”1616 schema:1717 type: array1818 items:19 type: object20 required: [id, title, body, createdAt, updatedAt]21 properties:22 id:23 type: string24 format: uuid25 title:26 type: string27 body:28 type: string29 createdAt:30 type: string31 format: date-time32 updatedAt:33 type: string34 format: date-time19 $ref: '#/components/schemas/Note'3520post:3621 summary: メモを作る3722 requestBody:3924 content:4025 application/json:4126 schema:42 type: object43 additionalProperties: false44 required: [title]45 properties:46 title:47 type: string48 minLength: 149 maxLength: 10050 body:51 type: string52 maxLength: 100053 default: ''27 $ref: '#/components/schemas/CreateNoteInput'5428 responses:5529 '201':5630 description: 作ったメモ5731 content:5832 application/json:5933 schema:60 type: object61 required: [id, title, body, createdAt, updatedAt]62 properties:63 id:64 type: string65 format: uuid66 title:67 type: string68 body:69 type: string70 createdAt:71 type: string72 format: date-time73 updatedAt:74 type: string75 format: date-time34 $ref: '#/components/schemas/Note'7635 '400':77 description: 入力が決まりに合わない36 $ref: '#/components/responses/BadRequest'7837 /notes/{id}:7938 get:8039 summary: メモを 1 件返す8140 parameters:82 - name: id83 in: path84 required: true85 schema:86 type: string87 format: uuid41 - $ref: '#/components/parameters/NoteId'8842 responses:8943 '200':9044 description: メモ9145 content:9246 application/json:9347 schema:94 type: object95 required: [id, title, body, createdAt, updatedAt]96 properties:97 id:98 type: string99 format: uuid100 title:101 type: string102 body:103 type: string104 createdAt:105 type: string106 format: date-time107 updatedAt:108 type: string109 format: date-time48 $ref: '#/components/schemas/Note'11049 '400':111 description: ID が UUID の形でない50 $ref: '#/components/responses/BadRequest'11251 '404':113 description: メモが無い52 $ref: '#/components/responses/NotFound'11453 put:11554 summary: メモを置き換える(全部の項目を送る)11655 parameters:117 - name: id118 in: path119 required: true120 schema:121 type: string122 format: uuid56 - $ref: '#/components/parameters/NoteId'12357 requestBody:12458 required: true12559 content:12660 application/json:12761 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: 100062 $ref: '#/components/schemas/UpdateNoteInput'13963 responses:14064 '200':14165 description: 書き換えたメモ14266 content:14367 application/json:14468 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-time69 $ref: '#/components/schemas/Note'16170 '400':162 description: 入力が決まりに合わない71 $ref: '#/components/responses/BadRequest'16372 '404':164 description: メモが無い73 $ref: '#/components/responses/NotFound'16574 delete:16675 summary: メモを消す16776 parameters:168 - name: id169 in: path170 required: true171 schema:172 type: string173 format: uuid77 - $ref: '#/components/parameters/NoteId'17478 responses:17579 '204':17680 description: 消した17781 '400':178 description: ID が UUID の形でない82 $ref: '#/components/responses/BadRequest'17983 '404':180 description: メモが無い84 $ref: '#/components/responses/NotFound'85components:86 parameters:87 NoteId:88 name: id89 in: path90 required: true91 schema:92 type: string93 format: uuid94 schemas:95 Note:96 type: object97 required: [id, title, body, createdAt, updatedAt]98 properties:99 id:100 type: string101 format: uuid102 title:103 type: string104 body:105 type: string106 createdAt:107 type: string108 format: date-time109 updatedAt:110 type: string111 format: date-time112 CreateNoteInput:113 type: object114 additionalProperties: false115 required: [title]116 properties:117 title:118 type: string119 minLength: 1120 maxLength: 100121 body:122 type: string123 maxLength: 1000124 default: ''125 UpdateNoteInput:126 type: object127 additionalProperties: false128 required: [title, body]129 properties:130 title:131 type: string132 minLength: 1133 maxLength: 100134 body:135 type: string136 maxLength: 1000137 responses:138 BadRequest:139 description: 入力が決まりに合わない140 NotFound:141 description: メモが無いcomponents の中 |
まとめたもの |
|---|---|
schemas |
メモの形(Note)、作るときの本文(CreateNoteInput)、書き換えるときの本文(UpdateNoteInput) |
parameters |
パスの id(NoteId) |
responses |
400(BadRequest)・404(NotFound) |
$ref: '#/components/schemas/Note'は、「このファイルのcomponents→schemas→Note」を指します。Cmd(Windows・Linux は Ctrl)を押しながらクリックすると、参照先へ移動しますschemasに付けた名前は、04 章で作る TypeScript の型の名前にもなります
プレビューで確かめる
Section titled “プレビューで確かめる”VS Code で api/openapi.yaml を開き、エディタの右上のプレビューのボタンを押します。横に API のドキュメントの画面が出ます。 前の節と同じ 5 つの操作が、同じ形で見えれば大丈夫です。まとめても、約束の中身は変わりません。
参考にした公式ドキュメント
Section titled “参考にした公式ドキュメント”この節は、次の公式ドキュメントをもとに書いています。
- OpenAPI Specification v3.0.3 … Components Object と Reference Object(
$ref) - 42Crunch: OpenAPI (Swagger) Editor(README) …
$refの参照先へ移動