@typia/utils 12.0.0-dev.20260316 → 12.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +85 -85
- package/package.json +2 -2
- package/src/converters/LlmSchemaConverter.ts +617 -617
- package/src/converters/OpenApiConverter.ts +375 -375
- package/src/converters/internal/OpenApiV3Downgrader.ts +381 -381
- package/src/converters/internal/OpenApiV3Upgrader.ts +494 -494
- package/src/converters/internal/OpenApiV3_1Downgrader.ts +318 -318
- package/src/converters/internal/OpenApiV3_1Upgrader.ts +710 -710
- package/src/converters/internal/OpenApiV3_2Upgrader.ts +342 -342
- package/src/converters/internal/SwaggerV2Downgrader.ts +450 -450
- package/src/converters/internal/SwaggerV2Upgrader.ts +547 -547
- package/src/http/HttpError.ts +114 -114
- package/src/http/HttpLlm.ts +169 -169
- package/src/http/HttpMigration.ts +94 -94
- package/src/http/internal/HttpLlmApplicationComposer.ts +360 -360
- package/src/http/internal/HttpMigrateApplicationComposer.ts +56 -56
- package/src/http/internal/HttpMigrateRouteComposer.ts +505 -505
- package/src/utils/LlmJson.ts +173 -173
- package/src/utils/internal/parseLenientJson.ts +919 -919
|
@@ -1,342 +1,342 @@
|
|
|
1
|
-
import { OpenApi, OpenApiV3_2 } from "@typia/interface";
|
|
2
|
-
|
|
3
|
-
import { OpenApiV3_1TypeChecker } from "../../validators/OpenApiV3_1TypeChecker";
|
|
4
|
-
import { OpenApiV3_1Upgrader } from "./OpenApiV3_1Upgrader";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* OpenAPI v3.2 to emended OpenApi converter.
|
|
8
|
-
*
|
|
9
|
-
* Reuses JSON Schema conversion logic from OpenApiV3_1Upgrader since
|
|
10
|
-
* the schema format is identical between v3.1 and v3.2.
|
|
11
|
-
*/
|
|
12
|
-
export namespace OpenApiV3_2Upgrader {
|
|
13
|
-
export const convert = (input: OpenApiV3_2.IDocument): OpenApi.IDocument => {
|
|
14
|
-
if ((input as OpenApi.IDocument)["x-typia-emended-v12"] === true)
|
|
15
|
-
return input as OpenApi.IDocument;
|
|
16
|
-
return {
|
|
17
|
-
...input,
|
|
18
|
-
openapi: "3.2.0",
|
|
19
|
-
components: convertComponents(input.components ?? {}),
|
|
20
|
-
tags: input.tags?.map(convertTag),
|
|
21
|
-
paths: input.paths
|
|
22
|
-
? Object.fromEntries(
|
|
23
|
-
Object.entries(input.paths)
|
|
24
|
-
.filter(([_, v]) => v !== undefined)
|
|
25
|
-
.map(
|
|
26
|
-
([key, value]) => [key, convertPathItem(input)(value)] as const,
|
|
27
|
-
),
|
|
28
|
-
)
|
|
29
|
-
: undefined,
|
|
30
|
-
webhooks: input.webhooks
|
|
31
|
-
? Object.fromEntries(
|
|
32
|
-
Object.entries(input.webhooks)
|
|
33
|
-
.filter(([_, v]) => v !== undefined)
|
|
34
|
-
.map(
|
|
35
|
-
([key, value]) =>
|
|
36
|
-
[key, convertWebhooks(input)(value)!] as const,
|
|
37
|
-
)
|
|
38
|
-
.filter(([_, value]) => value !== undefined),
|
|
39
|
-
)
|
|
40
|
-
: undefined,
|
|
41
|
-
"x-typia-emended-v12": true,
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/* -----------------------------------------------------------
|
|
46
|
-
TAGS
|
|
47
|
-
----------------------------------------------------------- */
|
|
48
|
-
const convertTag = (tag: OpenApiV3_2.IDocument.ITag): OpenApi.IDocument.ITag => ({
|
|
49
|
-
name: tag.name,
|
|
50
|
-
summary: tag.summary,
|
|
51
|
-
description: tag.description,
|
|
52
|
-
parent: tag.parent,
|
|
53
|
-
kind: tag.kind,
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
/* -----------------------------------------------------------
|
|
57
|
-
OPERATORS
|
|
58
|
-
----------------------------------------------------------- */
|
|
59
|
-
const convertWebhooks =
|
|
60
|
-
(doc: OpenApiV3_2.IDocument) =>
|
|
61
|
-
(
|
|
62
|
-
webhook:
|
|
63
|
-
| OpenApiV3_2.IPath
|
|
64
|
-
| OpenApiV3_2.IJsonSchema.IReference<`#/components/pathItems/${string}`>,
|
|
65
|
-
): OpenApi.IPath | undefined => {
|
|
66
|
-
if (!OpenApiV3_1TypeChecker.isReference(webhook))
|
|
67
|
-
return convertPathItem(doc)(webhook);
|
|
68
|
-
const found: OpenApiV3_2.IPath | undefined =
|
|
69
|
-
doc.components?.pathItems?.[webhook.$ref.split("/").pop() ?? ""];
|
|
70
|
-
return found ? convertPathItem(doc)(found) : undefined;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const convertPathItem =
|
|
74
|
-
(doc: OpenApiV3_2.IDocument) =>
|
|
75
|
-
(pathItem: OpenApiV3_2.IPath): OpenApi.IPath => ({
|
|
76
|
-
...(pathItem as any),
|
|
77
|
-
...(pathItem.get
|
|
78
|
-
? { get: convertOperation(doc)(pathItem)(pathItem.get) }
|
|
79
|
-
: undefined),
|
|
80
|
-
...(pathItem.put
|
|
81
|
-
? { put: convertOperation(doc)(pathItem)(pathItem.put) }
|
|
82
|
-
: undefined),
|
|
83
|
-
...(pathItem.post
|
|
84
|
-
? { post: convertOperation(doc)(pathItem)(pathItem.post) }
|
|
85
|
-
: undefined),
|
|
86
|
-
...(pathItem.delete
|
|
87
|
-
? { delete: convertOperation(doc)(pathItem)(pathItem.delete) }
|
|
88
|
-
: undefined),
|
|
89
|
-
...(pathItem.options
|
|
90
|
-
? { options: convertOperation(doc)(pathItem)(pathItem.options) }
|
|
91
|
-
: undefined),
|
|
92
|
-
...(pathItem.head
|
|
93
|
-
? { head: convertOperation(doc)(pathItem)(pathItem.head) }
|
|
94
|
-
: undefined),
|
|
95
|
-
...(pathItem.patch
|
|
96
|
-
? { patch: convertOperation(doc)(pathItem)(pathItem.patch) }
|
|
97
|
-
: undefined),
|
|
98
|
-
...(pathItem.trace
|
|
99
|
-
? { trace: convertOperation(doc)(pathItem)(pathItem.trace) }
|
|
100
|
-
: undefined),
|
|
101
|
-
...(pathItem.query
|
|
102
|
-
? { query: convertOperation(doc)(pathItem)(pathItem.query) }
|
|
103
|
-
: undefined),
|
|
104
|
-
...(pathItem.additionalOperations
|
|
105
|
-
? {
|
|
106
|
-
additionalOperations: Object.fromEntries(
|
|
107
|
-
Object.entries(pathItem.additionalOperations)
|
|
108
|
-
.filter(([_, v]) => v !== undefined)
|
|
109
|
-
.map(
|
|
110
|
-
([key, value]) =>
|
|
111
|
-
[key, convertOperation(doc)(pathItem)(value)] as const,
|
|
112
|
-
),
|
|
113
|
-
),
|
|
114
|
-
}
|
|
115
|
-
: undefined),
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
const convertOperation =
|
|
119
|
-
(doc: OpenApiV3_2.IDocument) =>
|
|
120
|
-
(pathItem: OpenApiV3_2.IPath) =>
|
|
121
|
-
(input: OpenApiV3_2.IOperation): OpenApi.IOperation => ({
|
|
122
|
-
...input,
|
|
123
|
-
parameters:
|
|
124
|
-
pathItem.parameters !== undefined || input.parameters !== undefined
|
|
125
|
-
? [...(pathItem.parameters ?? []), ...(input.parameters ?? [])]
|
|
126
|
-
.map((p) => {
|
|
127
|
-
if (!OpenApiV3_1TypeChecker.isReference(p))
|
|
128
|
-
return convertParameter(doc.components ?? {})(p);
|
|
129
|
-
const found:
|
|
130
|
-
| Omit<OpenApiV3_2.IOperation.IParameter, "in">
|
|
131
|
-
| undefined = p.$ref.startsWith("#/components/headers/")
|
|
132
|
-
? doc.components?.headers?.[p.$ref.split("/").pop() ?? ""]
|
|
133
|
-
: doc.components?.parameters?.[p.$ref.split("/").pop() ?? ""];
|
|
134
|
-
return found !== undefined
|
|
135
|
-
? convertParameter(doc.components ?? {})({
|
|
136
|
-
...found,
|
|
137
|
-
in: "header",
|
|
138
|
-
})
|
|
139
|
-
: undefined!;
|
|
140
|
-
})
|
|
141
|
-
.filter((_, v) => v !== undefined)
|
|
142
|
-
: undefined,
|
|
143
|
-
requestBody: input.requestBody
|
|
144
|
-
? convertRequestBody(doc)(input.requestBody)
|
|
145
|
-
: undefined,
|
|
146
|
-
responses: input.responses
|
|
147
|
-
? Object.fromEntries(
|
|
148
|
-
Object.entries(input.responses)
|
|
149
|
-
.filter(([_, v]) => v !== undefined)
|
|
150
|
-
.map(
|
|
151
|
-
([key, value]) => [key, convertResponse(doc)(value)!] as const,
|
|
152
|
-
)
|
|
153
|
-
.filter(([_, value]) => value !== undefined),
|
|
154
|
-
)
|
|
155
|
-
: undefined,
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
const convertParameter =
|
|
159
|
-
(components: OpenApiV3_2.IComponents) =>
|
|
160
|
-
(
|
|
161
|
-
input: OpenApiV3_2.IOperation.IParameter,
|
|
162
|
-
): OpenApi.IOperation.IParameter => ({
|
|
163
|
-
...input,
|
|
164
|
-
schema: convertSchema(components)(input.schema),
|
|
165
|
-
examples: input.examples
|
|
166
|
-
? Object.fromEntries(
|
|
167
|
-
Object.entries(input.examples)
|
|
168
|
-
.map(([key, value]) => [
|
|
169
|
-
key,
|
|
170
|
-
OpenApiV3_1TypeChecker.isReference(value)
|
|
171
|
-
? components.examples?.[value.$ref.split("/").pop() ?? ""]
|
|
172
|
-
: value,
|
|
173
|
-
])
|
|
174
|
-
.filter(([_, v]) => v !== undefined),
|
|
175
|
-
)
|
|
176
|
-
: undefined,
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
const convertRequestBody =
|
|
180
|
-
(doc: OpenApiV3_2.IDocument) =>
|
|
181
|
-
(
|
|
182
|
-
input:
|
|
183
|
-
| OpenApiV3_2.IOperation.IRequestBody
|
|
184
|
-
| OpenApiV3_2.IJsonSchema.IReference<`#/components/requestBodies/${string}`>,
|
|
185
|
-
): OpenApi.IOperation.IRequestBody | undefined => {
|
|
186
|
-
if (OpenApiV3_1TypeChecker.isReference(input)) {
|
|
187
|
-
const found: OpenApiV3_2.IOperation.IRequestBody | undefined =
|
|
188
|
-
doc.components?.requestBodies?.[input.$ref.split("/").pop() ?? ""];
|
|
189
|
-
if (found === undefined) return undefined;
|
|
190
|
-
input = found;
|
|
191
|
-
}
|
|
192
|
-
return {
|
|
193
|
-
...input,
|
|
194
|
-
content: input.content
|
|
195
|
-
? convertContent(doc.components ?? {})(input.content)
|
|
196
|
-
: undefined,
|
|
197
|
-
};
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
const convertResponse =
|
|
201
|
-
(doc: OpenApiV3_2.IDocument) =>
|
|
202
|
-
(
|
|
203
|
-
input:
|
|
204
|
-
| OpenApiV3_2.IOperation.IResponse
|
|
205
|
-
| OpenApiV3_2.IJsonSchema.IReference<`#/components/responses/${string}`>,
|
|
206
|
-
): OpenApi.IOperation.IResponse | undefined => {
|
|
207
|
-
if (OpenApiV3_1TypeChecker.isReference(input)) {
|
|
208
|
-
const found: OpenApiV3_2.IOperation.IResponse | undefined =
|
|
209
|
-
doc.components?.responses?.[input.$ref.split("/").pop() ?? ""];
|
|
210
|
-
if (found === undefined) return undefined;
|
|
211
|
-
input = found;
|
|
212
|
-
}
|
|
213
|
-
return {
|
|
214
|
-
...input,
|
|
215
|
-
content: input.content
|
|
216
|
-
? convertContent(doc.components ?? {})(input.content)
|
|
217
|
-
: undefined,
|
|
218
|
-
headers: input.headers
|
|
219
|
-
? Object.fromEntries(
|
|
220
|
-
Object.entries(input.headers)
|
|
221
|
-
.filter(([_, v]) => v !== undefined)
|
|
222
|
-
.map(
|
|
223
|
-
([key, value]) =>
|
|
224
|
-
[
|
|
225
|
-
key,
|
|
226
|
-
(() => {
|
|
227
|
-
if (OpenApiV3_1TypeChecker.isReference(value) === false)
|
|
228
|
-
return convertParameter(doc.components ?? {})({
|
|
229
|
-
...value,
|
|
230
|
-
in: "header",
|
|
231
|
-
});
|
|
232
|
-
const found:
|
|
233
|
-
| Omit<OpenApiV3_2.IOperation.IParameter, "in">
|
|
234
|
-
| undefined = value.$ref.startsWith(
|
|
235
|
-
"#/components/headers/",
|
|
236
|
-
)
|
|
237
|
-
? doc.components?.headers?.[
|
|
238
|
-
value.$ref.split("/").pop() ?? ""
|
|
239
|
-
]
|
|
240
|
-
: undefined;
|
|
241
|
-
return found !== undefined
|
|
242
|
-
? convertParameter(doc.components ?? {})({
|
|
243
|
-
...found,
|
|
244
|
-
in: "header",
|
|
245
|
-
})
|
|
246
|
-
: undefined!;
|
|
247
|
-
})(),
|
|
248
|
-
] as const,
|
|
249
|
-
)
|
|
250
|
-
.filter(([_, v]) => v !== undefined),
|
|
251
|
-
)
|
|
252
|
-
: undefined,
|
|
253
|
-
};
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
const convertContent =
|
|
257
|
-
(components: OpenApiV3_2.IComponents) =>
|
|
258
|
-
(
|
|
259
|
-
record: Record<string, OpenApiV3_2.IOperation.IMediaType>,
|
|
260
|
-
): Record<string, OpenApi.IOperation.IMediaType> =>
|
|
261
|
-
Object.fromEntries(
|
|
262
|
-
Object.entries(record)
|
|
263
|
-
.filter(([_, v]) => v !== undefined)
|
|
264
|
-
.map(
|
|
265
|
-
([key, value]) =>
|
|
266
|
-
[
|
|
267
|
-
key,
|
|
268
|
-
{
|
|
269
|
-
...value,
|
|
270
|
-
schema: value.schema
|
|
271
|
-
? convertSchema(components)(value.schema)
|
|
272
|
-
: undefined,
|
|
273
|
-
itemSchema: value.itemSchema
|
|
274
|
-
? convertSchema(components)(value.itemSchema)
|
|
275
|
-
: undefined,
|
|
276
|
-
examples: value.examples
|
|
277
|
-
? Object.fromEntries(
|
|
278
|
-
Object.entries(value.examples)
|
|
279
|
-
.map(([key, value]) => [
|
|
280
|
-
key,
|
|
281
|
-
OpenApiV3_1TypeChecker.isReference(value)
|
|
282
|
-
? components.examples?.[
|
|
283
|
-
value.$ref.split("/").pop() ?? ""
|
|
284
|
-
]
|
|
285
|
-
: value,
|
|
286
|
-
])
|
|
287
|
-
.filter(([_, v]) => v !== undefined),
|
|
288
|
-
)
|
|
289
|
-
: undefined,
|
|
290
|
-
},
|
|
291
|
-
] as const,
|
|
292
|
-
),
|
|
293
|
-
);
|
|
294
|
-
|
|
295
|
-
/* -----------------------------------------------------------
|
|
296
|
-
DEFINITIONS
|
|
297
|
-
----------------------------------------------------------- */
|
|
298
|
-
export const convertComponents = (
|
|
299
|
-
input: OpenApiV3_2.IComponents,
|
|
300
|
-
): OpenApi.IComponents => ({
|
|
301
|
-
schemas: Object.fromEntries(
|
|
302
|
-
Object.entries(input.schemas ?? {})
|
|
303
|
-
.filter(([_, v]) => v !== undefined)
|
|
304
|
-
.map(([key, value]) => [key, convertSchema(input)(value)] as const),
|
|
305
|
-
),
|
|
306
|
-
securitySchemes: input.securitySchemes
|
|
307
|
-
? Object.fromEntries(
|
|
308
|
-
Object.entries(input.securitySchemes)
|
|
309
|
-
.filter(([_, v]) => v !== undefined)
|
|
310
|
-
.map(
|
|
311
|
-
([key, value]) =>
|
|
312
|
-
[key, convertSecurityScheme(value)] as const,
|
|
313
|
-
),
|
|
314
|
-
)
|
|
315
|
-
: undefined,
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
const convertSecurityScheme = (
|
|
319
|
-
input: OpenApiV3_2.ISecurityScheme,
|
|
320
|
-
): OpenApi.ISecurityScheme => {
|
|
321
|
-
if (input.type === "oauth2") {
|
|
322
|
-
return {
|
|
323
|
-
...input,
|
|
324
|
-
flows: {
|
|
325
|
-
...input.flows,
|
|
326
|
-
deviceAuthorization: input.flows.deviceAuthorization,
|
|
327
|
-
},
|
|
328
|
-
oauth2MetadataUrl: input.oauth2MetadataUrl,
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
return input;
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Reuse schema conversion from OpenApiV3_1Upgrader.
|
|
336
|
-
* OpenAPI v3.2 uses the same JSON Schema (2020-12) as v3.1.
|
|
337
|
-
*/
|
|
338
|
-
export const convertSchema =
|
|
339
|
-
(components: OpenApiV3_2.IComponents) =>
|
|
340
|
-
(input: OpenApiV3_2.IJsonSchema): OpenApi.IJsonSchema =>
|
|
341
|
-
OpenApiV3_1Upgrader.convertSchema(components as any)(input as any);
|
|
342
|
-
}
|
|
1
|
+
import { OpenApi, OpenApiV3_2 } from "@typia/interface";
|
|
2
|
+
|
|
3
|
+
import { OpenApiV3_1TypeChecker } from "../../validators/OpenApiV3_1TypeChecker";
|
|
4
|
+
import { OpenApiV3_1Upgrader } from "./OpenApiV3_1Upgrader";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* OpenAPI v3.2 to emended OpenApi converter.
|
|
8
|
+
*
|
|
9
|
+
* Reuses JSON Schema conversion logic from OpenApiV3_1Upgrader since
|
|
10
|
+
* the schema format is identical between v3.1 and v3.2.
|
|
11
|
+
*/
|
|
12
|
+
export namespace OpenApiV3_2Upgrader {
|
|
13
|
+
export const convert = (input: OpenApiV3_2.IDocument): OpenApi.IDocument => {
|
|
14
|
+
if ((input as OpenApi.IDocument)["x-typia-emended-v12"] === true)
|
|
15
|
+
return input as OpenApi.IDocument;
|
|
16
|
+
return {
|
|
17
|
+
...input,
|
|
18
|
+
openapi: "3.2.0",
|
|
19
|
+
components: convertComponents(input.components ?? {}),
|
|
20
|
+
tags: input.tags?.map(convertTag),
|
|
21
|
+
paths: input.paths
|
|
22
|
+
? Object.fromEntries(
|
|
23
|
+
Object.entries(input.paths)
|
|
24
|
+
.filter(([_, v]) => v !== undefined)
|
|
25
|
+
.map(
|
|
26
|
+
([key, value]) => [key, convertPathItem(input)(value)] as const,
|
|
27
|
+
),
|
|
28
|
+
)
|
|
29
|
+
: undefined,
|
|
30
|
+
webhooks: input.webhooks
|
|
31
|
+
? Object.fromEntries(
|
|
32
|
+
Object.entries(input.webhooks)
|
|
33
|
+
.filter(([_, v]) => v !== undefined)
|
|
34
|
+
.map(
|
|
35
|
+
([key, value]) =>
|
|
36
|
+
[key, convertWebhooks(input)(value)!] as const,
|
|
37
|
+
)
|
|
38
|
+
.filter(([_, value]) => value !== undefined),
|
|
39
|
+
)
|
|
40
|
+
: undefined,
|
|
41
|
+
"x-typia-emended-v12": true,
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/* -----------------------------------------------------------
|
|
46
|
+
TAGS
|
|
47
|
+
----------------------------------------------------------- */
|
|
48
|
+
const convertTag = (tag: OpenApiV3_2.IDocument.ITag): OpenApi.IDocument.ITag => ({
|
|
49
|
+
name: tag.name,
|
|
50
|
+
summary: tag.summary,
|
|
51
|
+
description: tag.description,
|
|
52
|
+
parent: tag.parent,
|
|
53
|
+
kind: tag.kind,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
/* -----------------------------------------------------------
|
|
57
|
+
OPERATORS
|
|
58
|
+
----------------------------------------------------------- */
|
|
59
|
+
const convertWebhooks =
|
|
60
|
+
(doc: OpenApiV3_2.IDocument) =>
|
|
61
|
+
(
|
|
62
|
+
webhook:
|
|
63
|
+
| OpenApiV3_2.IPath
|
|
64
|
+
| OpenApiV3_2.IJsonSchema.IReference<`#/components/pathItems/${string}`>,
|
|
65
|
+
): OpenApi.IPath | undefined => {
|
|
66
|
+
if (!OpenApiV3_1TypeChecker.isReference(webhook))
|
|
67
|
+
return convertPathItem(doc)(webhook);
|
|
68
|
+
const found: OpenApiV3_2.IPath | undefined =
|
|
69
|
+
doc.components?.pathItems?.[webhook.$ref.split("/").pop() ?? ""];
|
|
70
|
+
return found ? convertPathItem(doc)(found) : undefined;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const convertPathItem =
|
|
74
|
+
(doc: OpenApiV3_2.IDocument) =>
|
|
75
|
+
(pathItem: OpenApiV3_2.IPath): OpenApi.IPath => ({
|
|
76
|
+
...(pathItem as any),
|
|
77
|
+
...(pathItem.get
|
|
78
|
+
? { get: convertOperation(doc)(pathItem)(pathItem.get) }
|
|
79
|
+
: undefined),
|
|
80
|
+
...(pathItem.put
|
|
81
|
+
? { put: convertOperation(doc)(pathItem)(pathItem.put) }
|
|
82
|
+
: undefined),
|
|
83
|
+
...(pathItem.post
|
|
84
|
+
? { post: convertOperation(doc)(pathItem)(pathItem.post) }
|
|
85
|
+
: undefined),
|
|
86
|
+
...(pathItem.delete
|
|
87
|
+
? { delete: convertOperation(doc)(pathItem)(pathItem.delete) }
|
|
88
|
+
: undefined),
|
|
89
|
+
...(pathItem.options
|
|
90
|
+
? { options: convertOperation(doc)(pathItem)(pathItem.options) }
|
|
91
|
+
: undefined),
|
|
92
|
+
...(pathItem.head
|
|
93
|
+
? { head: convertOperation(doc)(pathItem)(pathItem.head) }
|
|
94
|
+
: undefined),
|
|
95
|
+
...(pathItem.patch
|
|
96
|
+
? { patch: convertOperation(doc)(pathItem)(pathItem.patch) }
|
|
97
|
+
: undefined),
|
|
98
|
+
...(pathItem.trace
|
|
99
|
+
? { trace: convertOperation(doc)(pathItem)(pathItem.trace) }
|
|
100
|
+
: undefined),
|
|
101
|
+
...(pathItem.query
|
|
102
|
+
? { query: convertOperation(doc)(pathItem)(pathItem.query) }
|
|
103
|
+
: undefined),
|
|
104
|
+
...(pathItem.additionalOperations
|
|
105
|
+
? {
|
|
106
|
+
additionalOperations: Object.fromEntries(
|
|
107
|
+
Object.entries(pathItem.additionalOperations)
|
|
108
|
+
.filter(([_, v]) => v !== undefined)
|
|
109
|
+
.map(
|
|
110
|
+
([key, value]) =>
|
|
111
|
+
[key, convertOperation(doc)(pathItem)(value)] as const,
|
|
112
|
+
),
|
|
113
|
+
),
|
|
114
|
+
}
|
|
115
|
+
: undefined),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const convertOperation =
|
|
119
|
+
(doc: OpenApiV3_2.IDocument) =>
|
|
120
|
+
(pathItem: OpenApiV3_2.IPath) =>
|
|
121
|
+
(input: OpenApiV3_2.IOperation): OpenApi.IOperation => ({
|
|
122
|
+
...input,
|
|
123
|
+
parameters:
|
|
124
|
+
pathItem.parameters !== undefined || input.parameters !== undefined
|
|
125
|
+
? [...(pathItem.parameters ?? []), ...(input.parameters ?? [])]
|
|
126
|
+
.map((p) => {
|
|
127
|
+
if (!OpenApiV3_1TypeChecker.isReference(p))
|
|
128
|
+
return convertParameter(doc.components ?? {})(p);
|
|
129
|
+
const found:
|
|
130
|
+
| Omit<OpenApiV3_2.IOperation.IParameter, "in">
|
|
131
|
+
| undefined = p.$ref.startsWith("#/components/headers/")
|
|
132
|
+
? doc.components?.headers?.[p.$ref.split("/").pop() ?? ""]
|
|
133
|
+
: doc.components?.parameters?.[p.$ref.split("/").pop() ?? ""];
|
|
134
|
+
return found !== undefined
|
|
135
|
+
? convertParameter(doc.components ?? {})({
|
|
136
|
+
...found,
|
|
137
|
+
in: "header",
|
|
138
|
+
})
|
|
139
|
+
: undefined!;
|
|
140
|
+
})
|
|
141
|
+
.filter((_, v) => v !== undefined)
|
|
142
|
+
: undefined,
|
|
143
|
+
requestBody: input.requestBody
|
|
144
|
+
? convertRequestBody(doc)(input.requestBody)
|
|
145
|
+
: undefined,
|
|
146
|
+
responses: input.responses
|
|
147
|
+
? Object.fromEntries(
|
|
148
|
+
Object.entries(input.responses)
|
|
149
|
+
.filter(([_, v]) => v !== undefined)
|
|
150
|
+
.map(
|
|
151
|
+
([key, value]) => [key, convertResponse(doc)(value)!] as const,
|
|
152
|
+
)
|
|
153
|
+
.filter(([_, value]) => value !== undefined),
|
|
154
|
+
)
|
|
155
|
+
: undefined,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const convertParameter =
|
|
159
|
+
(components: OpenApiV3_2.IComponents) =>
|
|
160
|
+
(
|
|
161
|
+
input: OpenApiV3_2.IOperation.IParameter,
|
|
162
|
+
): OpenApi.IOperation.IParameter => ({
|
|
163
|
+
...input,
|
|
164
|
+
schema: convertSchema(components)(input.schema),
|
|
165
|
+
examples: input.examples
|
|
166
|
+
? Object.fromEntries(
|
|
167
|
+
Object.entries(input.examples)
|
|
168
|
+
.map(([key, value]) => [
|
|
169
|
+
key,
|
|
170
|
+
OpenApiV3_1TypeChecker.isReference(value)
|
|
171
|
+
? components.examples?.[value.$ref.split("/").pop() ?? ""]
|
|
172
|
+
: value,
|
|
173
|
+
])
|
|
174
|
+
.filter(([_, v]) => v !== undefined),
|
|
175
|
+
)
|
|
176
|
+
: undefined,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const convertRequestBody =
|
|
180
|
+
(doc: OpenApiV3_2.IDocument) =>
|
|
181
|
+
(
|
|
182
|
+
input:
|
|
183
|
+
| OpenApiV3_2.IOperation.IRequestBody
|
|
184
|
+
| OpenApiV3_2.IJsonSchema.IReference<`#/components/requestBodies/${string}`>,
|
|
185
|
+
): OpenApi.IOperation.IRequestBody | undefined => {
|
|
186
|
+
if (OpenApiV3_1TypeChecker.isReference(input)) {
|
|
187
|
+
const found: OpenApiV3_2.IOperation.IRequestBody | undefined =
|
|
188
|
+
doc.components?.requestBodies?.[input.$ref.split("/").pop() ?? ""];
|
|
189
|
+
if (found === undefined) return undefined;
|
|
190
|
+
input = found;
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
...input,
|
|
194
|
+
content: input.content
|
|
195
|
+
? convertContent(doc.components ?? {})(input.content)
|
|
196
|
+
: undefined,
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const convertResponse =
|
|
201
|
+
(doc: OpenApiV3_2.IDocument) =>
|
|
202
|
+
(
|
|
203
|
+
input:
|
|
204
|
+
| OpenApiV3_2.IOperation.IResponse
|
|
205
|
+
| OpenApiV3_2.IJsonSchema.IReference<`#/components/responses/${string}`>,
|
|
206
|
+
): OpenApi.IOperation.IResponse | undefined => {
|
|
207
|
+
if (OpenApiV3_1TypeChecker.isReference(input)) {
|
|
208
|
+
const found: OpenApiV3_2.IOperation.IResponse | undefined =
|
|
209
|
+
doc.components?.responses?.[input.$ref.split("/").pop() ?? ""];
|
|
210
|
+
if (found === undefined) return undefined;
|
|
211
|
+
input = found;
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
...input,
|
|
215
|
+
content: input.content
|
|
216
|
+
? convertContent(doc.components ?? {})(input.content)
|
|
217
|
+
: undefined,
|
|
218
|
+
headers: input.headers
|
|
219
|
+
? Object.fromEntries(
|
|
220
|
+
Object.entries(input.headers)
|
|
221
|
+
.filter(([_, v]) => v !== undefined)
|
|
222
|
+
.map(
|
|
223
|
+
([key, value]) =>
|
|
224
|
+
[
|
|
225
|
+
key,
|
|
226
|
+
(() => {
|
|
227
|
+
if (OpenApiV3_1TypeChecker.isReference(value) === false)
|
|
228
|
+
return convertParameter(doc.components ?? {})({
|
|
229
|
+
...value,
|
|
230
|
+
in: "header",
|
|
231
|
+
});
|
|
232
|
+
const found:
|
|
233
|
+
| Omit<OpenApiV3_2.IOperation.IParameter, "in">
|
|
234
|
+
| undefined = value.$ref.startsWith(
|
|
235
|
+
"#/components/headers/",
|
|
236
|
+
)
|
|
237
|
+
? doc.components?.headers?.[
|
|
238
|
+
value.$ref.split("/").pop() ?? ""
|
|
239
|
+
]
|
|
240
|
+
: undefined;
|
|
241
|
+
return found !== undefined
|
|
242
|
+
? convertParameter(doc.components ?? {})({
|
|
243
|
+
...found,
|
|
244
|
+
in: "header",
|
|
245
|
+
})
|
|
246
|
+
: undefined!;
|
|
247
|
+
})(),
|
|
248
|
+
] as const,
|
|
249
|
+
)
|
|
250
|
+
.filter(([_, v]) => v !== undefined),
|
|
251
|
+
)
|
|
252
|
+
: undefined,
|
|
253
|
+
};
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const convertContent =
|
|
257
|
+
(components: OpenApiV3_2.IComponents) =>
|
|
258
|
+
(
|
|
259
|
+
record: Record<string, OpenApiV3_2.IOperation.IMediaType>,
|
|
260
|
+
): Record<string, OpenApi.IOperation.IMediaType> =>
|
|
261
|
+
Object.fromEntries(
|
|
262
|
+
Object.entries(record)
|
|
263
|
+
.filter(([_, v]) => v !== undefined)
|
|
264
|
+
.map(
|
|
265
|
+
([key, value]) =>
|
|
266
|
+
[
|
|
267
|
+
key,
|
|
268
|
+
{
|
|
269
|
+
...value,
|
|
270
|
+
schema: value.schema
|
|
271
|
+
? convertSchema(components)(value.schema)
|
|
272
|
+
: undefined,
|
|
273
|
+
itemSchema: value.itemSchema
|
|
274
|
+
? convertSchema(components)(value.itemSchema)
|
|
275
|
+
: undefined,
|
|
276
|
+
examples: value.examples
|
|
277
|
+
? Object.fromEntries(
|
|
278
|
+
Object.entries(value.examples)
|
|
279
|
+
.map(([key, value]) => [
|
|
280
|
+
key,
|
|
281
|
+
OpenApiV3_1TypeChecker.isReference(value)
|
|
282
|
+
? components.examples?.[
|
|
283
|
+
value.$ref.split("/").pop() ?? ""
|
|
284
|
+
]
|
|
285
|
+
: value,
|
|
286
|
+
])
|
|
287
|
+
.filter(([_, v]) => v !== undefined),
|
|
288
|
+
)
|
|
289
|
+
: undefined,
|
|
290
|
+
},
|
|
291
|
+
] as const,
|
|
292
|
+
),
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
/* -----------------------------------------------------------
|
|
296
|
+
DEFINITIONS
|
|
297
|
+
----------------------------------------------------------- */
|
|
298
|
+
export const convertComponents = (
|
|
299
|
+
input: OpenApiV3_2.IComponents,
|
|
300
|
+
): OpenApi.IComponents => ({
|
|
301
|
+
schemas: Object.fromEntries(
|
|
302
|
+
Object.entries(input.schemas ?? {})
|
|
303
|
+
.filter(([_, v]) => v !== undefined)
|
|
304
|
+
.map(([key, value]) => [key, convertSchema(input)(value)] as const),
|
|
305
|
+
),
|
|
306
|
+
securitySchemes: input.securitySchemes
|
|
307
|
+
? Object.fromEntries(
|
|
308
|
+
Object.entries(input.securitySchemes)
|
|
309
|
+
.filter(([_, v]) => v !== undefined)
|
|
310
|
+
.map(
|
|
311
|
+
([key, value]) =>
|
|
312
|
+
[key, convertSecurityScheme(value)] as const,
|
|
313
|
+
),
|
|
314
|
+
)
|
|
315
|
+
: undefined,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
const convertSecurityScheme = (
|
|
319
|
+
input: OpenApiV3_2.ISecurityScheme,
|
|
320
|
+
): OpenApi.ISecurityScheme => {
|
|
321
|
+
if (input.type === "oauth2") {
|
|
322
|
+
return {
|
|
323
|
+
...input,
|
|
324
|
+
flows: {
|
|
325
|
+
...input.flows,
|
|
326
|
+
deviceAuthorization: input.flows.deviceAuthorization,
|
|
327
|
+
},
|
|
328
|
+
oauth2MetadataUrl: input.oauth2MetadataUrl,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
return input;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Reuse schema conversion from OpenApiV3_1Upgrader.
|
|
336
|
+
* OpenAPI v3.2 uses the same JSON Schema (2020-12) as v3.1.
|
|
337
|
+
*/
|
|
338
|
+
export const convertSchema =
|
|
339
|
+
(components: OpenApiV3_2.IComponents) =>
|
|
340
|
+
(input: OpenApiV3_2.IJsonSchema): OpenApi.IJsonSchema =>
|
|
341
|
+
OpenApiV3_1Upgrader.convertSchema(components as any)(input as any);
|
|
342
|
+
}
|