|
1
|
import fs from "fs/promises" |
|
2
|
import { OpenAPIObject, SchemaObject } from "openapi3-ts" |
|
3
|
import { OperationObject } from "openapi3-ts/src/model/OpenApi" |
|
4
|
import path from "path" |
|
5
|
import { v4 as uid } from "uuid" |
|
6
|
import { getTmpDirectory } from "../utils/fs-utils" |
|
7
|
import { readYaml } from "../utils/yaml-utils" |
|
8
|
import { readJson } from "../utils/json-utils" |
|
9
|
import execa from "execa" |
|
10
|
|
|
11
|
/** |
|
12
|
* OAS output directory |
|
13
|
* |
|
14
|
* @privateRemarks |
|
15
|
* This should be the only directory OAS is loaded from for Medusa V2. |
|
16
|
* For now, we only use it if the --v2 flag it passed to the CLI tool. |
|
17
|
*/ |
|
18
|
const oasOutputPath = path.resolve( |
|
19
|
__dirname, |
|
20
|
"..", |
|
21
|
"..", |
|
22
|
"..", |
|
23
|
"..", |
|
24
|
"..", |
|
25
|
"..", |
|
26
|
"www", |
|
27
|
"utils", |
|
28
|
"generated", |
|
29
|
"oas-output" |
|
30
|
) |
|
31
|
const basePath = path.resolve(__dirname, `../../`) |
|
32
|
|
|
33
|
export const runCLI = async (command: string, options: string[] = []) => { |
|
34
|
const params = ["run", "medusa-oas", command, ...options] |
|
35
|
try { |
|
36
|
const { all: logs } = await execa("yarn", params, { |
|
37
|
cwd: basePath, |
|
38
|
all: true, |
|
39
|
}) |
|
40
|
} catch (err) { |
|
41
|
throw new Error(err.message + err.all) |
|
42
|
} |
|
43
|
} |
|
44
|
|
|
45
|
const listOperations = (oas: OpenAPIObject): OperationObject[] => { |
|
46
|
const operations: OperationObject[] = [] |
|
47
|
for (const url in oas.paths) { |
|
48
|
if (oas.paths.hasOwnProperty(url)) { |
|
49
|
const path = oas.paths[url] |
|
50
|
for (const method in path) { |
|
51
|
if (path.hasOwnProperty(method)) { |
|
52
|
switch (method) { |
|
53
|
case "get": |
|
54
|
case "put": |
|
55
|
case "post": |
|
56
|
case "delete": |
|
57
|
case "options": |
|
58
|
case "head": |
|
59
|
case "patch": |
|
60
|
operations.push(path[method]) |
|
61
|
break |
|
62
|
} |
|
63
|
} |
|
64
|
} |
|
65
|
} |
|
66
|
} |
|
67
|
return operations |
|
68
|
} |
|
69
|
|
|
70
|
describe("command oas", () => { |
|
71
|
let tmpDir: string |
|
72
|
|
|
73
|
beforeAll(async () => { |
|
74
|
tmpDir = await getTmpDirectory() |
|
75
|
}) |
|
76
|
|
|
77
|
describe("--type admin", () => { |
|
78
|
let oas: OpenAPIObject |
|
79
|
|
|
80
|
/** |
|
81
|
* In a CI context, beforeAll might exceed the configured jest timeout. |
|
82
|
* Until we upgrade our jest version, the timeout error will be swallowed |
|
83
|
* and the test will fail in unexpected ways. |
|
84
|
*/ |
|
85
|
beforeAll(async () => { |
|
86
|
const outDir = path.resolve(tmpDir, uid()) |
|
87
|
await runCLI("oas", ["--type", "admin", "--out-dir", outDir, "--local"]) |
|
88
|
const generatedFilePath = path.resolve(outDir, "admin.oas.json") |
|
89
|
oas = (await readJson(generatedFilePath)) as OpenAPIObject |
|
90
|
}) |
|
91
|
|
|
92
|
it("generates oas with admin routes only", async () => { |
|
93
|
const routes = Object.keys(oas.paths) |
|
94
|
expect(routes.includes("/admin/products")).toBeTruthy() |
|
95
|
expect(routes.includes("/store/products")).toBeFalsy() |
|
96
|
}) |
|
97
|
|
|
98
|
it("generates oas using admin.oas.base.yaml", async () => { |
|
99
|
const yamlFilePath = path.resolve( |
|
100
|
oasOutputPath, |
|
101
|
"base", |
|
102
|
"admin.oas.base.yaml" |
|
103
|
) |
|
104
|
const oasBase = (await readYaml(yamlFilePath)) as OpenAPIObject |
|
105
|
expect(oas.info.title).toEqual(oasBase.info.title) |
|
106
|
}) |
|
107
|
}) |
|
108
|
|
|
109
|
describe("--type store", () => { |
|
110
|
let oas: OpenAPIObject |
|
111
|
|
|
112
|
beforeAll(async () => { |
|
113
|
const outDir = path.resolve(tmpDir, uid()) |
|
114
|
await runCLI("oas", ["--type", "store", "--out-dir", outDir, "--local"]) |
|
115
|
const generatedFilePath = path.resolve(outDir, "store.oas.json") |
|
116
|
oas = (await readJson(generatedFilePath)) as OpenAPIObject |
|
117
|
}) |
|
118
|
|
|
119
|
it("generates oas with store routes only", async () => { |
|
120
|
const routes = Object.keys(oas.paths) |
|
121
|
expect(routes.includes("/admin/products")).toBeFalsy() |
|
122
|
expect(routes.includes("/store/products")).toBeTruthy() |
|
123
|
}) |
|
124
|
|
|
125
|
it("generates oas using store.oas.base.yaml", async () => { |
|
126
|
const yamlFilePath = path.resolve( |
|
127
|
oasOutputPath, |
|
128
|
"base", |
|
129
|
"store.oas.base.yaml" |
|
130
|
) |
|
131
|
const oasBase = (await readYaml(yamlFilePath)) as OpenAPIObject |
|
132
|
expect(oas.info.title).toEqual(oasBase.info.title) |
|
133
|
}) |
|
134
|
}) |
|
135
|
|
|
136
|
describe("--type combined", () => { |
|
137
|
let oas: OpenAPIObject |
|
138
|
|
|
139
|
beforeAll(async () => { |
|
140
|
const outDir = path.resolve(tmpDir, uid()) |
|
141
|
await runCLI("oas", [ |
|
142
|
"--type", |
|
143
|
"combined", |
|
144
|
"--out-dir", |
|
145
|
outDir, |
|
146
|
"--local", |
|
147
|
]) |
|
148
|
const generatedFilePath = path.resolve(outDir, "combined.oas.json") |
|
149
|
oas = (await readJson(generatedFilePath)) as OpenAPIObject |
|
150
|
}) |
|
151
|
|
|
152
|
it("generates oas with admin and store routes", async () => { |
|
153
|
const routes = Object.keys(oas.paths) |
|
154
|
expect(routes.includes("/admin/products")).toBeTruthy() |
|
155
|
expect(routes.includes("/store/products")).toBeTruthy() |
|
156
|
}) |
|
157
|
|
|
158
|
it("generates oas using default.oas.base.yaml", async () => { |
|
159
|
const yamlFilePath = path.resolve( |
|
160
|
basePath, |
|
161
|
"oas", |
|
162
|
"default.oas.base.yaml" |
|
163
|
) |
|
164
|
const oasBase = (await readYaml(yamlFilePath)) as OpenAPIObject |
|
165
|
expect(oas.info.title).toEqual(oasBase.info.title) |
|
166
|
}) |
|
167
|
|
|
168
|
it("prefixes tags with api type", async () => { |
|
169
|
const found = (oas.tags ?? []).filter((tag) => { |
|
170
|
return !(tag.name.startsWith("Admin") || tag.name.startsWith("Store")) |
|
171
|
}) |
|
172
|
expect(found).toEqual([]) |
|
173
|
}) |
|
174
|
|
|
175
|
it("prefixes route's tags with api type", async () => { |
|
176
|
const tags: string[] = listOperations(oas) |
|
177
|
.map((operation) => { |
|
178
|
return operation.tags ?? [] |
|
179
|
}) |
|
180
|
.flat() |
|
181
|
const found = tags.filter((tag) => { |
|
182
|
return !(tag.startsWith("Admin") || tag.startsWith("Store")) |
|
183
|
}) |
|
184
|
expect(found).toEqual([]) |
|
185
|
}) |
|
186
|
|
|
187
|
it("prefixes route's operationId with api type", async () => { |
|
188
|
const operationIds: string[] = listOperations(oas) |
|
189
|
.map((operation) => operation.operationId) |
|
190
|
.filter((operationId): operationId is string => !!operationId) |
|
191
|
const found = operationIds.filter((tag) => { |
|
192
|
return !(tag.startsWith("Admin") || tag.startsWith("Store")) |
|
193
|
}) |
|
194
|
expect(found).toEqual([]) |
|
195
|
}) |
|
196
|
|
|
197
|
it("combines components.schemas from admin and store", async () => { |
|
198
|
const schemas = Object.keys(oas.components?.schemas ?? {}) |
|
199
|
expect(schemas.includes("AdminProductsListRes")).toBeTruthy() |
|
200
|
expect(schemas.includes("StoreProductsListRes")).toBeTruthy() |
|
201
|
}) |
|
202
|
}) |
|
203
|
|
|
204
|
/** |
|
205
|
* to optimize test suite time, we only test --paths with the store api |
|
206
|
*/ |
|
207
|
describe("--paths", () => { |
|
208
|
let oas: OpenAPIObject |
|
209
|
|
|
210
|
beforeAll(async () => { |
|
211
|
const fileContent = ` |
|
212
|
/** @oas [get] /foobar/tests |
|
213
|
* operationId: GetFoobarTests |
|
214
|
*/ |
|
215
|
/** @oas [get] /store/regions |
|
216
|
* operationId: OverwrittenOperation |
|
217
|
*/ |
|
218
|
/** |
|
219
|
* @schema FoobarTestSchema |
|
220
|
* type: object |
|
221
|
* properties: |
|
222
|
* foo: |
|
223
|
* type: string |
|
224
|
*/ |
|
225
|
/** |
|
226
|
* @schema StoreRegionsListRes |
|
227
|
* type: object |
|
228
|
* properties: |
|
229
|
* foo: |
|
230
|
* type: string |
|
231
|
*/ |
|
232
|
` |
|
233
|
const additionalPath = path.resolve(tmpDir, uid()) |
|
234
|
const filePath = path.resolve(additionalPath, "foobar.ts") |
|
235
|
await fs.mkdir(additionalPath, { recursive: true }) |
|
236
|
await fs.writeFile(filePath, fileContent, "utf8") |
|
237
|
|
|
238
|
const outDir = path.resolve(tmpDir, uid()) |
|
239
|
await runCLI("oas", [ |
|
240
|
"--type", |
|
241
|
"store", |
|
242
|
"--out-dir", |
|
243
|
outDir, |
|
244
|
"--paths", |
|
245
|
additionalPath, |
|
246
|
"--local", |
|
247
|
]) |
|
248
|
const generatedFilePath = path.resolve(outDir, "store.oas.json") |
|
249
|
oas = (await readJson(generatedFilePath)) as OpenAPIObject |
|
250
|
}) |
|
251
|
|
|
252
|
it("should add new path to existing paths", async () => { |
|
253
|
const routes = Object.keys(oas.paths) |
|
254
|
expect(routes.includes("/store/products")).toBeTruthy() |
|
255
|
expect(routes.includes("/foobar/tests")).toBeTruthy() |
|
256
|
}) |
|
257
|
|
|
258
|
it("should overwrite existing path", async () => { |
|
259
|
expect(oas.paths["/store/regions"]["get"].operationId).toBe( |
|
260
|
"OverwrittenOperation" |
|
261
|
) |
|
262
|
}) |
|
263
|
|
|
264
|
it("should add new schema to existing schemas", async () => { |
|
265
|
const schemas = Object.keys(oas.components?.schemas ?? {}) |
|
266
|
expect(schemas.includes("StoreProductsListRes")).toBeTruthy() |
|
267
|
expect(schemas.includes("FoobarTestSchema")).toBeTruthy() |
|
268
|
}) |
|
269
|
|
|
270
|
it("should overwrite existing schema", async () => { |
|
271
|
const schema = oas.components?.schemas?.StoreRegionsListRes as |
|
272
|
| SchemaObject |
|
273
|
| undefined |
|
274
|
expect(schema?.properties?.foo).toBeDefined() |
|
275
|
}) |
|
276
|
}) |
|
277
|
|
|
278
|
/** |
|
279
|
* to optimize test suite time, we only test --base with the store api |
|
280
|
*/ |
|
281
|
describe("--base", () => { |
|
282
|
let oas: OpenAPIObject |
|
283
|
|
|
284
|
beforeAll(async () => { |
|
285
|
const fileContent = ` |
|
286
|
openapi: 3.1.0 |
|
287
|
info: |
|
288
|
version: 1.0.1 |
|
289
|
title: Custom API |
|
290
|
servers: |
|
291
|
- url: https://foobar.com |
|
292
|
security: |
|
293
|
- api_key: [] |
|
294
|
externalDocs: |
|
295
|
url: https://docs.com |
|
296
|
webhooks: |
|
297
|
"foo-hook": |
|
298
|
get: |
|
299
|
responses: |
|
300
|
"200": |
|
301
|
description: OK |
|
302
|
tags: |
|
303
|
- name: Products |
|
304
|
description: Overwritten tag |
|
305
|
- name: FoobarTag |
|
306
|
description: Foobar tag description |
|
307
|
paths: |
|
308
|
"/foobar/tests": |
|
309
|
get: |
|
310
|
operationId: GetFoobarTests |
|
311
|
responses: |
|
312
|
"200": |
|
313
|
description: OK |
|
314
|
"/store/regions": |
|
315
|
get: |
|
316
|
operationId: OverwrittenOperation |
|
317
|
responses: |
|
318
|
"200": |
|
319
|
description: OK |
|
320
|
components: |
|
321
|
schemas: |
|
322
|
FoobarTestSchema: |
|
323
|
type: object |
|
324
|
properties: |
|
325
|
foo: |
|
326
|
type: string |
|
327
|
StoreRegionsListRes: |
|
328
|
type: object |
|
329
|
properties: |
|
330
|
foo: |
|
331
|
type: string |
|
332
|
callbacks: |
|
333
|
fooCallback: |
|
334
|
get: |
|
335
|
description: foo callback |
|
336
|
examples: |
|
337
|
fooExample: |
|
338
|
description: foo example |
|
339
|
headers: |
|
340
|
fooHeader: |
|
341
|
description: foo header |
|
342
|
links: |
|
343
|
fooLink: |
|
344
|
description: foo link |
|
345
|
operationRef: GetFoobarTests |
|
346
|
parameters: |
|
347
|
fooParameter: |
|
348
|
description: foo parameter |
|
349
|
name: foobar |
|
350
|
in: path |
|
351
|
required: true |
|
352
|
schema: |
|
353
|
type: string |
|
354
|
requestBodies: |
|
355
|
fooRequestBody: |
|
356
|
description: foo requestBody |
|
357
|
content: |
|
358
|
"application/octet-stream": { } |
|
359
|
responses: |
|
360
|
fooResponse: |
|
361
|
description: foo response |
|
362
|
securitySchemes: |
|
363
|
fooSecurity: |
|
364
|
description: foo security |
|
365
|
type: apiKey |
|
366
|
name: foo-api-key |
|
367
|
in: header |
|
368
|
` |
|
369
|
const targetDir = path.resolve(tmpDir, uid()) |
|
370
|
const filePath = path.resolve(targetDir, "custom.oas.base.yaml") |
|
371
|
await fs.mkdir(targetDir, { recursive: true }) |
|
372
|
await fs.writeFile(filePath, fileContent, "utf8") |
|
373
|
|
|
374
|
const outDir = path.resolve(tmpDir, uid()) |
|
375
|
await runCLI("oas", [ |
|
376
|
"--type", |
|
377
|
"store", |
|
378
|
"--out-dir", |
|
379
|
outDir, |
|
380
|
"--base", |
|
381
|
filePath, |
|
382
|
"--local", |
|
383
|
]) |
|
384
|
const generatedFilePath = path.resolve(outDir, "store.oas.json") |
|
385
|
oas = (await readJson(generatedFilePath)) as OpenAPIObject |
|
386
|
}) |
|
387
|
|
|
388
|
it("should add new path to existing paths", async () => { |
|
389
|
const routes = Object.keys(oas.paths) |
|
390
|
expect(routes.includes("/store/products")).toBeTruthy() |
|
391
|
expect(routes.includes("/foobar/tests")).toBeTruthy() |
|
392
|
}) |
|
393
|
|
|
394
|
it("should overwrite existing path", async () => { |
|
395
|
expect(oas.paths["/store/regions"]["get"].operationId).toBe( |
|
396
|
"OverwrittenOperation" |
|
397
|
) |
|
398
|
}) |
|
399
|
|
|
400
|
it("should add new schema to existing schemas", async () => { |
|
401
|
const schemas = Object.keys(oas.components?.schemas ?? {}) |
|
402
|
expect(schemas.includes("StoreProductsListRes")).toBeTruthy() |
|
403
|
expect(schemas.includes("FoobarTestSchema")).toBeTruthy() |
|
404
|
}) |
|
405
|
|
|
406
|
it("should overwrite existing schema", async () => { |
|
407
|
const schema = oas.components?.schemas?.StoreRegionsListRes as |
|
408
|
| SchemaObject |
|
409
|
| undefined |
|
410
|
expect(schema?.properties?.foo).toBeDefined() |
|
411
|
}) |
|
412
|
|
|
413
|
it("should replace base properties", async () => { |
|
414
|
expect(oas.openapi).toBe("3.1.0") |
|
415
|
expect(oas.info).toEqual({ version: "1.0.1", title: "Custom API" }) |
|
416
|
expect(oas.servers).toEqual([{ url: "https://foobar.com" }]) |
|
417
|
expect(oas.security).toEqual([{ api_key: [] }]) |
|
418
|
expect(oas.externalDocs).toEqual({ url: "https://docs.com" }) |
|
419
|
expect(oas.webhooks).toEqual({ |
|
420
|
"foo-hook": { get: { responses: { "200": { description: "OK" } } } }, |
|
421
|
}) |
|
422
|
}) |
|
423
|
|
|
424
|
it("should add new tag", async () => { |
|
425
|
expect(oas.tags).toEqual( |
|
426
|
expect.arrayContaining([ |
|
427
|
expect.objectContaining({ |
|
428
|
name: "FoobarTag", |
|
429
|
}), |
|
430
|
]) |
|
431
|
) |
|
432
|
}) |
|
433
|
|
|
434
|
it("should overwrite existing tag", async () => { |
|
435
|
expect(oas.tags).toEqual( |
|
436
|
expect.arrayContaining([ |
|
437
|
expect.objectContaining({ |
|
438
|
name: "Products", |
|
439
|
description: "Overwritten tag", |
|
440
|
}), |
|
441
|
]) |
|
442
|
) |
|
443
|
}) |
|
444
|
|
|
445
|
it("should add new components", async () => { |
|
446
|
const components = oas.components ?? {} |
|
447
|
expect( |
|
448
|
Object.keys(components.callbacks ?? {}).includes("fooCallback") |
|
449
|
).toBeTruthy() |
|
450
|
expect( |
|
451
|
Object.keys(components.examples ?? {}).includes("fooExample") |
|
452
|
).toBeTruthy() |
|
453
|
expect( |
|
454
|
Object.keys(components.headers ?? {}).includes("fooHeader") |
|
455
|
).toBeTruthy() |
|
456
|
expect( |
|
457
|
Object.keys(components.links ?? {}).includes("fooLink") |
|
458
|
).toBeTruthy() |
|
459
|
expect( |
|
460
|
Object.keys(components.parameters ?? {}).includes("fooParameter") |
|
461
|
).toBeTruthy() |
|
462
|
expect( |
|
463
|
Object.keys(components.requestBodies ?? {}).includes("fooRequestBody") |
|
464
|
).toBeTruthy() |
|
465
|
expect( |
|
466
|
Object.keys(components.responses ?? {}).includes("fooResponse") |
|
467
|
).toBeTruthy() |
|
468
|
expect( |
|
469
|
Object.keys(components.securitySchemes ?? {}).includes("fooSecurity") |
|
470
|
).toBeTruthy() |
|
471
|
}) |
|
472
|
}) |
|
473
|
|
|
474
|
describe("public OAS", () => { |
|
475
|
let oas: OpenAPIObject |
|
476
|
/** |
|
477
|
* In a CI context, beforeAll might exceed the configured jest timeout. |
|
478
|
* Until we upgrade our jest version, the timeout error will be swallowed |
|
479
|
* and the test will fail in unexpected ways. |
|
480
|
*/ |
|
481
|
beforeAll(async () => { |
|
482
|
const outDir = path.resolve(tmpDir, uid()) |
|
483
|
await runCLI("oas", ["--type", "admin", "--out-dir", outDir]) |
|
484
|
const generatedFilePath = path.resolve(outDir, "admin.oas.json") |
|
485
|
oas = (await readJson(generatedFilePath)) as OpenAPIObject |
|
486
|
}) |
|
487
|
|
|
488
|
it("generates oas with admin routes only", async () => { |
|
489
|
const routes = Object.keys(oas.paths) |
|
490
|
expect(routes.includes("/admin/products")).toBeTruthy() |
|
491
|
expect(routes.includes("/store/products")).toBeFalsy() |
|
492
|
}) |
|
493
|
}) |
|
494
|
|
|
495
|
describe("public OAS with base", () => { |
|
496
|
let oas: OpenAPIObject |
|
497
|
beforeAll(async () => { |
|
498
|
const fileContent = ` |
|
499
|
openapi: 3.1.0 |
|
500
|
info: |
|
501
|
version: 1.0.1 |
|
502
|
title: Custom API |
|
503
|
servers: |
|
504
|
- url: https://foobar.com |
|
505
|
security: |
|
506
|
- api_key: [] |
|
507
|
externalDocs: |
|
508
|
url: https://docs.com |
|
509
|
webhooks: |
|
510
|
"foo-hook": |
|
511
|
get: |
|
512
|
responses: |
|
513
|
"200": |
|
514
|
description: OK |
|
515
|
tags: |
|
516
|
- name: Products |
|
517
|
description: Overwritten tag |
|
518
|
- name: FoobarTag |
|
519
|
description: Foobar tag description |
|
520
|
paths: |
|
521
|
"/foobar/tests": |
|
522
|
get: |
|
523
|
operationId: GetFoobarTests |
|
524
|
responses: |
|
525
|
"200": |
|
526
|
description: OK |
|
527
|
"/store/regions": |
|
528
|
get: |
|
529
|
operationId: OverwrittenOperation |
|
530
|
responses: |
|
531
|
"200": |
|
532
|
description: OK |
|
533
|
components: |
|
534
|
schemas: |
|
535
|
FoobarTestSchema: |
|
536
|
type: object |
|
537
|
properties: |
|
538
|
foo: |
|
539
|
type: string |
|
540
|
StoreRegionsListRes: |
|
541
|
type: object |
|
542
|
properties: |
|
543
|
foo: |
|
544
|
type: string |
|
545
|
callbacks: |
|
546
|
fooCallback: |
|
547
|
get: |
|
548
|
description: foo callback |
|
549
|
examples: |
|
550
|
fooExample: |
|
551
|
description: foo example |
|
552
|
headers: |
|
553
|
fooHeader: |
|
554
|
description: foo header |
|
555
|
links: |
|
556
|
fooLink: |
|
557
|
description: foo link |
|
558
|
operationRef: GetFoobarTests |
|
559
|
parameters: |
|
560
|
fooParameter: |
|
561
|
description: foo parameter |
|
562
|
name: foobar |
|
563
|
in: path |
|
564
|
required: true |
|
565
|
schema: |
|
566
|
type: string |
|
567
|
requestBodies: |
|
568
|
fooRequestBody: |
|
569
|
description: foo requestBody |
|
570
|
content: |
|
571
|
"application/octet-stream": { } |
|
572
|
responses: |
|
573
|
fooResponse: |
|
574
|
description: foo response |
|
575
|
securitySchemes: |
|
576
|
fooSecurity: |
|
577
|
description: foo security |
|
578
|
type: apiKey |
|
579
|
name: foo-api-key |
|
580
|
in: header |
|
581
|
` |
|
582
|
const targetDir = path.resolve(tmpDir, uid()) |
|
583
|
const filePath = path.resolve(targetDir, "custom.oas.base.yaml") |
|
584
|
await fs.mkdir(targetDir, { recursive: true }) |
|
585
|
await fs.writeFile(filePath, fileContent, "utf8") |
|
586
|
|
|
587
|
const outDir = path.resolve(tmpDir, uid()) |
|
588
|
await runCLI("oas", [ |
|
589
|
"--type", |
|
590
|
"store", |
|
591
|
"--out-dir", |
|
592
|
outDir, |
|
593
|
"--base", |
|
594
|
filePath, |
|
595
|
]) |
|
596
|
const generatedFilePath = path.resolve(outDir, "store.oas.json") |
|
597
|
oas = (await readJson(generatedFilePath)) as OpenAPIObject |
|
598
|
}) |
|
599
|
|
|
600
|
it("should add new path to existing paths", async () => { |
|
601
|
const routes = Object.keys(oas.paths) |
|
602
|
expect(routes.includes("/store/products")).toBeTruthy() |
|
603
|
expect(routes.includes("/foobar/tests")).toBeTruthy() |
|
604
|
}) |
|
605
|
|
|
606
|
it("should overwrite existing path", async () => { |
|
607
|
expect(oas.paths["/store/regions"]["get"].operationId).toBe( |
|
608
|
"OverwrittenOperation" |
|
609
|
) |
|
610
|
}) |
|
611
|
|
|
612
|
it("should add new schema to existing schemas", async () => { |
|
613
|
const schemas = Object.keys(oas.components?.schemas ?? {}) |
|
614
|
expect(schemas.includes("StoreProductsListRes")).toBeTruthy() |
|
615
|
expect(schemas.includes("FoobarTestSchema")).toBeTruthy() |
|
616
|
}) |
|
617
|
|
|
618
|
it("should overwrite existing schema", async () => { |
|
619
|
const schema = oas.components?.schemas?.StoreRegionsListRes as |
|
620
|
| SchemaObject |
|
621
|
| undefined |
|
622
|
expect(schema?.properties?.foo).toBeDefined() |
|
623
|
}) |
|
624
|
|
|
625
|
it("should replace base properties", async () => { |
|
626
|
expect(oas.openapi).toBe("3.1.0") |
|
627
|
expect(oas.info).toEqual({ version: "1.0.1", title: "Custom API" }) |
|
628
|
expect(oas.servers).toEqual([{ url: "https://foobar.com" }]) |
|
629
|
expect(oas.security).toEqual([{ api_key: [] }]) |
|
630
|
expect(oas.externalDocs).toEqual({ url: "https://docs.com" }) |
|
631
|
expect(oas.webhooks).toEqual({ |
|
632
|
"foo-hook": { get: { responses: { "200": { description: "OK" } } } }, |
|
633
|
}) |
|
634
|
}) |
|
635
|
|
|
636
|
it("should add new tag", async () => { |
|
637
|
expect(oas.tags).toEqual( |
|
638
|
expect.arrayContaining([ |
|
639
|
expect.objectContaining({ |
|
640
|
name: "FoobarTag", |
|
641
|
}), |
|
642
|
]) |
|
643
|
) |
|
644
|
}) |
|
645
|
|
|
646
|
it("should overwrite existing tag", async () => { |
|
647
|
expect(oas.tags).toEqual( |
|
648
|
expect.arrayContaining([ |
|
649
|
expect.objectContaining({ |
|
650
|
name: "Products", |
|
651
|
description: "Overwritten tag", |
|
652
|
}), |
|
653
|
]) |
|
654
|
) |
|
655
|
}) |
|
656
|
|
|
657
|
it("should add new components", async () => { |
|
658
|
const components = oas.components ?? {} |
|
659
|
expect( |
|
660
|
Object.keys(components.callbacks ?? {}).includes("fooCallback") |
|
661
|
).toBeTruthy() |
|
662
|
expect( |
|
663
|
Object.keys(components.examples ?? {}).includes("fooExample") |
|
664
|
).toBeTruthy() |
|
665
|
expect( |
|
666
|
Object.keys(components.headers ?? {}).includes("fooHeader") |
|
667
|
).toBeTruthy() |
|
668
|
expect( |
|
669
|
Object.keys(components.links ?? {}).includes("fooLink") |
|
670
|
).toBeTruthy() |
|
671
|
expect( |
|
672
|
Object.keys(components.parameters ?? {}).includes("fooParameter") |
|
673
|
).toBeTruthy() |
|
674
|
expect( |
|
675
|
Object.keys(components.requestBodies ?? {}).includes("fooRequestBody") |
|
676
|
).toBeTruthy() |
|
677
|
expect( |
|
678
|
Object.keys(components.responses ?? {}).includes("fooResponse") |
|
679
|
).toBeTruthy() |
|
680
|
expect( |
|
681
|
Object.keys(components.securitySchemes ?? {}).includes("fooSecurity") |
|
682
|
).toBeTruthy() |
|
683
|
}) |
|
684
|
}) |
|
685
|
}) |