@typia/utils 12.0.0-dev.20260309 → 12.0.0-dev.20260310

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.
Files changed (92) hide show
  1. package/lib/http/internal/HttpLlmApplicationComposer.mjs +5 -1
  2. package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
  3. package/lib/index.mjs +9 -9
  4. package/lib/utils/LlmJson.mjs +9 -2
  5. package/lib/utils/LlmJson.mjs.map +1 -1
  6. package/lib/utils/internal/stringifyValidationFailure.js +17 -15
  7. package/lib/utils/internal/stringifyValidationFailure.js.map +1 -1
  8. package/lib/utils/internal/stringifyValidationFailure.mjs +17 -15
  9. package/lib/utils/internal/stringifyValidationFailure.mjs.map +1 -1
  10. package/lib/validators/internal/OpenApiOneOfValidator.mjs +5 -1
  11. package/lib/validators/internal/OpenApiOneOfValidator.mjs.map +1 -1
  12. package/package.json +2 -2
  13. package/src/converters/LlmSchemaConverter.ts +647 -647
  14. package/src/converters/OpenApiConverter.ts +285 -285
  15. package/src/converters/index.ts +5 -5
  16. package/src/converters/internal/LlmDescriptionInverter.ts +178 -178
  17. package/src/converters/internal/LlmParametersComposer.ts +52 -52
  18. package/src/converters/internal/OpenApiConstraintShifter.ts +154 -154
  19. package/src/converters/internal/OpenApiExclusiveEmender.ts +46 -46
  20. package/src/converters/internal/OpenApiV3Downgrader.ts +355 -355
  21. package/src/converters/internal/OpenApiV3Upgrader.ts +470 -470
  22. package/src/converters/internal/OpenApiV3_1Upgrader.ts +685 -685
  23. package/src/converters/internal/SwaggerV2Downgrader.ts +424 -424
  24. package/src/converters/internal/SwaggerV2Upgrader.ts +523 -523
  25. package/src/http/HttpError.ts +107 -107
  26. package/src/http/HttpLlm.ts +167 -167
  27. package/src/http/HttpMigration.ts +92 -92
  28. package/src/http/index.ts +3 -3
  29. package/src/http/internal/HttpLlmApplicationComposer.ts +361 -361
  30. package/src/http/internal/HttpLlmFunctionFetcher.ts +37 -37
  31. package/src/http/internal/HttpMigrateApplicationComposer.ts +56 -56
  32. package/src/http/internal/HttpMigrateRouteAccessor.ts +135 -135
  33. package/src/http/internal/HttpMigrateRouteComposer.ts +505 -505
  34. package/src/http/internal/HttpMigrateRouteFetcher.ts +203 -203
  35. package/src/index.ts +4 -4
  36. package/src/utils/ArrayUtil.ts +42 -42
  37. package/src/utils/LlmJson.ts +141 -141
  38. package/src/utils/MapUtil.ts +15 -15
  39. package/src/utils/NamingConvention.ts +205 -205
  40. package/src/utils/Singleton.ts +17 -17
  41. package/src/utils/StringUtil.ts +14 -14
  42. package/src/utils/dedent.ts +57 -57
  43. package/src/utils/index.ts +8 -8
  44. package/src/utils/internal/EndpointUtil.ts +44 -44
  45. package/src/utils/internal/JsonDescriptor.ts +70 -70
  46. package/src/utils/internal/OpenApiTypeCheckerBase.ts +822 -822
  47. package/src/utils/internal/coerceLlmArguments.ts +314 -314
  48. package/src/utils/internal/parseLenientJson.ts +894 -894
  49. package/src/utils/internal/stringifyValidationFailure.ts +415 -411
  50. package/src/validators/LlmTypeChecker.ts +402 -402
  51. package/src/validators/OpenApiTypeChecker.ts +297 -297
  52. package/src/validators/OpenApiV3TypeChecker.ts +70 -70
  53. package/src/validators/OpenApiV3_1TypeChecker.ts +86 -86
  54. package/src/validators/OpenApiValidator.ts +94 -94
  55. package/src/validators/SwaggerV2TypeChecker.ts +71 -71
  56. package/src/validators/functional/_isBigintString.ts +8 -8
  57. package/src/validators/functional/_isFormatByte.ts +7 -7
  58. package/src/validators/functional/_isFormatDate.ts +3 -3
  59. package/src/validators/functional/_isFormatDateTime.ts +4 -4
  60. package/src/validators/functional/_isFormatDuration.ts +4 -4
  61. package/src/validators/functional/_isFormatEmail.ts +4 -4
  62. package/src/validators/functional/_isFormatHostname.ts +4 -4
  63. package/src/validators/functional/_isFormatIdnEmail.ts +4 -4
  64. package/src/validators/functional/_isFormatIdnHostname.ts +4 -4
  65. package/src/validators/functional/_isFormatIpv4.ts +4 -4
  66. package/src/validators/functional/_isFormatIpv6.ts +4 -4
  67. package/src/validators/functional/_isFormatIri.ts +3 -3
  68. package/src/validators/functional/_isFormatIriReference.ts +4 -4
  69. package/src/validators/functional/_isFormatJsonPointer.ts +3 -3
  70. package/src/validators/functional/_isFormatPassword.ts +1 -1
  71. package/src/validators/functional/_isFormatRegex.ts +8 -8
  72. package/src/validators/functional/_isFormatRelativeJsonPointer.ts +4 -4
  73. package/src/validators/functional/_isFormatTime.ts +4 -4
  74. package/src/validators/functional/_isFormatUri.ts +6 -6
  75. package/src/validators/functional/_isFormatUriReference.ts +5 -5
  76. package/src/validators/functional/_isFormatUriTemplate.ts +4 -4
  77. package/src/validators/functional/_isFormatUrl.ts +4 -4
  78. package/src/validators/functional/_isFormatUuid.ts +3 -3
  79. package/src/validators/functional/_isUniqueItems.ts +159 -159
  80. package/src/validators/index.ts +14 -14
  81. package/src/validators/internal/IOpenApiValidatorContext.ts +17 -17
  82. package/src/validators/internal/OpenApiArrayValidator.ts +49 -49
  83. package/src/validators/internal/OpenApiBooleanValidator.ts +11 -11
  84. package/src/validators/internal/OpenApiConstantValidator.ts +11 -11
  85. package/src/validators/internal/OpenApiIntegerValidator.ts +49 -49
  86. package/src/validators/internal/OpenApiNumberValidator.ts +48 -48
  87. package/src/validators/internal/OpenApiObjectValidator.ts +83 -83
  88. package/src/validators/internal/OpenApiOneOfValidator.ts +309 -309
  89. package/src/validators/internal/OpenApiSchemaNamingRule.ts +124 -124
  90. package/src/validators/internal/OpenApiStationValidator.ts +115 -115
  91. package/src/validators/internal/OpenApiStringValidator.ts +88 -88
  92. package/src/validators/internal/OpenApiTupleValidator.ts +55 -55
