|
1
|
import { describe, it, expect } from "vitest" |
|
2
|
import type { OpenAPI } from "types" |
|
3
|
import schemaToMarkdown from "../schema-to-markdown" |
|
4
|
import { MarkdownContext } from "../shared" |
|
5
|
|
|
6
|
const ctx: MarkdownContext = { |
|
7
|
area: "store", |
|
8
|
baseUrl: "https://docs.medusajs.com", |
|
9
|
basePath: "/api", |
|
10
|
} |
|
11
|
|
|
12
|
describe("schemaToMarkdown", () => { |
|
13
|
it("renders object properties with required-first ordering and flags", () => { |
|
14
|
const schema = { |
|
15
|
type: "object", |
|
16
|
required: ["id"], |
|
17
|
properties: { |
|
18
|
name: { type: "string", description: "the name" }, |
|
19
|
id: { type: "string", description: "the id" }, |
|
20
|
}, |
|
21
|
} as unknown as OpenAPI.SchemaObject |
|
22
|
|
|
23
|
const md = schemaToMarkdown(schema, ctx) |
|
24
|
const lines = md.split("\n") |
|
25
|
// required `id` comes before optional `name` |
|
26
|
expect(lines[0]).toContain("`id`: string") |
|
27
|
expect(lines[0]).not.toContain("optional") |
|
28
|
expect(lines[1]).toContain("`name`: string") |
|
29
|
expect(lines[1]).toContain("(optional)") |
|
30
|
expect(md).toContain("— The id") |
|
31
|
}) |
|
32
|
|
|
33
|
it("renders enum, default, and example metadata", () => { |
|
34
|
const schema = { |
|
35
|
type: "object", |
|
36
|
required: ["status"], |
|
37
|
properties: { |
|
38
|
status: { |
|
39
|
type: "string", |
|
40
|
enum: ["pending", "completed"], |
|
41
|
default: "pending", |
|
42
|
example: "completed", |
|
43
|
}, |
|
44
|
}, |
|
45
|
} as unknown as OpenAPI.SchemaObject |
|
46
|
|
|
47
|
const md = schemaToMarkdown(schema, ctx) |
|
48
|
expect(md).toContain('Default: `"pending"`') |
|
49
|
expect(md).toContain('Enum: `"pending"`, `"completed"`') |
|
50
|
expect(md).toContain('Example: `"completed"`') |
|
51
|
}) |
|
52
|
|
|
53
|
it("renders arrays of objects and nests item fields", () => { |
|
54
|
const schema = { |
|
55
|
type: "object", |
|
56
|
required: ["items"], |
|
57
|
properties: { |
|
58
|
items: { |
|
59
|
type: "array", |
|
60
|
items: { |
|
61
|
type: "object", |
|
62
|
required: ["quantity"], |
|
63
|
properties: { |
|
64
|
quantity: { type: "number", description: "how many" }, |
|
65
|
}, |
|
66
|
}, |
|
67
|
}, |
|
68
|
}, |
|
69
|
} as unknown as OpenAPI.SchemaObject |
|
70
|
|
|
71
|
const md = schemaToMarkdown(schema, ctx) |
|
72
|
expect(md).toContain("`items`: Array of objects") |
|
73
|
// nested field is indented under the array property |
|
74
|
expect(md).toMatch(/\n {2}- `quantity`: number/) |
|
75
|
}) |
|
76
|
|
|
77
|
it("merges allOf object members", () => { |
|
78
|
const schema = { |
|
79
|
type: "object", |
|
80
|
required: ["combined"], |
|
81
|
properties: { |
|
82
|
combined: { |
|
83
|
allOf: [ |
|
84
|
{ type: "object", properties: { a: { type: "string" } } }, |
|
85
|
{ |
|
86
|
type: "object", |
|
87
|
required: ["b"], |
|
88
|
properties: { b: { type: "number" } }, |
|
89
|
}, |
|
90
|
], |
|
91
|
}, |
|
92
|
}, |
|
93
|
} as unknown as OpenAPI.SchemaObject |
|
94
|
|
|
95
|
const md = schemaToMarkdown(schema, ctx) |
|
96
|
expect(md).toMatch(/\n {2}- `a`: string/) |
|
97
|
expect(md).toMatch(/\n {2}- `b`: number/) |
|
98
|
}) |
|
99
|
|
|
100
|
it("renders oneOf members as options with type and description", () => { |
|
101
|
const schema = { |
|
102
|
type: "object", |
|
103
|
required: ["value"], |
|
104
|
properties: { |
|
105
|
value: { |
|
106
|
oneOf: [ |
|
107
|
{ type: "string", title: "AsString", description: "As a string." }, |
|
108
|
{ type: "number", title: "AsNumber" }, |
|
109
|
], |
|
110
|
}, |
|
111
|
}, |
|
112
|
} as unknown as OpenAPI.SchemaObject |
|
113
|
|
|
114
|
const md = schemaToMarkdown(schema, ctx) |
|
115
|
expect(md).toContain("`value`: AsString or AsNumber") |
|
116
|
// each option shows its type and (if present) description — not dropped |
|
117
|
expect(md).toContain("**Option 1**: string — As a string.") |
|
118
|
expect(md).toContain("**Option 2**: number") |
|
119
|
}) |
|
120
|
|
|
121
|
it("absolutizes internal externalDocs links with base url + base path", () => { |
|
122
|
const schema = { |
|
123
|
type: "object", |
|
124
|
required: ["field"], |
|
125
|
properties: { |
|
126
|
field: { |
|
127
|
type: "string", |
|
128
|
externalDocs: { |
|
129
|
url: "/store/foo", |
|
130
|
description: "Read More", |
|
131
|
}, |
|
132
|
}, |
|
133
|
}, |
|
134
|
} as unknown as OpenAPI.SchemaObject |
|
135
|
|
|
136
|
const md = schemaToMarkdown(schema, ctx) |
|
137
|
expect(md).toContain( |
|
138
|
"Related guide: [Read More](https://docs.medusajs.com/api/store/foo)" |
|
139
|
) |
|
140
|
}) |
|
141
|
}) |