|
1
|
import { exists, getTmpDirectory } from "../utils/fs-utils" |
|
2
|
import { writeJson } from "../utils/json-utils" |
|
3
|
import { OpenAPIObject } from "openapi3-ts" |
|
4
|
import path from "path" |
|
5
|
import { v4 as uid } from "uuid" |
|
6
|
import { readYaml, writeYaml } from "../utils/yaml-utils" |
|
7
|
import { mkdir, readdir } from "fs/promises" |
|
8
|
import { |
|
9
|
formatHintRecommendation, |
|
10
|
getCircularPatchRecommendation, |
|
11
|
getCircularReferences, |
|
12
|
} from "../utils/circular-patch-utils" |
|
13
|
import execa from "execa" |
|
14
|
|
|
15
|
const basePath = path.resolve(__dirname, `../../../`) |
|
16
|
|
|
17
|
export const runCLI = async (command: string, options: string[] = []) => { |
|
18
|
const params = ["run", "medusa-oas", command, ...options] |
|
19
|
try { |
|
20
|
const { all: logs } = await execa("yarn", params, { |
|
21
|
cwd: basePath, |
|
22
|
all: true, |
|
23
|
}) |
|
24
|
} catch (err) { |
|
25
|
throw new Error(err.message + err.all) |
|
26
|
} |
|
27
|
} |
|
28
|
|
|
29
|
export const getBaseOpenApi = (): OpenAPIObject => { |
|
30
|
return { |
|
31
|
openapi: "3.0.0", |
|
32
|
info: { |
|
33
|
title: "Test", |
|
34
|
version: "1.0.0", |
|
35
|
}, |
|
36
|
paths: {}, |
|
37
|
components: {}, |
|
38
|
} |
|
39
|
} |
|
40
|
|
|
41
|
describe("command docs", () => { |
|
42
|
let tmpDir: string |
|
43
|
let openApi: OpenAPIObject |
|
44
|
|
|
45
|
beforeAll(async () => { |
|
46
|
tmpDir = await getTmpDirectory() |
|
47
|
}) |
|
48
|
|
|
49
|
beforeEach(async () => { |
|
50
|
openApi = getBaseOpenApi() |
|
51
|
}) |
|
52
|
|
|
53
|
describe("basic usage", () => { |
|
54
|
let srcFile: string |
|
55
|
|
|
56
|
beforeAll(async () => { |
|
57
|
openApi = getBaseOpenApi() |
|
58
|
openApi.components = { |
|
59
|
schemas: { |
|
60
|
Order: { |
|
61
|
type: "object", |
|
62
|
}, |
|
63
|
}, |
|
64
|
} |
|
65
|
const outDir = path.resolve(tmpDir, uid()) |
|
66
|
await mkdir(outDir, { recursive: true }) |
|
67
|
srcFile = path.resolve(outDir, "store.oas.json") |
|
68
|
await writeJson(srcFile, openApi) |
|
69
|
}) |
|
70
|
|
|
71
|
it("should generate docs", async () => { |
|
72
|
const outDir = path.resolve(tmpDir, uid()) |
|
73
|
await runCLI("docs", ["--src-file", srcFile, "--out-dir", outDir]) |
|
74
|
const generatedFilePath = path.resolve(outDir, "openapi.yaml") |
|
75
|
const oas = (await readYaml(generatedFilePath)) as OpenAPIObject |
|
76
|
const files = await readdir(outDir) |
|
77
|
expect(oas.components?.schemas?.Order).toBeDefined() |
|
78
|
expect(files.length).toBe(1) |
|
79
|
}) |
|
80
|
|
|
81
|
it("should clean output directory", async () => { |
|
82
|
const outDir = path.resolve(tmpDir, uid()) |
|
83
|
await mkdir(outDir, { recursive: true }) |
|
84
|
await writeJson(path.resolve(outDir, "test.json"), { foo: "bar" }) |
|
85
|
await runCLI("docs", [ |
|
86
|
"--src-file", |
|
87
|
srcFile, |
|
88
|
"--out-dir", |
|
89
|
outDir, |
|
90
|
"--clean", |
|
91
|
]) |
|
92
|
const files = await readdir(outDir) |
|
93
|
expect(files.includes("test.json")).toBeFalsy() |
|
94
|
expect(files.length).toBe(1) |
|
95
|
}) |
|
96
|
|
|
97
|
it("should not output any files", async () => { |
|
98
|
const outDir = path.resolve(tmpDir, uid()) |
|
99
|
await runCLI("docs", [ |
|
100
|
"--src-file", |
|
101
|
srcFile, |
|
102
|
"--out-dir", |
|
103
|
outDir, |
|
104
|
"--dry-run", |
|
105
|
]) |
|
106
|
const outDirExists = await exists(outDir) |
|
107
|
expect(outDirExists).toBeFalsy() |
|
108
|
}) |
|
109
|
|
|
110
|
it("should split output into multiple files and directories", async () => { |
|
111
|
const outDir = path.resolve(tmpDir, uid()) |
|
112
|
/** |
|
113
|
* For split to output files, we need to not be in test mode |
|
114
|
* See https://github.com/Redocly/redocly-cli/blob/v1.0.0-beta.125/packages/cli/src/utils.ts#L206-L215 |
|
115
|
*/ |
|
116
|
const previousEnv = process.env.NODE_ENV |
|
117
|
process.env.NODE_ENV = "development" |
|
118
|
await runCLI("docs", [ |
|
119
|
"--src-file", |
|
120
|
srcFile, |
|
121
|
"--out-dir", |
|
122
|
outDir, |
|
123
|
"--split", |
|
124
|
"--clean", |
|
125
|
]) |
|
126
|
process.env.NODE_ENV = previousEnv |
|
127
|
const oasFileExists = await exists(path.resolve(outDir, "openapi.yaml")) |
|
128
|
const outFileExists = await exists( |
|
129
|
path.resolve(outDir, "components/schemas/Order.yaml") |
|
130
|
) |
|
131
|
expect(oasFileExists).toBeTruthy() |
|
132
|
expect(outFileExists).toBeTruthy() |
|
133
|
}) |
|
134
|
|
|
135
|
it("should generate static HTML docs", async () => { |
|
136
|
const outDir = path.resolve(tmpDir, uid()) |
|
137
|
await runCLI("docs", [ |
|
138
|
"--src-file", |
|
139
|
srcFile, |
|
140
|
"--out-dir", |
|
141
|
outDir, |
|
142
|
"--html", |
|
143
|
]) |
|
144
|
const generatedFilePath = path.resolve(outDir, "index.html") |
|
145
|
const htmlFileExists = await exists(generatedFilePath) |
|
146
|
expect(htmlFileExists).toBeTruthy() |
|
147
|
}) |
|
148
|
}) |
|
149
|
|
|
150
|
describe("--config", () => { |
|
151
|
let srcFile: string |
|
152
|
let configFile: string |
|
153
|
let configYamlFile: string |
|
154
|
|
|
155
|
beforeAll(async () => { |
|
156
|
openApi = getBaseOpenApi() |
|
157
|
openApi.components = { |
|
158
|
schemas: { |
|
159
|
Customer: { |
|
160
|
type: "object", |
|
161
|
properties: { |
|
162
|
address: { |
|
163
|
$ref: "#/components/schemas/Address", |
|
164
|
}, |
|
165
|
}, |
|
166
|
}, |
|
167
|
TestOrder: { |
|
168
|
type: "object", |
|
169
|
properties: { |
|
170
|
address: { |
|
171
|
$ref: "#/components/schemas/Address", |
|
172
|
}, |
|
173
|
}, |
|
174
|
}, |
|
175
|
Address: { |
|
176
|
type: "object", |
|
177
|
properties: { |
|
178
|
/** |
|
179
|
* We expect this circular reference to already be handled by |
|
180
|
* our CLI's default redocly-config.yaml |
|
181
|
*/ |
|
182
|
customer: { |
|
183
|
$ref: "#/components/schemas/Customer", |
|
184
|
}, |
|
185
|
/** |
|
186
|
* We expect this circular reference to not be handled. |
|
187
|
*/ |
|
188
|
test_order: { |
|
189
|
$ref: "#/components/schemas/TestOrder", |
|
190
|
}, |
|
191
|
}, |
|
192
|
}, |
|
193
|
}, |
|
194
|
} |
|
195
|
const config = { |
|
196
|
decorators: { |
|
197
|
"medusa/circular-patch": { |
|
198
|
schemas: { |
|
199
|
Address: ["TestOrder"], |
|
200
|
}, |
|
201
|
}, |
|
202
|
}, |
|
203
|
} |
|
204
|
const outDir = path.resolve(tmpDir, uid()) |
|
205
|
await mkdir(outDir, { recursive: true }) |
|
206
|
srcFile = path.resolve(outDir, "store.oas.json") |
|
207
|
await writeJson(srcFile, openApi) |
|
208
|
configFile = path.resolve(outDir, "redocly-config.json") |
|
209
|
await writeJson(configFile, config) |
|
210
|
configYamlFile = path.resolve(outDir, "redocly-config.yaml") |
|
211
|
await writeYaml(configYamlFile, JSON.stringify(config)) |
|
212
|
}) |
|
213
|
|
|
214
|
it("should fail with unhandled circular reference", async () => { |
|
215
|
const outDir = path.resolve(tmpDir, uid()) |
|
216
|
await expect( |
|
217
|
runCLI("docs", [ |
|
218
|
"--src-file", |
|
219
|
srcFile, |
|
220
|
"--out-dir", |
|
221
|
outDir, |
|
222
|
"--dry-run", |
|
223
|
]) |
|
224
|
).rejects.toThrow("Unhandled circular references") |
|
225
|
}) |
|
226
|
|
|
227
|
it("should succeed with patched circular reference", async () => { |
|
228
|
const outDir = path.resolve(tmpDir, uid()) |
|
229
|
await expect( |
|
230
|
runCLI("docs", [ |
|
231
|
"--src-file", |
|
232
|
srcFile, |
|
233
|
"--out-dir", |
|
234
|
outDir, |
|
235
|
"--config", |
|
236
|
configFile, |
|
237
|
"--dry-run", |
|
238
|
]) |
|
239
|
).resolves.not.toThrow() |
|
240
|
}) |
|
241
|
|
|
242
|
it("should succeed when config is of type yaml", async () => { |
|
243
|
const outDir = path.resolve(tmpDir, uid()) |
|
244
|
await expect( |
|
245
|
runCLI("docs", [ |
|
246
|
"--src-file", |
|
247
|
srcFile, |
|
248
|
"--out-dir", |
|
249
|
outDir, |
|
250
|
"--config", |
|
251
|
configYamlFile, |
|
252
|
"--dry-run", |
|
253
|
]) |
|
254
|
).resolves.not.toThrow() |
|
255
|
}) |
|
256
|
|
|
257
|
it("should fail when config is not a file", async () => { |
|
258
|
const outDir = path.resolve(tmpDir, uid()) |
|
259
|
await expect( |
|
260
|
runCLI("docs", [ |
|
261
|
"--src-file", |
|
262
|
srcFile, |
|
263
|
"--out-dir", |
|
264
|
outDir, |
|
265
|
"--dry-run", |
|
266
|
"--config", |
|
267
|
outDir, |
|
268
|
]) |
|
269
|
).rejects.toThrow("--config must be a file") |
|
270
|
}) |
|
271
|
|
|
272
|
it("should fail when config is not of supported file type", async () => { |
|
273
|
const outDir = path.resolve(tmpDir, uid()) |
|
274
|
await mkdir(outDir, { recursive: true }) |
|
275
|
const tmpFile = path.resolve(outDir, "tmp.txt") |
|
276
|
await writeJson(tmpFile, { foo: "bar" }) |
|
277
|
await expect( |
|
278
|
runCLI("docs", [ |
|
279
|
"--src-file", |
|
280
|
srcFile, |
|
281
|
"--out-dir", |
|
282
|
outDir, |
|
283
|
"--dry-run", |
|
284
|
"--config", |
|
285
|
tmpFile, |
|
286
|
]) |
|
287
|
).rejects.toThrow("--config file must be of type .json or .yaml") |
|
288
|
}) |
|
289
|
}) |
|
290
|
|
|
291
|
describe("circular references", () => { |
|
292
|
let srcFile: string |
|
293
|
|
|
294
|
beforeAll(async () => { |
|
295
|
openApi = getBaseOpenApi() |
|
296
|
openApi.components = { |
|
297
|
schemas: { |
|
298
|
Customer: { |
|
299
|
type: "object", |
|
300
|
properties: { |
|
301
|
address: { |
|
302
|
$ref: "#/components/schemas/Address", |
|
303
|
}, |
|
304
|
}, |
|
305
|
}, |
|
306
|
TestOrder: { |
|
307
|
type: "object", |
|
308
|
properties: { |
|
309
|
address: { |
|
310
|
$ref: "#/components/schemas/Address", |
|
311
|
}, |
|
312
|
}, |
|
313
|
}, |
|
314
|
Address: { |
|
315
|
type: "object", |
|
316
|
properties: { |
|
317
|
customer: { |
|
318
|
$ref: "#/components/schemas/Customer", |
|
319
|
}, |
|
320
|
test_order: { |
|
321
|
$ref: "#/components/schemas/TestOrder", |
|
322
|
}, |
|
323
|
}, |
|
324
|
}, |
|
325
|
}, |
|
326
|
} |
|
327
|
const outDir = path.resolve(tmpDir, uid()) |
|
328
|
await mkdir(outDir, { recursive: true }) |
|
329
|
srcFile = path.resolve(outDir, "store.oas.json") |
|
330
|
await writeJson(srcFile, openApi) |
|
331
|
}) |
|
332
|
|
|
333
|
it("should find circular references", async () => { |
|
334
|
const { circularRefs, oas } = await getCircularReferences(srcFile) |
|
335
|
expect(circularRefs.length).toBe(2) |
|
336
|
expect(circularRefs).toEqual( |
|
337
|
expect.arrayContaining([ |
|
338
|
"#/components/schemas/Address/properties/customer", |
|
339
|
"#/components/schemas/TestOrder/properties/address", |
|
340
|
]) |
|
341
|
) |
|
342
|
}) |
|
343
|
|
|
344
|
it("should recommend which schemas to patch to resolve circular references", async () => { |
|
345
|
/** |
|
346
|
* The recommendation is heavily influenced but the dereference operation |
|
347
|
* from @readme/json-schema-ref-parser. It's not an exact science and the |
|
348
|
* results may vary between versions. |
|
349
|
*/ |
|
350
|
const { circularRefs, oas } = await getCircularReferences(srcFile) |
|
351
|
const recommendation = getCircularPatchRecommendation(circularRefs, oas) |
|
352
|
expect(recommendation).toEqual( |
|
353
|
expect.objectContaining({ |
|
354
|
Address: expect.arrayContaining(["Customer"]), |
|
355
|
TestOrder: expect.arrayContaining(["Address"]), |
|
356
|
}) |
|
357
|
) |
|
358
|
}) |
|
359
|
|
|
360
|
it("should format hint", async () => { |
|
361
|
const { circularRefs, oas } = await getCircularReferences(srcFile) |
|
362
|
const recommendation = getCircularPatchRecommendation(circularRefs, oas) |
|
363
|
const hint = formatHintRecommendation(recommendation) |
|
364
|
expect(hint).toEqual( |
|
365
|
expect.stringContaining(`decorators: |
|
366
|
medusa/circular-patch: |
|
367
|
schemas: |
|
368
|
Address: |
|
369
|
- Customer |
|
370
|
TestOrder: |
|
371
|
- Address |
|
372
|
`) |
|
373
|
) |
|
374
|
}) |
|
375
|
}) |
|
376
|
}) |