@@ -1,523 +1,523 @@
1
- import { IJsonSchemaAttribute, OpenApi, SwaggerV2 } from "@typia/interface";
2
-
3
- import { OpenApiTypeChecker } from "../../validators/OpenApiTypeChecker";
4
- import { SwaggerV2TypeChecker } from "../../validators/SwaggerV2TypeChecker";
5
- import { OpenApiExclusiveEmender } from "./OpenApiExclusiveEmender";
6
-
7
- export namespace SwaggerV2Upgrader {
8
- export const convert = (input: SwaggerV2.IDocument): OpenApi.IDocument => ({
9
- openapi: "3.1.0",
10
- info: input.info,
11
- components: convertComponents(input),
12
- paths: input.paths
13
- ? Object.fromEntries(
14
- Object.entries(input.paths)
15
- .filter(([_, v]) => v !== undefined)
16
- .map(
17
- ([key, value]) => [key, convertPathItem(input)(value)] as const,
18
- ),
19
- )
20
- : undefined,
21
- servers: input.host
22
- ? [
23
- {
24
- url: input.host,
25
- },
26
- ]
27
- : undefined,
28
- security: input.security,
29
- tags: input.tags,
30
- "x-samchon-emended-v4": true,
31
- });
32
-
33
- /* -----------------------------------------------------------
34
- OPERATORS
35
- ----------------------------------------------------------- */
36
- const convertPathItem =
37
- (doc: SwaggerV2.IDocument) =>
38
- (pathItem: SwaggerV2.IPath): OpenApi.IPath => ({
39
- ...(pathItem as any),
40
- ...(pathItem.get
41
- ? { get: convertOperation(doc)(pathItem)(pathItem.get) }
42
- : undefined),
43
- ...(pathItem.put
44
- ? { put: convertOperation(doc)(pathItem)(pathItem.put) }
45
- : undefined),
46
- ...(pathItem.post
47
- ? { post: convertOperation(doc)(pathItem)(pathItem.post) }
48
- : undefined),
49
- ...(pathItem.delete
50
- ? { delete: convertOperation(doc)(pathItem)(pathItem.delete) }
51
- : undefined),
52
- ...(pathItem.options
53
- ? { options: convertOperation(doc)(pathItem)(pathItem.options) }
54
- : undefined),
55
- ...(pathItem.head
56
- ? { head: convertOperation(doc)(pathItem)(pathItem.head) }
57
- : undefined),
58
- ...(pathItem.patch
59
- ? { patch: convertOperation(doc)(pathItem)(pathItem.patch) }
60
- : undefined),
61
- ...(pathItem.trace
62
- ? { trace: convertOperation(doc)(pathItem)(pathItem.trace) }
63
- : undefined),
64
- });
65
-
66
- const convertOperation =
67
- (doc: SwaggerV2.IDocument) =>
68
- (pathItem: SwaggerV2.IPath) =>
69
- (input: SwaggerV2.IOperation): OpenApi.IOperation => ({
70
- ...input,
71
- parameters:
72
- pathItem.parameters !== undefined || input.parameters !== undefined
73
- ? (
74
- [...(pathItem.parameters ?? []), ...(input.parameters ?? [])]
75
- .map((p) =>
76
- SwaggerV2TypeChecker.isReference(p)
77
- ? doc.parameters?.[p.$ref.split("/").pop() ?? ""]!
78
- : p,
79
- )
80
- .filter(
81
- (p) =>
82
- p !== undefined &&
83
- p.in !== "body" &&
84
- (p as SwaggerV2.IOperation.IBodyParameter).schema ===
85
- undefined,
86
- ) as SwaggerV2.IOperation.IGeneralParameter[]
87
- ).map(convertParameter(doc.definitions ?? {}))
88
- : undefined,
89
- requestBody: (() => {
90
- const found: SwaggerV2.IOperation.IBodyParameter | undefined =
91
- input.parameters?.find((p) => {
92
- if (SwaggerV2TypeChecker.isReference(p))
93
- p = doc.parameters?.[p.$ref.split("/").pop() ?? ""]!;
94
- return (
95
- (p as SwaggerV2.IOperation.IBodyParameter)?.schema !== undefined
96
- );
97
- }) as SwaggerV2.IOperation.IBodyParameter | undefined;
98
- return found
99
- ? convertRequestBody(doc.definitions ?? {})(found)
100
- : undefined;
101
- })(),
102
- responses: input.responses
103
- ? Object.fromEntries(
104
- Object.entries(input.responses)
105
- .filter(([_, v]) => v !== undefined)
106
- .map(
107
- ([key, value]) => [key, convertResponse(doc)(value)!] as const,
108
- )
109
- .filter(([_, v]) => v !== undefined),
110
- )
111
- : undefined,
112
- });
113
-
114
- const convertParameter =
115
- (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
116
- (
117
- input: SwaggerV2.IOperation.IGeneralParameter,
118
- ): OpenApi.IOperation.IParameter => ({
119
- name: input.name,
120
- in: input.in as any,
121
- description: input.description,
122
- schema: convertSchema(definitions)(input),
123
- required: true,
124
- });
125
- const convertRequestBody =
126
- (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
127
- (
128
- input: SwaggerV2.IOperation.IBodyParameter,
129
- ): OpenApi.IOperation.IRequestBody => ({
130
- description: input.description,
131
- content: {
132
- "application/json": {
133
- schema: convertSchema(definitions)(input.schema),
134
- },
135
- },
136
- });
137
-
138
- const convertResponse =
139
- (doc: SwaggerV2.IDocument) =>
140
- (
141
- input:
142
- | SwaggerV2.IOperation.IResponse
143
- | SwaggerV2.IJsonSchema.IReference<`#/definitions/responses/${string}`>,
144
- ): OpenApi.IOperation.IResponse | undefined => {
145
- if (SwaggerV2TypeChecker.isReference(input)) {
146
- const found: SwaggerV2.IOperation.IResponse | undefined =
147
- doc.responses?.[input.$ref.split("/").pop() ?? ""]!;
148
- if (found === undefined) return undefined;
149
- input = found;
150
- }
151
- return {
152
- description: input.description,
153
- content: input.schema
154
- ? {
155
- "application/json": {
156
- schema: convertSchema(doc.definitions ?? {})(input.schema),
157
- example: input.example,
158
- },
159
- }
160
- : undefined,
161
- headers: input.headers
162
- ? Object.fromEntries(
163
- Object.entries(input.headers)
164
- .filter(([_, v]) => v !== undefined)
165
- .map(
166
- ([key, value]) =>
167
- [
168
- key,
169
- {
170
- schema: convertSchema(doc.definitions ?? {})(value),
171
- in: "header",
172
- },
173
- ] as const,
174
- ),
175
- )
176
- : undefined,
177
- };
178
- };
179
-
180
- /* -----------------------------------------------------------
181
- DEFINITIONS
182
- ----------------------------------------------------------- */
183
- export const convertComponents = (
184
- input: SwaggerV2.IDocument,
185
- ): OpenApi.IComponents => ({
186
- schemas: Object.fromEntries(
187
- Object.entries(input.definitions ?? {})
188
- .filter(([_, v]) => v !== undefined)
189
- .map(([key, value]) => [
190
- key,
191
- convertSchema(input.definitions ?? {})(value),
192
- ]),
193
- ),
194
- securitySchemes: input.securityDefinitions
195
- ? Object.fromEntries(
196
- Object.entries(input.securityDefinitions)
197
- .filter(([_, v]) => v !== undefined)
198
- .map(([key, value]) => [key, convertSecurityScheme(value)])
199
- .filter(([_, v]) => v !== undefined),
200
- )
201
- : undefined,
202
- });
203
-
204
- const convertSecurityScheme = (
205
- input: SwaggerV2.ISecurityDefinition,
206
- ): OpenApi.ISecurityScheme => {
207
- if (input.type === "apiKey") return input;
208
- else if (input.type === "basic")
209
- return {
210
- type: "http",
211
- scheme: "basic",
212
- description: input.description,
213
- };
214
- else if (input.type === "oauth2")
215
- if (input.flow === "implicit")
216
- return {
217
- type: "oauth2",
218
- description: input.description,
219
- flows: {
220
- implicit: {
221
- authorizationUrl: input.authorizationUrl,
222
- scopes: input.scopes,
223
- },
224
- },
225
- };
226
- else if (input.flow === "accessCode")
227
- return {
228
- type: "oauth2",
229
- description: input.description,
230
- flows: {
231
- authorizationCode: {
232
- authorizationUrl: input.authorizationUrl,
233
- tokenUrl: input.tokenUrl,
234
- scopes: input.scopes,
235
- },
236
- },
237
- };
238
- else if (input.flow === "password")
239
- return {
240
- type: "oauth2",
241
- description: input.description,
242
- flows: {
243
- password: {
244
- tokenUrl: input.tokenUrl,
245
- scopes: input.scopes,
246
- },
247
- },
248
- };
249
- else if (input.flow === "application")
250
- return {
251
- type: "oauth2",
252
- description: input.description,
253
- flows: {
254
- clientCredentials: {
255
- tokenUrl: input.tokenUrl,
256
- scopes: input.scopes,
257
- },
258
- },
259
- };
260
- else return undefined!;
261
- return undefined!;
262
- };
263
-
264
- export const convertSchema =
265
- (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
266
- (input: SwaggerV2.IJsonSchema): OpenApi.IJsonSchema => {
267
- const nullable: { value: boolean; default?: null } = {
268
- value: false,
269
- default: undefined,
270
- };
271
- const union: OpenApi.IJsonSchema[] = [];
272
- const attribute: IJsonSchemaAttribute = {
273
- title: input.title,
274
- description: input.description,
275
- deprecated: input.deprecated,
276
- readOnly: input.readOnly,
277
- example: input.example,
278
- examples: input.examples
279
- ? Object.fromEntries(input.examples.map((v, i) => [`v${i}`, v]))
280
- : undefined,
281
- ...Object.fromEntries(
282
- Object.entries(input).filter(
283
- ([key, value]) => key.startsWith("x-") && value !== undefined,
284
- ),
285
- ),
286
- };
287
- const visit = (schema: SwaggerV2.IJsonSchema): void => {
288
- // NULLABLE PROPERTY
289
- if (
290
- (schema as SwaggerV2.IJsonSchema.__ISignificant<any>)[
291
- "x-nullable"
292
- ] === true
293
- ) {
294
- nullable.value ||= true;
295
- if ((schema as SwaggerV2.IJsonSchema.INumber).default === null)
296
- nullable.default = null;
297
- }
298
- if (
299
- Array.isArray((schema as SwaggerV2.IJsonSchema.INumber).enum) &&
300
- (schema as SwaggerV2.IJsonSchema.INumber).enum?.length &&
301
- (schema as SwaggerV2.IJsonSchema.INumber).enum?.some(
302
- (e) => e === null,
303
- )
304
- )
305
- nullable.value ||= true;
306
- // UNION TYPE CASE
307
- if (SwaggerV2TypeChecker.isAnyOf(schema))
308
- schema["x-anyOf"].forEach(visit);
309
- else if (SwaggerV2TypeChecker.isOneOf(schema))
310
- schema["x-oneOf"].forEach(visit);
311
- else if (SwaggerV2TypeChecker.isAllOf(schema))
312
- if (schema.allOf.length === 1) visit(schema.allOf[0]!);
313
- else union.push(convertAllOfSchema(definitions)(schema));
314
- // ATOMIC TYPE CASE (CONSIDER ENUM VALUES)
315
- else if (
316
- SwaggerV2TypeChecker.isBoolean(schema) ||
317
- SwaggerV2TypeChecker.isInteger(schema) ||
318
- SwaggerV2TypeChecker.isNumber(schema) ||
319
- SwaggerV2TypeChecker.isString(schema)
320
- )
321
- if (
322
- schema.enum?.length &&
323
- schema.enum.filter((e) => e !== null).length
324
- )
325
- union.push(
326
- ...schema.enum
327
- .filter((v) => v !== null)
328
- .map((value) => ({ const: value })),
329
- );
330
- else if (
331
- SwaggerV2TypeChecker.isInteger(schema) ||
332
- SwaggerV2TypeChecker.isNumber(schema)
333
- )
334
- union.push(
335
- OpenApiExclusiveEmender.emend({
336
- ...schema,
337
- default: (schema.default ?? undefined) satisfies
338
- | boolean
339
- | number
340
- | string
341
- | undefined as any,
342
- examples: schema.examples
343
- ? Object.fromEntries(
344
- schema.examples.map((v, i) => [`v${i}`, v]),
345
- )
346
- : undefined,
347
- exclusiveMinimum:
348
- typeof schema.exclusiveMinimum === "boolean"
349
- ? schema.exclusiveMinimum === true
350
- ? schema.minimum
351
- : undefined
352
- : schema.exclusiveMinimum,
353
- exclusiveMaximum:
354
- typeof schema.exclusiveMaximum === "boolean"
355
- ? schema.exclusiveMaximum === true
356
- ? schema.maximum
357
- : undefined
358
- : schema.exclusiveMaximum,
359
- minimum:
360
- schema.exclusiveMinimum === true ? undefined : schema.minimum,
361
- maximum:
362
- schema.exclusiveMaximum === true ? undefined : schema.maximum,
363
- ...{ enum: undefined },
364
- }),
365
- );
366
- else
367
- union.push({
368
- ...schema,
369
- default: (schema.default ?? undefined) satisfies
370
- | boolean
371
- | number
372
- | string
373
- | undefined as any,
374
- examples: schema.examples
375
- ? Object.fromEntries(
376
- schema.examples.map((v, i) => [`v${i}`, v]),
377
- )
378
- : undefined,
379
- ...{ enum: undefined },
380
- });
381
- // INSTANCE TYPE CASE
382
- else if (SwaggerV2TypeChecker.isArray(schema))
383
- union.push({
384
- ...schema,
385
- items: convertSchema(definitions)(schema.items),
386
- examples: schema.examples
387
- ? Object.fromEntries(schema.examples.map((v, i) => [`v${i}`, v]))
388
- : undefined,
389
- });
390
- else if (SwaggerV2TypeChecker.isObject(schema))
391
- union.push({
392
- ...schema,
393
- ...{
394
- properties: schema.properties
395
- ? Object.fromEntries(
396
- Object.entries(schema.properties)
397
- .filter(([_, v]) => v !== undefined)
398
- .map(([key, value]) => [
399
- key,
400
- convertSchema(definitions)(value),
401
- ]),
402
- )
403
- : {},
404
- additionalProperties: schema.additionalProperties
405
- ? typeof schema.additionalProperties === "object" &&
406
- schema.additionalProperties !== null
407
- ? convertSchema(definitions)(schema.additionalProperties)
408
- : schema.additionalProperties
409
- : undefined,
410
- },
411
- examples: schema.examples
412
- ? Object.fromEntries(schema.examples.map((v, i) => [`v${i}`, v]))
413
- : undefined,
414
- required: schema.required ?? [],
415
- });
416
- else if (SwaggerV2TypeChecker.isReference(schema))
417
- union.push({
418
- ...schema,
419
- $ref: schema.$ref.replace(
420
- "#/definitions/",
421
- "#/components/schemas/",
422
- ),
423
- examples: schema.examples
424
- ? Object.fromEntries(schema.examples.map((v, i) => [`v${i}`, v]))
425
- : undefined,
426
- });
427
- else
428
- union.push({
429
- ...schema,
430
- examples: schema.examples
431
- ? Object.fromEntries(schema.examples.map((v, i) => [`v${i}`, v]))
432
- : undefined,
433
- });
434
- };
435
-
436
- visit(input);
437
- if (
438
- nullable.value === true &&
439
- !union.some((e) => (e as OpenApi.IJsonSchema.INull).type === "null")
440
- )
441
- union.push({
442
- type: "null",
443
- default: nullable.default,
444
- });
445
- if (
446
- union.length === 2 &&
447
- union.filter((x) => OpenApiTypeChecker.isNull(x)).length === 1
448
- ) {
449
- const type: OpenApi.IJsonSchema = union.filter(
450
- (x) => OpenApiTypeChecker.isNull(x) === false,
451
- )[0]!;
452
- for (const key of [
453
- "title",
454
- "description",
455
- "deprecated",
456
- "example",
457
- "examples",
458
- ] as const)
459
- if (type[key] !== undefined) delete type[key];
460
- }
461
- return {
462
- ...(union.length === 0
463
- ? { type: undefined }
464
- : union.length === 1
465
- ? { ...union[0] }
466
- : { oneOf: union.map((u) => ({ ...u, "x-nullable": undefined })) }),
467
- ...attribute,
468
- ...{ "x-nullable": undefined },
469
- };
470
- };
471
-
472
- const convertAllOfSchema =
473
- (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
474
- (input: SwaggerV2.IJsonSchema.IAllOf): OpenApi.IJsonSchema => {
475
- const objects: Array<SwaggerV2.IJsonSchema.IObject | null> =
476
- input.allOf.map((schema) => retrieveObject(definitions)(schema));
477
- if (objects.some((obj) => obj === null))
478
- return {
479
- type: undefined,
480
- ...{
481
- allOf: undefined,
482
- },
483
- };
484
- return {
485
- ...input,
486
- type: "object",
487
- properties: Object.fromEntries(
488
- objects
489
- .map((o) => Object.entries(o?.properties ?? {}))
490
- .flat()
491
- .map(
492
- ([key, value]) =>
493
- [key, convertSchema(definitions)(value)] as const,
494
- ),
495
- ),
496
- ...{
497
- allOf: undefined,
498
- required: [...new Set(objects.map((o) => o?.required ?? []).flat())],
499
- },
500
- };
501
- };
502
-
503
- const retrieveObject =
504
- (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
505
- (
506
- input: SwaggerV2.IJsonSchema,
507
- visited: Set<SwaggerV2.IJsonSchema> = new Set(),
508
- ): SwaggerV2.IJsonSchema.IObject | null => {
509
- if (SwaggerV2TypeChecker.isObject(input))
510
- return input.properties !== undefined && !input.additionalProperties
511
- ? input
512
- : null;
513
- else if (visited.has(input)) return null;
514
- else visited.add(input);
515
-
516
- if (SwaggerV2TypeChecker.isReference(input))
517
- return retrieveObject(definitions)(
518
- definitions?.[input.$ref.split("/").pop() ?? ""] ?? {},
519
- visited,
520
- );
521
- return null;
522
- };
523
- }
1
+ import { IJsonSchemaAttribute, OpenApi, SwaggerV2 } from "@typia/interface";
2
+
3
+ import { OpenApiTypeChecker } from "../../validators/OpenApiTypeChecker";
4
+ import { SwaggerV2TypeChecker } from "../../validators/SwaggerV2TypeChecker";
5
+ import { OpenApiExclusiveEmender } from "./OpenApiExclusiveEmender";
6
+
7
+ export namespace SwaggerV2Upgrader {
8
+ export const convert = (input: SwaggerV2.IDocument): OpenApi.IDocument => ({
9
+ openapi: "3.1.0",
10
+ info: input.info,
11
+ components: convertComponents(input),
12
+ paths: input.paths
13
+ ? Object.fromEntries(
14
+ Object.entries(input.paths)
15
+ .filter(([_, v]) => v !== undefined)
16
+ .map(
17
+ ([key, value]) => [key, convertPathItem(input)(value)] as const,
18
+ ),
19
+ )
20
+ : undefined,
21
+ servers: input.host
22
+ ? [
23
+ {
24
+ url: input.host,
25
+ },
26
+ ]
27
+ : undefined,
28
+ security: input.security,
29
+ tags: input.tags,
30
+ "x-samchon-emended-v4": true,
31
+ });
32
+
33
+ /* -----------------------------------------------------------
34
+ OPERATORS
35
+ ----------------------------------------------------------- */
36
+ const convertPathItem =
37
+ (doc: SwaggerV2.IDocument) =>
38
+ (pathItem: SwaggerV2.IPath): OpenApi.IPath => ({
39
+ ...(pathItem as any),
40
+ ...(pathItem.get
41
+ ? { get: convertOperation(doc)(pathItem)(pathItem.get) }
42
+ : undefined),
43
+ ...(pathItem.put
44
+ ? { put: convertOperation(doc)(pathItem)(pathItem.put) }
45
+ : undefined),
46
+ ...(pathItem.post
47
+ ? { post: convertOperation(doc)(pathItem)(pathItem.post) }
48
+ : undefined),
49
+ ...(pathItem.delete
50
+ ? { delete: convertOperation(doc)(pathItem)(pathItem.delete) }
51
+ : undefined),
52
+ ...(pathItem.options
53
+ ? { options: convertOperation(doc)(pathItem)(pathItem.options) }
54
+ : undefined),
55
+ ...(pathItem.head
56
+ ? { head: convertOperation(doc)(pathItem)(pathItem.head) }
57
+ : undefined),
58
+ ...(pathItem.patch
59
+ ? { patch: convertOperation(doc)(pathItem)(pathItem.patch) }
60
+ : undefined),
61
+ ...(pathItem.trace
62
+ ? { trace: convertOperation(doc)(pathItem)(pathItem.trace) }
63
+ : undefined),
64
+ });
65
+
66
+ const convertOperation =
67
+ (doc: SwaggerV2.IDocument) =>
68
+ (pathItem: SwaggerV2.IPath) =>
69
+ (input: SwaggerV2.IOperation): OpenApi.IOperation => ({
70
+ ...input,
71
+ parameters:
72
+ pathItem.parameters !== undefined || input.parameters !== undefined
73
+ ? (
74
+ [...(pathItem.parameters ?? []), ...(input.parameters ?? [])]
75
+ .map((p) =>
76
+ SwaggerV2TypeChecker.isReference(p)
77
+ ? doc.parameters?.[p.$ref.split("/").pop() ?? ""]!
78
+ : p,
79
+ )
80
+ .filter(
81
+ (p) =>
82
+ p !== undefined &&
83
+ p.in !== "body" &&
84
+ (p as SwaggerV2.IOperation.IBodyParameter).schema ===
85
+ undefined,
86
+ ) as SwaggerV2.IOperation.IGeneralParameter[]
87
+ ).map(convertParameter(doc.definitions ?? {}))
88
+ : undefined,
89
+ requestBody: (() => {
90
+ const found: SwaggerV2.IOperation.IBodyParameter | undefined =
91
+ input.parameters?.find((p) => {
92
+ if (SwaggerV2TypeChecker.isReference(p))
93
+ p = doc.parameters?.[p.$ref.split("/").pop() ?? ""]!;
94
+ return (
95
+ (p as SwaggerV2.IOperation.IBodyParameter)?.schema !== undefined
96
+ );
97
+ }) as SwaggerV2.IOperation.IBodyParameter | undefined;
98
+ return found
99
+ ? convertRequestBody(doc.definitions ?? {})(found)
100
+ : undefined;
101
+ })(),
102
+ responses: input.responses
103
+ ? Object.fromEntries(
104
+ Object.entries(input.responses)
105
+ .filter(([_, v]) => v !== undefined)
106
+ .map(
107
+ ([key, value]) => [key, convertResponse(doc)(value)!] as const,
108
+ )
109
+ .filter(([_, v]) => v !== undefined),
110
+ )
111
+ : undefined,
112
+ });
113
+
114
+ const convertParameter =
115
+ (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
116
+ (
117
+ input: SwaggerV2.IOperation.IGeneralParameter,
118
+ ): OpenApi.IOperation.IParameter => ({
119
+ name: input.name,
120
+ in: input.in as any,
121
+ description: input.description,
122
+ schema: convertSchema(definitions)(input),
123
+ required: true,
124
+ });
125
+ const convertRequestBody =
126
+ (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
127
+ (
128
+ input: SwaggerV2.IOperation.IBodyParameter,
129
+ ): OpenApi.IOperation.IRequestBody => ({
130
+ description: input.description,
131
+ content: {
132
+ "application/json": {
133
+ schema: convertSchema(definitions)(input.schema),
134
+ },
135
+ },
136
+ });
137
+
138
+ const convertResponse =
139
+ (doc: SwaggerV2.IDocument) =>
140
+ (
141
+ input:
142
+ | SwaggerV2.IOperation.IResponse
143
+ | SwaggerV2.IJsonSchema.IReference<`#/definitions/responses/${string}`>,
144
+ ): OpenApi.IOperation.IResponse | undefined => {
145
+ if (SwaggerV2TypeChecker.isReference(input)) {
146
+ const found: SwaggerV2.IOperation.IResponse | undefined =
147
+ doc.responses?.[input.$ref.split("/").pop() ?? ""]!;
148
+ if (found === undefined) return undefined;
149
+ input = found;
150
+ }
151
+ return {
152
+ description: input.description,
153
+ content: input.schema
154
+ ? {
155
+ "application/json": {
156
+ schema: convertSchema(doc.definitions ?? {})(input.schema),
157
+ example: input.example,
158
+ },
159
+ }
160
+ : undefined,
161
+ headers: input.headers
162
+ ? Object.fromEntries(
163
+ Object.entries(input.headers)
164
+ .filter(([_, v]) => v !== undefined)
165
+ .map(
166
+ ([key, value]) =>
167
+ [
168
+ key,
169
+ {
170
+ schema: convertSchema(doc.definitions ?? {})(value),
171
+ in: "header",
172
+ },
173
+ ] as const,
174
+ ),
175
+ )
176
+ : undefined,
177
+ };
178
+ };
179
+
180
+ /* -----------------------------------------------------------
181
+ DEFINITIONS
182
+ ----------------------------------------------------------- */
183
+ export const convertComponents = (
184
+ input: SwaggerV2.IDocument,
185
+ ): OpenApi.IComponents => ({
186
+ schemas: Object.fromEntries(
187
+ Object.entries(input.definitions ?? {})
188
+ .filter(([_, v]) => v !== undefined)
189
+ .map(([key, value]) => [
190
+ key,
191
+ convertSchema(input.definitions ?? {})(value),
192
+ ]),
193
+ ),
194
+ securitySchemes: input.securityDefinitions
195
+ ? Object.fromEntries(
196
+ Object.entries(input.securityDefinitions)
197
+ .filter(([_, v]) => v !== undefined)
198
+ .map(([key, value]) => [key, convertSecurityScheme(value)])
199
+ .filter(([_, v]) => v !== undefined),
200
+ )
201
+ : undefined,
202
+ });
203
+
204
+ const convertSecurityScheme = (
205
+ input: SwaggerV2.ISecurityDefinition,
206
+ ): OpenApi.ISecurityScheme => {
207
+ if (input.type === "apiKey") return input;
208
+ else if (input.type === "basic")
209
+ return {
210
+ type: "http",
211
+ scheme: "basic",
212
+ description: input.description,
213
+ };
214
+ else if (input.type === "oauth2")
215
+ if (input.flow === "implicit")
216
+ return {
217
+ type: "oauth2",
218
+ description: input.description,
219
+ flows: {
220
+ implicit: {
221
+ authorizationUrl: input.authorizationUrl,
222
+ scopes: input.scopes,
223
+ },
224
+ },
225
+ };
226
+ else if (input.flow === "accessCode")
227
+ return {
228
+ type: "oauth2",
229
+ description: input.description,
230
+ flows: {
231
+ authorizationCode: {
232
+ authorizationUrl: input.authorizationUrl,
233
+ tokenUrl: input.tokenUrl,
234
+ scopes: input.scopes,
235
+ },
236
+ },
237
+ };
238
+ else if (input.flow === "password")
239
+ return {
240
+ type: "oauth2",
241
+ description: input.description,
242
+ flows: {
243
+ password: {
244
+ tokenUrl: input.tokenUrl,
245
+ scopes: input.scopes,
246
+ },
247
+ },
248
+ };
249
+ else if (input.flow === "application")
250
+ return {
251
+ type: "oauth2",
252
+ description: input.description,
253
+ flows: {
254
+ clientCredentials: {
255
+ tokenUrl: input.tokenUrl,
256
+ scopes: input.scopes,
257
+ },
258
+ },
259
+ };
260
+ else return undefined!;
261
+ return undefined!;
262
+ };
263
+
264
+ export const convertSchema =
265
+ (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
266
+ (input: SwaggerV2.IJsonSchema): OpenApi.IJsonSchema => {
267
+ const nullable: { value: boolean; default?: null } = {
268
+ value: false,
269
+ default: undefined,
270
+ };
271
+ const union: OpenApi.IJsonSchema[] = [];
272
+ const attribute: IJsonSchemaAttribute = {
273
+ title: input.title,
274
+ description: input.description,
275
+ deprecated: input.deprecated,
276
+ readOnly: input.readOnly,
277
+ example: input.example,
278
+ examples: input.examples
279
+ ? Object.fromEntries(input.examples.map((v, i) => [`v${i}`, v]))
280
+ : undefined,
281
+ ...Object.fromEntries(
282
+ Object.entries(input).filter(
283
+ ([key, value]) => key.startsWith("x-") && value !== undefined,
284
+ ),
285
+ ),
286
+ };
287
+ const visit = (schema: SwaggerV2.IJsonSchema): void => {
288
+ // NULLABLE PROPERTY
289
+ if (
290
+ (schema as SwaggerV2.IJsonSchema.__ISignificant<any>)[
291
+ "x-nullable"
292
+ ] === true
293
+ ) {
294
+ nullable.value ||= true;
295
+ if ((schema as SwaggerV2.IJsonSchema.INumber).default === null)
296
+ nullable.default = null;
297
+ }
298
+ if (
299
+ Array.isArray((schema as SwaggerV2.IJsonSchema.INumber).enum) &&
300
+ (schema as SwaggerV2.IJsonSchema.INumber).enum?.length &&
301
+ (schema as SwaggerV2.IJsonSchema.INumber).enum?.some(
302
+ (e) => e === null,
303
+ )
304
+ )
305
+ nullable.value ||= true;
306
+ // UNION TYPE CASE
307
+ if (SwaggerV2TypeChecker.isAnyOf(schema))
308
+ schema["x-anyOf"].forEach(visit);
309
+ else if (SwaggerV2TypeChecker.isOneOf(schema))
310
+ schema["x-oneOf"].forEach(visit);
311
+ else if (SwaggerV2TypeChecker.isAllOf(schema))
312
+ if (schema.allOf.length === 1) visit(schema.allOf[0]!);
313
+ else union.push(convertAllOfSchema(definitions)(schema));
314
+ // ATOMIC TYPE CASE (CONSIDER ENUM VALUES)
315
+ else if (
316
+ SwaggerV2TypeChecker.isBoolean(schema) ||
317
+ SwaggerV2TypeChecker.isInteger(schema) ||
318
+ SwaggerV2TypeChecker.isNumber(schema) ||
319
+ SwaggerV2TypeChecker.isString(schema)
320
+ )
321
+ if (
322
+ schema.enum?.length &&
323
+ schema.enum.filter((e) => e !== null).length
324
+ )
325
+ union.push(
326
+ ...schema.enum
327
+ .filter((v) => v !== null)
328
+ .map((value) => ({ const: value })),
329
+ );
330
+ else if (
331
+ SwaggerV2TypeChecker.isInteger(schema) ||
332
+ SwaggerV2TypeChecker.isNumber(schema)
333
+ )
334
+ union.push(
335
+ OpenApiExclusiveEmender.emend({
336
+ ...schema,
337
+ default: (schema.default ?? undefined) satisfies
338
+ | boolean
339
+ | number
340
+ | string
341
+ | undefined as any,
342
+ examples: schema.examples
343
+ ? Object.fromEntries(
344
+ schema.examples.map((v, i) => [`v${i}`, v]),
345
+ )
346
+ : undefined,
347
+ exclusiveMinimum:
348
+ typeof schema.exclusiveMinimum === "boolean"
349
+ ? schema.exclusiveMinimum === true
350
+ ? schema.minimum
351
+ : undefined
352
+ : schema.exclusiveMinimum,
353
+ exclusiveMaximum:
354
+ typeof schema.exclusiveMaximum === "boolean"
355
+ ? schema.exclusiveMaximum === true
356
+ ? schema.maximum
357
+ : undefined
358
+ : schema.exclusiveMaximum,
359
+ minimum:
360
+ schema.exclusiveMinimum === true ? undefined : schema.minimum,
361
+ maximum:
362
+ schema.exclusiveMaximum === true ? undefined : schema.maximum,
363
+ ...{ enum: undefined },
364
+ }),
365
+ );
366
+ else
367
+ union.push({
368
+ ...schema,
369
+ default: (schema.default ?? undefined) satisfies
370
+ | boolean
371
+ | number
372
+ | string
373
+ | undefined as any,
374
+ examples: schema.examples
375
+ ? Object.fromEntries(
376
+ schema.examples.map((v, i) => [`v${i}`, v]),
377
+ )
378
+ : undefined,
379
+ ...{ enum: undefined },
380
+ });
381
+ // INSTANCE TYPE CASE
382
+ else if (SwaggerV2TypeChecker.isArray(schema))
383
+ union.push({
384
+ ...schema,
385
+ items: convertSchema(definitions)(schema.items),
386
+ examples: schema.examples
387
+ ? Object.fromEntries(schema.examples.map((v, i) => [`v${i}`, v]))
388
+ : undefined,
389
+ });
390
+ else if (SwaggerV2TypeChecker.isObject(schema))
391
+ union.push({
392
+ ...schema,
393
+ ...{
394
+ properties: schema.properties
395
+ ? Object.fromEntries(
396
+ Object.entries(schema.properties)
397
+ .filter(([_, v]) => v !== undefined)
398
+ .map(([key, value]) => [
399
+ key,
400
+ convertSchema(definitions)(value),
401
+ ]),
402
+ )
403
+ : {},
404
+ additionalProperties: schema.additionalProperties
405
+ ? typeof schema.additionalProperties === "object" &&
406
+ schema.additionalProperties !== null
407
+ ? convertSchema(definitions)(schema.additionalProperties)
408
+ : schema.additionalProperties
409
+ : undefined,
410
+ },
411
+ examples: schema.examples
412
+ ? Object.fromEntries(schema.examples.map((v, i) => [`v${i}`, v]))
413
+ : undefined,
414
+ required: schema.required ?? [],
415
+ });
416
+ else if (SwaggerV2TypeChecker.isReference(schema))
417
+ union.push({
418
+ ...schema,
419
+ $ref: schema.$ref.replace(
420
+ "#/definitions/",
421
+ "#/components/schemas/",
422
+ ),
423
+ examples: schema.examples
424
+ ? Object.fromEntries(schema.examples.map((v, i) => [`v${i}`, v]))
425
+ : undefined,
426
+ });
427
+ else
428
+ union.push({
429
+ ...schema,
430
+ examples: schema.examples
431
+ ? Object.fromEntries(schema.examples.map((v, i) => [`v${i}`, v]))
432
+ : undefined,
433
+ });
434
+ };
435
+
436
+ visit(input);
437
+ if (
438
+ nullable.value === true &&
439
+ !union.some((e) => (e as OpenApi.IJsonSchema.INull).type === "null")
440
+ )
441
+ union.push({
442
+ type: "null",
443
+ default: nullable.default,
444
+ });
445
+ if (
446
+ union.length === 2 &&
447
+ union.filter((x) => OpenApiTypeChecker.isNull(x)).length === 1
448
+ ) {
449
+ const type: OpenApi.IJsonSchema = union.filter(
450
+ (x) => OpenApiTypeChecker.isNull(x) === false,
451
+ )[0]!;
452
+ for (const key of [
453
+ "title",
454
+ "description",
455
+ "deprecated",
456
+ "example",
457
+ "examples",
458
+ ] as const)
459
+ if (type[key] !== undefined) delete type[key];
460
+ }
461
+ return {
462
+ ...(union.length === 0
463
+ ? { type: undefined }
464
+ : union.length === 1
465
+ ? { ...union[0] }
466
+ : { oneOf: union.map((u) => ({ ...u, "x-nullable": undefined })) }),
467
+ ...attribute,
468
+ ...{ "x-nullable": undefined },
469
+ };
470
+ };
471
+
472
+ const convertAllOfSchema =
473
+ (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
474
+ (input: SwaggerV2.IJsonSchema.IAllOf): OpenApi.IJsonSchema => {
475
+ const objects: Array<SwaggerV2.IJsonSchema.IObject | null> =
476
+ input.allOf.map((schema) => retrieveObject(definitions)(schema));
477
+ if (objects.some((obj) => obj === null))
478
+ return {
479
+ type: undefined,
480
+ ...{
481
+ allOf: undefined,
482
+ },
483
+ };
484
+ return {
485
+ ...input,
486
+ type: "object",
487
+ properties: Object.fromEntries(
488
+ objects
489
+ .map((o) => Object.entries(o?.properties ?? {}))
490
+ .flat()
491
+ .map(
492
+ ([key, value]) =>
493
+ [key, convertSchema(definitions)(value)] as const,
494
+ ),
495
+ ),
496
+ ...{
497
+ allOf: undefined,
498
+ required: [...new Set(objects.map((o) => o?.required ?? []).flat())],
499
+ },
500
+ };
501
+ };
502
+
503
+ const retrieveObject =
504
+ (definitions: Record<string, SwaggerV2.IJsonSchema>) =>
505
+ (
506
+ input: SwaggerV2.IJsonSchema,
507
+ visited: Set<SwaggerV2.IJsonSchema> = new Set(),
508
+ ): SwaggerV2.IJsonSchema.IObject | null => {
509
+ if (SwaggerV2TypeChecker.isObject(input))
510
+ return input.properties !== undefined && !input.additionalProperties
511
+ ? input
512
+ : null;
513
+ else if (visited.has(input)) return null;
514
+ else visited.add(input);
515
+
516
+ if (SwaggerV2TypeChecker.isReference(input))
517
+ return retrieveObject(definitions)(
518
+ definitions?.[input.$ref.split("/").pop() ?? ""] ?? {},
519
+ visited,
520
+ );
521
+ return null;
522
+ };
523
+ }