-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenapi-content.tex
More file actions
453 lines (379 loc) · 13.1 KB
/
Copy pathopenapi-content.tex
File metadata and controls
453 lines (379 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
\section{OpenAPI Overview}
\begin{frame}{Why This Gets Its Own Short Overview}
\begin{itemize}
\item Lab 3 is about building a REST-style API with Express.
\item OpenAPI is how we write down the API contract clearly.
\item The contract helps humans, tests, clients, and tools agree on what the server should do.
\end{itemize}
\vspace{0.35cm}
\begin{block}{Main idea}
The Express server is the thing that runs. The OpenAPI file is the written promise about how that server behaves.
\end{block}
\end{frame}
\begin{frame}{Where OpenAPI Fits}
\begin{center}
\begin{tikzpicture}[node distance=1.65cm]
\node[clientserverbox] (client) {Client\\Application};
\node[clientserverbox, right=of client] (api) {REST API\\Server};
\node[clientserverbox, below=of api] (spec) {OpenAPI\\Document};
\draw[archarrowboth] (client) -- node[archlabel] {HTTP + JSON} (api);
\draw[archarrow] (spec) -- node[archlabel, right] {describes} (api);
\draw[archarrow] (spec) -| node[archlabel, near start] {guides} (client);
\end{tikzpicture}
\end{center}
\vspace{0.25cm}
\begin{itemize}
\item The client needs to know what requests are allowed.
\item The server needs to return predictable responses.
\item The spec records that agreement in YAML or JSON.
\end{itemize}
\end{frame}
\begin{frame}{The Problem OpenAPI Solves}
\begin{columns}[T,totalwidth=\textwidth]
\begin{column}{0.48\textwidth}
\begin{block}{Without a contract}
\begin{itemize}
\item Guess the URL
\item Guess the JSON shape
\item Guess the status codes
\item Hope the README is current
\end{itemize}
\end{block}
\end{column}
\begin{column}{0.48\textwidth}
\begin{block}{With OpenAPI}
\begin{itemize}
\item Routes are listed
\item Schemas are defined
\item Examples are visible
\item Tools can validate it
\end{itemize}
\end{block}
\end{column}
\end{columns}
\vspace{0.35cm}
\centering
\textbf{Old-fashioned engineering rule: write down the interface.}
\end{frame}
\section{The Basic Structure}
\begin{frame}{OpenAPI Is Not the API}
\begin{center}
\begin{tikzpicture}[node distance=1.55cm]
\node[coursebox] (doc) {\textbf{OpenAPI Spec}\\YAML or JSON};
\node[coursebox, below left=of doc] (human) {Human Docs\\Swagger UI / Redoc};
\node[coursebox, below=of doc] (tests) {Validation\\Tests};
\node[coursebox, below right=of doc] (clients) {Generated\\Clients};
\draw[archarrow] (doc) -- (human);
\draw[archarrow] (doc) -- (tests);
\draw[archarrow] (doc) -- (clients);
\end{tikzpicture}
\end{center}
\vspace{0.2cm}
\begin{itemize}
\item The spec describes expected behavior.
\item Your Express routes still have to implement that behavior.
\item The spec and code can drift if nobody checks them.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{A Tiny OpenAPI File}
\begin{lstlisting}[style=code,basicstyle=\ttfamily\scriptsize,language={}]
openapi: 3.0.3
info:
title: Items API
version: 1.0.0
paths:
/items:
get:
summary: List all items
responses:
"200":
description: A list of items
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Item"
\end{lstlisting}
\vspace{0.15cm}
Even a small file can describe what route exists and what a successful response looks like.
\end{frame}
\begin{frame}{The Main Pieces}
\begin{center}
\begin{tikzpicture}[node distance=0.55cm]
\node[monolithlayer] (openapi) {\textbf{openapi} -- spec version};
\node[monolithlayer, below=of openapi] (info) {\textbf{info} -- title, version, description};
\node[monolithlayer, below=of info] (servers) {\textbf{servers} -- base URLs};
\node[monolithlayer, below=of servers] (paths) {\textbf{paths} -- routes and HTTP methods};
\node[monolithlayer, below=of paths] (components) {\textbf{components} -- reusable schemas, parameters, responses};
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}{Lab 3 API at a Glance}
\begin{center}
\begin{tabular}{lll}
\textbf{Method} & \textbf{Path} & \textbf{Purpose} \\
GET & \texttt{/health} & Check that the server is running \\
GET & \texttt{/items} & Get all items \\
POST & \texttt{/items} & Create a new item \\
GET & \texttt{/items/\{id\}} & Get one item \\
PUT & \texttt{/items/\{id\}} & Update one item \\
DELETE & \texttt{/items/\{id\}} & Delete one item \\
\end{tabular}
\end{center}
\vspace{0.35cm}
\begin{block}{Teaching point}
This table is what most students expect a README to say. OpenAPI says the same thing in a tool-readable form.
\end{block}
\end{frame}
\section{Paths, Methods, and Responses}
\begin{frame}{Paths Describe Resources}
\begin{itemize}
\item A path is the URL pattern after the server base URL.
\item Each path can have one or more HTTP methods.
\item The method describes the kind of action being requested.
\end{itemize}
\vspace{0.3cm}
\begin{center}
\begin{tabular}{ll}
\textbf{HTTP Method} & \textbf{Typical REST Meaning} \\
GET & Read a resource \\
POST & Create a resource \\
PUT & Replace a resource \\
PATCH & Partially update a resource \\
DELETE & Delete a resource \\
\end{tabular}
\end{center}
\end{frame}
\begin{frame}[fragile]{Path Parameters}
\begin{lstlisting}[style=code,basicstyle=\ttfamily\scriptsize,language={}]
paths:
/items/{id}:
get:
summary: Get one item by ID
parameters:
- $ref: "#/components/parameters/ItemId"
responses:
"200":
description: The requested item
"404":
description: Item not found
\end{lstlisting}
\vspace{0.15cm}
\textbf{Rule of thumb:} every \texttt{\{thing\}} in the path needs a matching parameter definition.
\end{frame}
\begin{frame}[fragile]{Reusable Parameters}
\begin{lstlisting}[style=code,basicstyle=\ttfamily\scriptsize,language={}]
components:
parameters:
ItemId:
name: id
in: path
required: true
description: Numeric item ID
schema:
type: integer
example: 1
\end{lstlisting}
\vspace{0.15cm}
Using \texttt{components} avoids repeating the same parameter definition for GET, PUT, and DELETE.
\end{frame}
\begin{frame}{Status Codes Are Part of the Contract}
\begin{center}
\begin{tabular}{ll}
\textbf{Code} & \textbf{Meaning in this lab} \\
200 & Request succeeded \\
201 & Item was created \\
204 & Item was deleted; no body returned \\
400 & Request body is invalid \\
404 & Requested item was not found \\
500 & Server failed unexpectedly \\
\end{tabular}
\end{center}
\vspace{0.35cm}
\begin{itemize}
\item Document the normal case and the common failure cases.
\item A useful client must know what can work and what can fail.
\end{itemize}
\end{frame}
\section{Schemas and Request Bodies}
\begin{frame}[fragile]{Schemas Describe JSON Shape}
\begin{lstlisting}[style=code,basicstyle=\ttfamily\scriptsize,language={}]
components:
schemas:
Item:
type: object
properties:
id:
type: integer
example: 1
name:
type: string
example: keyboard
quantity:
type: number
example: 10
required: [id, name, quantity]
\end{lstlisting}
\vspace{0.15cm}
Schemas answer: \textbf{What fields exist? What types are allowed? What is required?}
\end{frame}
\begin{frame}[fragile]{Input and Output Are Different}
\begin{lstlisting}[style=code,basicstyle=\ttfamily\scriptsize,language={}]
Item:
required: [id, name, quantity]
ItemInput:
required: [name, quantity]
\end{lstlisting}
\vspace{0.2cm}
\begin{itemize}
\item The client sends \texttt{name} and \texttt{quantity}.
\item The server creates the \texttt{id}.
\item That means request and response schemas do not always match.
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Request Bodies}
\begin{lstlisting}[style=code,basicstyle=\ttfamily\scriptsize,language={}]
paths:
/items:
post:
summary: Create a new item
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/ItemInput"
responses:
"201":
description: Item created
\end{lstlisting}
\vspace{0.2cm}
The schema should match what Express expects to read from \texttt{req.body}.
\end{frame}
\begin{frame}[fragile]{Error Responses Need Schemas Too}
\begin{lstlisting}[style=code,basicstyle=\ttfamily\scriptsize,language={}]
ErrorResponse:
type: object
properties:
error:
type: string
example: Item not found
required:
- error
\end{lstlisting}
\vspace{0.2cm}
\begin{block}{Why this matters}
Clients should not have to guess whether an error is returned as \texttt{error}, \texttt{message}, \texttt{msg}, or a plain string.
\end{block}
\end{frame}
\section{Using OpenAPI in Practice}
\begin{frame}{Spec First or Code First?}
\begin{columns}[T,totalwidth=\textwidth]
\begin{column}{0.48\textwidth}
\begin{block}{Spec first}
\begin{itemize}
\item Design the contract first
\item Then implement routes
\item Good for teams and grading
\end{itemize}
\end{block}
\end{column}
\begin{column}{0.48\textwidth}
\begin{block}{Code first}
\begin{itemize}
\item Write routes first
\item Generate or write docs after
\item Easy to start; drift is danger
\end{itemize}
\end{block}
\end{column}
\end{columns}
\vspace{0.35cm}
\centering
\textbf{For this course: learn the contract, then make the code match it.}
\end{frame}
\begin{frame}{What Tools Can Do With It}
\begin{itemize}
\item Render interactive documentation.
\item Mock an API before the server is finished.
\item Validate requests and responses.
\item Generate client code or server stubs.
\item Support tests that compare real behavior to the contract.
\end{itemize}
\vspace{0.35cm}
\begin{center}
\begin{tikzpicture}[node distance=1.35cm]
\node[clientserverbox] (yaml) {openapi.yaml};
\node[clientserverbox, right=of yaml] (docs) {Docs};
\node[clientserverbox, below=of docs] (mock) {Mocks};
\node[clientserverbox, left=of mock] (tests) {Tests};
\draw[archarrow] (yaml) -- (docs);
\draw[archarrow] (yaml) |- (mock);
\draw[archarrow] (yaml) -- (tests);
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}[fragile]{Serving Docs From Express}
\begin{lstlisting}[style=code,basicstyle=\ttfamily\scriptsize,language=JavaScript]
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import YAML from 'yamljs';
const app = express();
const openapi = YAML.load('./openapi.yaml');
app.use('/docs', swaggerUi.serve, swaggerUi.setup(openapi));
\end{lstlisting}
\vspace{0.15cm}
This does not magically implement the API. It only displays the contract. No fairy dust, no tiny REST goblins.
\end{frame}
\begin{frame}{Common Student Mistakes}
\begin{itemize}
\item Documenting a route that does not exist in Express.
\item Implementing a route that is missing from OpenAPI.
\item Returning a different JSON shape than the schema says.
\item Forgetting failure responses such as 400 and 404.
\item Treating examples as validation rules.
\end{itemize}
\vspace{0.35cm}
\begin{block}{Good habit}
After changing routes, update the spec or the tests immediately. Do not leave the contract stale.
\end{block}
\end{frame}
\section{How This Connects to Lab 3}
\begin{frame}{Lab 3 Mental Model}
\begin{center}
\begin{tikzpicture}[node distance=1.3cm]
\node[clientserverbox] (readme) {README\\Human instructions};
\node[clientserverbox, right=of readme] (openapi) {OpenAPI\\Machine-readable contract};
\node[clientserverbox, below=of openapi] (express) {Express\\Implementation};
\node[clientserverbox, left=of express] (tests) {Tests\\Evidence};
\draw[archarrowboth] (readme) -- (openapi);
\draw[archarrow] (openapi) -- node[archlabel, right] {should match} (express);
\draw[archarrow] (tests) -- node[archlabel] {check} (express);
\draw[archarrow] (tests) -- node[archlabel, left] {can compare} (openapi);
\end{tikzpicture}
\end{center}
\vspace{0.2cm}
\centering
The goal is not pretty YAML. The goal is a clear, testable API contract.
\end{frame}
\begin{frame}{What Students Should Be Able To Do}
\begin{itemize}
\item Read an OpenAPI file and identify the available routes.
\item Explain the request and response shape for each route.
\item Match Express route behavior to the documented contract.
\item Add or update a small path, schema, or response.
\item Notice when code and documentation disagree.
\end{itemize}
\end{frame}
\begin{frame}{Takeaways}
\begin{itemize}
\item OpenAPI is a contract for HTTP APIs.
\item It documents paths, methods, parameters, bodies, schemas, and responses.
\item Tools can turn the contract into docs, mocks, validation, and generated code.
\item The hard part is keeping the contract honest.
\end{itemize}
\vspace{0.45cm}
\centering
\Large \textbf{A client/server system is easier to build when the interface is written down.}
\end{frame}