|
1
|
import type { OpenAPI } from "types" |
|
2
|
import mergeAllOfTypes from "@/utils/merge-all-of-types" |
|
3
|
import checkRequired from "@/utils/check-required" |
|
4
|
import { resolveApiRefDocUrl } from "@/utils/resolve-doc-url" |
|
5
|
import { MarkdownContext, absolutizeLink, capitalize, oneLine } from "./shared" |
|
6
|
|
|
7
|
const INDENT = " " |
|
8
|
|
|
9
|
const isObjectSchema = (schema?: OpenAPI.SchemaObject): boolean => |
|
10
|
!!schema && |
|
11
|
(schema.type === "object" || |
|
12
|
!!schema.properties || |
|
13
|
!!schema.allOf || |
|
14
|
!!schema.anyOf || |
|
15
|
!!schema.oneOf) |
|
16
|
|
|
17
|
/** Mirrors `Parameters/Name/formatArrayDescription`. */ |
|
18
|
const formatArrayDescription = (items?: OpenAPI.SchemaObject): string => { |
|
19
|
if (!items) { |
|
20
|
return "Array" |
|
21
|
} |
|
22
|
const type = |
|
23
|
items.type === "object" |
|
24
|
? `objects ${items.title ? `(${items.title})` : ""}`.trim() |
|
25
|
: `${items.type || "object"}s` |
|
26
|
return `Array of ${type}` |
|
27
|
} |
|
28
|
|
|
29
|
/** Mirrors `Parameters/Name/formatUnionDescription` (distinct member types). */ |
|
30
|
const formatUnionDescription = (arr?: OpenAPI.SchemaObject[]): string => { |
|
31
|
const types = [...new Set(arr?.map((type) => type.type || "object"))] |
|
32
|
return types.join(" or ") |
|
33
|
} |
|
34
|
|
|
35
|
/** Human-readable type label, mirroring the `Name` component's logic. */ |
|
36
|
const typeDescription = (schema: OpenAPI.SchemaObject): string => { |
|
37
|
const nullable = schema.nullable ? " or null" : "" |
|
38
|
switch (true) { |
|
39
|
case schema.type === "object": |
|
40
|
return `object${schema.title ? ` (${schema.title})` : ""}${nullable}` |
|
41
|
case schema.type === "array": |
|
42
|
return `${formatArrayDescription( |
|
43
|
(schema as OpenAPI.ArraySchemaObject).items |
|
44
|
)}${nullable}` |
|
45
|
case schema.anyOf !== undefined: |
|
46
|
case schema.allOf !== undefined: |
|
47
|
return `${formatUnionDescription(schema.allOf || schema.anyOf)}${nullable}` |
|
48
|
case schema.oneOf !== undefined: |
|
49
|
return `${schema |
|
50
|
.oneOf!.map((item) => { |
|
51
|
if (item.type === "array") { |
|
52
|
return `array${item.items?.type ? ` of ${item.items.type}s` : ""}` |
|
53
|
} |
|
54
|
return item.title || item.type || "object" |
|
55
|
}) |
|
56
|
.join(" or ")}${nullable}` |
|
57
|
default: |
|
58
|
return `${!schema.type ? "any" : schema.type}${ |
|
59
|
schema.format ? ` <${schema.format}>` : "" |
|
60
|
}${nullable}` |
|
61
|
} |
|
62
|
} |
|
63
|
|
|
64
|
/** The inline metadata suffix (description, default, enum, example, guide). */ |
|
65
|
const descriptionSuffix = ( |
|
66
|
schema: OpenAPI.SchemaObject, |
|
67
|
ctx: MarkdownContext |
|
68
|
): string => { |
|
69
|
const segments: string[] = [] |
|
70
|
if (schema.description) { |
|
71
|
segments.push(oneLine(capitalize(schema.description))) |
|
72
|
} |
|
73
|
if (schema.default !== undefined) { |
|
74
|
segments.push(`Default: \`${JSON.stringify(schema.default)}\``) |
|
75
|
} |
|
76
|
if (schema.enum) { |
|
77
|
segments.push( |
|
78
|
`Enum: ${schema.enum |
|
79
|
.map((value) => `\`${JSON.stringify(value)}\``) |
|
80
|
.join(", ")}` |
|
81
|
) |
|
82
|
} |
|
83
|
if (schema.example !== undefined) { |
|
84
|
segments.push(`Example: \`${JSON.stringify(schema.example)}\``) |
|
85
|
} |
|
86
|
if (schema.externalDocs) { |
|
87
|
const href = absolutizeLink( |
|
88
|
resolveApiRefDocUrl(schema.externalDocs.url, ctx.area), |
|
89
|
ctx |
|
90
|
) |
|
91
|
segments.push( |
|
92
|
`Related guide: [${schema.externalDocs.description || "Read More"}](${href})` |
|
93
|
) |
|
94
|
} |
|
95
|
return segments.join(" ") |
|
96
|
} |
|
97
|
|
|
98
|
/** Order properties so required fields come first (mirrors `Types/Object`). */ |
|
99
|
const sortedPropertyNames = ( |
|
100
|
schema: OpenAPI.SchemaObject, |
|
101
|
properties: Record<string, OpenAPI.SchemaObject> |
|
102
|
): string[] => |
|
103
|
Object.keys(properties).sort((a, b) => { |
|
104
|
const aRequired = checkRequired(schema, a) ? 1 : 0 |
|
105
|
const bRequired = checkRequired(schema, b) ? 1 : 0 |
|
106
|
return bRequired - aRequired |
|
107
|
}) |
|
108
|
|
|
109
|
/** Render the nested children of a schema (properties/items/oneOf members). */ |
|
110
|
const renderChildren = ( |
|
111
|
schema: OpenAPI.SchemaObject, |
|
112
|
level: number, |
|
113
|
ctx: MarkdownContext |
|
114
|
): string => { |
|
115
|
let resolved = schema |
|
116
|
|
|
117
|
if (resolved.allOf) { |
|
118
|
resolved = mergeAllOfTypes(resolved) |
|
119
|
} else if (resolved.anyOf) { |
|
120
|
const objectMember = resolved.anyOf.find(isObjectSchema) |
|
121
|
if (!objectMember) { |
|
122
|
return "" |
|
123
|
} |
|
124
|
resolved = objectMember |
|
125
|
} |
|
126
|
|
|
127
|
if (resolved.oneOf) { |
|
128
|
const indent = INDENT.repeat(level) |
|
129
|
return resolved.oneOf |
|
130
|
.map((member, index) => { |
|
131
|
// Render each option with its type and description (the page shows |
|
132
|
// these per tab); recurse for object members. |
|
133
|
const suffix = descriptionSuffix(member, ctx) |
|
134
|
const line = `${indent}- **Option ${index + 1}**: ${typeDescription( |
|
135
|
member |
|
136
|
)}${suffix ? ` — ${suffix}` : ""}` |
|
137
|
const children = renderChildren(member, level + 1, ctx) |
|
138
|
return children ? `${line}\n${children}` : line |
|
139
|
}) |
|
140
|
.join("\n") |
|
141
|
} |
|
142
|
|
|
143
|
if ( |
|
144
|
resolved.type === "array" && |
|
145
|
(resolved as OpenAPI.ArraySchemaObject).items |
|
146
|
) { |
|
147
|
return renderChildren( |
|
148
|
(resolved as OpenAPI.ArraySchemaObject).items, |
|
149
|
level, |
|
150
|
ctx |
|
151
|
) |
|
152
|
} |
|
153
|
|
|
154
|
const additional = |
|
155
|
resolved.additionalProperties && |
|
156
|
typeof resolved.additionalProperties === "object" |
|
157
|
? (resolved.additionalProperties as OpenAPI.SchemaObject).properties |
|
158
|
: undefined |
|
159
|
const properties = resolved.properties || additional |
|
160
|
if (!properties) { |
|
161
|
return "" |
|
162
|
} |
|
163
|
|
|
164
|
return sortedPropertyNames(resolved, properties) |
|
165
|
.map((name) => |
|
166
|
renderProperty( |
|
167
|
properties[name], |
|
168
|
name, |
|
169
|
!!checkRequired(resolved, name), |
|
170
|
level, |
|
171
|
ctx |
|
172
|
) |
|
173
|
) |
|
174
|
.join("\n") |
|
175
|
} |
|
176
|
|
|
177
|
/** Render one named property line plus its nested children. */ |
|
178
|
const renderProperty = ( |
|
179
|
schema: OpenAPI.SchemaObject, |
|
180
|
name: string, |
|
181
|
required: boolean, |
|
182
|
level: number, |
|
183
|
ctx: MarkdownContext |
|
184
|
): string => { |
|
185
|
const indent = INDENT.repeat(level) |
|
186
|
let line = `${indent}- \`${name}\`: ${typeDescription(schema)}` |
|
187
|
|
|
188
|
const flags: string[] = [] |
|
189
|
if (!required) { |
|
190
|
flags.push("optional") |
|
191
|
} |
|
192
|
if (schema.deprecated) { |
|
193
|
flags.push("deprecated") |
|
194
|
} |
|
195
|
if (schema["x-expandable"]) { |
|
196
|
flags.push("expandable") |
|
197
|
} |
|
198
|
if (flags.length) { |
|
199
|
line += ` (${flags.join(", ")})` |
|
200
|
} |
|
201
|
|
|
202
|
const suffix = descriptionSuffix(schema, ctx) |
|
203
|
if (suffix) { |
|
204
|
line += ` — ${suffix}` |
|
205
|
} |
|
206
|
|
|
207
|
const children = renderChildren(schema, level + 1, ctx) |
|
208
|
return children ? `${line}\n${children}` : line |
|
209
|
} |
|
210
|
|
|
211
|
/** |
|
212
|
* Converts an OpenAPI schema to a nested Markdown bullet list, reproducing the |
|
213
|
* recursive `Tags/Operation/Parameters` renderer. Used for parameters, request |
|
214
|
* bodies, responses, event payloads, and the tag's associated schema. |
|
215
|
*/ |
|
216
|
export default function schemaToMarkdown( |
|
217
|
schema: OpenAPI.SchemaObject, |
|
218
|
ctx: MarkdownContext |
|
219
|
): string { |
|
220
|
const body = renderChildren(schema, 0, ctx) |
|
221
|
if (body) { |
|
222
|
return body |
|
223
|
} |
|
224
|
|
|
225
|
// Top-level primitive / empty object: emit a single descriptive line. |
|
226
|
const suffix = descriptionSuffix(schema, ctx) |
|
227
|
const base = `- ${typeDescription(schema)}` |
|
228
|
return suffix ? `${base} — ${suffix}` : base |
|
229
|
} |