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

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 (67) hide show
  1. package/lib/converters/LlmSchemaConverter.d.ts +0 -1
  2. package/lib/converters/LlmSchemaConverter.js +4 -31
  3. package/lib/converters/LlmSchemaConverter.js.map +1 -1
  4. package/lib/converters/LlmSchemaConverter.mjs +2 -32
  5. package/lib/converters/LlmSchemaConverter.mjs.map +1 -1
  6. package/lib/converters/OpenApiConverter.d.ts +29 -16
  7. package/lib/converters/OpenApiConverter.js +25 -15
  8. package/lib/converters/OpenApiConverter.js.map +1 -1
  9. package/lib/converters/OpenApiConverter.mjs +25 -15
  10. package/lib/converters/OpenApiConverter.mjs.map +1 -1
  11. package/lib/converters/internal/OpenApiV3Downgrader.js +38 -18
  12. package/lib/converters/internal/OpenApiV3Downgrader.js.map +1 -1
  13. package/lib/converters/internal/OpenApiV3Downgrader.mjs +50 -27
  14. package/lib/converters/internal/OpenApiV3Downgrader.mjs.map +1 -1
  15. package/lib/converters/internal/OpenApiV3Upgrader.js +36 -18
  16. package/lib/converters/internal/OpenApiV3Upgrader.js.map +1 -1
  17. package/lib/converters/internal/OpenApiV3Upgrader.mjs +50 -29
  18. package/lib/converters/internal/OpenApiV3Upgrader.mjs.map +1 -1
  19. package/lib/converters/internal/OpenApiV3_1Upgrader.js +38 -20
  20. package/lib/converters/internal/OpenApiV3_1Upgrader.js.map +1 -1
  21. package/lib/converters/internal/OpenApiV3_1Upgrader.mjs +51 -29
  22. package/lib/converters/internal/OpenApiV3_1Upgrader.mjs.map +1 -1
  23. package/lib/converters/internal/OpenApiV3_2Upgrader.d.ts +16 -0
  24. package/lib/converters/internal/OpenApiV3_2Upgrader.js +204 -0
  25. package/lib/converters/internal/OpenApiV3_2Upgrader.js.map +1 -0
  26. package/lib/converters/internal/OpenApiV3_2Upgrader.mjs +243 -0
  27. package/lib/converters/internal/OpenApiV3_2Upgrader.mjs.map +1 -0
  28. package/lib/converters/internal/SwaggerV2Downgrader.js +38 -18
  29. package/lib/converters/internal/SwaggerV2Downgrader.js.map +1 -1
  30. package/lib/converters/internal/SwaggerV2Downgrader.mjs +50 -27
  31. package/lib/converters/internal/SwaggerV2Downgrader.mjs.map +1 -1
  32. package/lib/converters/internal/SwaggerV2Upgrader.js +37 -19
  33. package/lib/converters/internal/SwaggerV2Upgrader.js.map +1 -1
  34. package/lib/converters/internal/SwaggerV2Upgrader.mjs +50 -29
  35. package/lib/converters/internal/SwaggerV2Upgrader.mjs.map +1 -1
  36. package/lib/http/HttpLlm.js +4 -5
  37. package/lib/http/HttpLlm.js.map +1 -1
  38. package/lib/http/HttpLlm.mjs +0 -1
  39. package/lib/http/HttpLlm.mjs.map +1 -1
  40. package/lib/http/internal/HttpLlmApplicationComposer.js +3 -4
  41. package/lib/http/internal/HttpLlmApplicationComposer.js.map +1 -1
  42. package/lib/http/internal/HttpLlmApplicationComposer.mjs +0 -1
  43. package/lib/http/internal/HttpLlmApplicationComposer.mjs.map +1 -1
  44. package/lib/http/internal/HttpMigrateApplicationComposer.js +1 -1
  45. package/lib/http/internal/HttpMigrateApplicationComposer.js.map +1 -1
  46. package/lib/http/internal/HttpMigrateApplicationComposer.mjs +1 -1
  47. package/lib/http/internal/HttpMigrateApplicationComposer.mjs.map +1 -1
  48. package/lib/http/internal/HttpMigrateRouteComposer.d.ts +1 -1
  49. package/lib/utils/LlmJson.d.ts +19 -1
  50. package/lib/utils/LlmJson.js +27 -0
  51. package/lib/utils/LlmJson.js.map +1 -1
  52. package/lib/utils/LlmJson.mjs +27 -0
  53. package/lib/utils/LlmJson.mjs.map +1 -1
  54. package/package.json +2 -2
  55. package/src/converters/LlmSchemaConverter.ts +1 -31
  56. package/src/converters/OpenApiConverter.ts +322 -285
  57. package/src/converters/internal/OpenApiV3Downgrader.ts +381 -355
  58. package/src/converters/internal/OpenApiV3Upgrader.ts +494 -470
  59. package/src/converters/internal/OpenApiV3_1Upgrader.ts +710 -685
  60. package/src/converters/internal/OpenApiV3_2Upgrader.ts +342 -0
  61. package/src/converters/internal/SwaggerV2Downgrader.ts +450 -424
  62. package/src/converters/internal/SwaggerV2Upgrader.ts +547 -523
  63. package/src/http/HttpLlm.ts +0 -1
  64. package/src/http/internal/HttpLlmApplicationComposer.ts +0 -1
  65. package/src/http/internal/HttpMigrateApplicationComposer.ts +56 -56
  66. package/src/http/internal/HttpMigrateRouteComposer.ts +505 -505
  67. package/src/utils/LlmJson.ts +173 -141
@@ -1,355 +1,381 @@
1
- import { OpenApi, OpenApiV3 } from "@typia/interface";
2
-
3
- import { OpenApiTypeChecker } from "../../validators/OpenApiTypeChecker";
4
-
5
- export namespace OpenApiV3Downgrader {
6
- export interface IComponentsCollection {
7
- original: OpenApi.IComponents;
8
- downgraded: OpenApiV3.IComponents;
9
- }
10
-
11
- export const downgrade = (input: OpenApi.IDocument): OpenApiV3.IDocument => {
12
- const collection: IComponentsCollection = downgradeComponents(
13
- input.components,
14
- );
15
- return {
16
- openapi: "3.0.0",
17
- servers: input.servers,
18
- info: input.info,
19
- components: collection.downgraded,
20
- paths: input.paths
21
- ? Object.fromEntries(
22
- Object.entries(input.paths)
23
- .filter(([_, v]) => v !== undefined)
24
- .map(
25
- ([key, value]) =>
26
- [key, downgradePathItem(collection)(value)] as const,
27
- ),
28
- )
29
- : undefined,
30
- security: input.security,
31
- tags: input.tags,
32
- };
33
- };
34
-
35
- /* -----------------------------------------------------------
36
- OPERATORS
37
- ----------------------------------------------------------- */
38
- const downgradePathItem =
39
- (collection: IComponentsCollection) =>
40
- (pathItem: OpenApi.IPath): OpenApiV3.IPath => ({
41
- ...(pathItem as any),
42
- ...(pathItem.get
43
- ? { get: downgradeOperation(collection)(pathItem.get) }
44
- : undefined),
45
- ...(pathItem.put
46
- ? { put: downgradeOperation(collection)(pathItem.put) }
47
- : undefined),
48
- ...(pathItem.post
49
- ? { post: downgradeOperation(collection)(pathItem.post) }
50
- : undefined),
51
- ...(pathItem.delete
52
- ? { delete: downgradeOperation(collection)(pathItem.delete) }
53
- : undefined),
54
- ...(pathItem.options
55
- ? { options: downgradeOperation(collection)(pathItem.options) }
56
- : undefined),
57
- ...(pathItem.head
58
- ? { head: downgradeOperation(collection)(pathItem.head) }
59
- : undefined),
60
- ...(pathItem.patch
61
- ? { patch: downgradeOperation(collection)(pathItem.patch) }
62
- : undefined),
63
- ...(pathItem.trace
64
- ? { trace: downgradeOperation(collection)(pathItem.trace) }
65
- : undefined),
66
- });
67
-
68
- const downgradeOperation =
69
- (collection: IComponentsCollection) =>
70
- (input: OpenApi.IOperation): OpenApiV3.IOperation => ({
71
- ...input,
72
- parameters: input.parameters
73
- ? input.parameters.map(downgradeParameter(collection))
74
- : undefined,
75
- requestBody: input.requestBody
76
- ? downgradeRequestBody(collection)(input.requestBody)
77
- : undefined,
78
- responses: input.responses
79
- ? Object.fromEntries(
80
- Object.entries(input.responses)
81
- .filter(([_, v]) => v !== undefined)
82
- .map(([key, value]) => [
83
- key,
84
- downgradeResponse(collection)(value),
85
- ]),
86
- )
87
- : undefined,
88
- });
89
-
90
- const downgradeParameter =
91
- (collection: IComponentsCollection) =>
92
- (
93
- input: OpenApi.IOperation.IParameter,
94
- ): OpenApiV3.IOperation.IParameter => ({
95
- ...input,
96
- schema: downgradeSchema(collection)(input.schema),
97
- });
98
-
99
- const downgradeRequestBody =
100
- (collection: IComponentsCollection) =>
101
- (
102
- input: OpenApi.IOperation.IRequestBody,
103
- ): OpenApiV3.IOperation.IRequestBody => ({
104
- ...input,
105
- content: input.content
106
- ? downgradeContent(collection)(input.content)
107
- : undefined,
108
- });
109
-
110
- const downgradeResponse =
111
- (collection: IComponentsCollection) =>
112
- (input: OpenApi.IOperation.IResponse): OpenApiV3.IOperation.IResponse => ({
113
- ...input,
114
- content: input.content
115
- ? downgradeContent(collection)(input.content)
116
- : undefined,
117
- headers: input.headers
118
- ? Object.fromEntries(
119
- Object.entries(input.headers)
120
- .filter(([_, v]) => v !== undefined)
121
- .map(([key, value]) => [
122
- key,
123
- {
124
- ...value,
125
- schema: downgradeSchema(collection)(value.schema),
126
- },
127
- ]),
128
- )
129
- : undefined,
130
- });
131
-
132
- const downgradeContent =
133
- (collection: IComponentsCollection) =>
134
- (
135
- record: OpenApi.IOperation.IContent,
136
- ): Record<string, OpenApiV3.IOperation.IMediaType> =>
137
- Object.fromEntries(
138
- Object.entries(record)
139
- .filter(([_, v]) => v !== undefined)
140
- .map(
141
- ([key, value]) =>
142
- [
143
- key,
144
- {
145
- ...value,
146
- schema: value?.schema
147
- ? downgradeSchema(collection)(value.schema)
148
- : undefined,
149
- },
150
- ] as const,
151
- ),
152
- );
153
-
154
- /* -----------------------------------------------------------
155
- DEFINITIONS
156
- ----------------------------------------------------------- */
157
- export const downgradeComponents = (
158
- input: OpenApi.IComponents,
159
- ): IComponentsCollection => {
160
- const collection: IComponentsCollection = {
161
- original: input,
162
- downgraded: {
163
- securitySchemes: input.securitySchemes,
164
- },
165
- };
166
- if (input.schemas) {
167
- collection.downgraded.schemas = {};
168
- for (const [key, value] of Object.entries(input.schemas))
169
- if (value !== undefined)
170
- collection.downgraded.schemas[key] =
171
- downgradeSchema(collection)(value);
172
- }
173
- return collection;
174
- };
175
-
176
- export const downgradeSchema =
177
- (collection: IComponentsCollection) =>
178
- (input: OpenApi.IJsonSchema): OpenApiV3.IJsonSchema => {
179
- const nullable: boolean = isNullable(new Set())(collection.original)(
180
- input,
181
- );
182
- const union: OpenApiV3.IJsonSchema[] = [];
183
- const attribute: OpenApiV3.IJsonSchema.__IAttribute = {
184
- title: input.title,
185
- description: input.description,
186
- deprecated: input.deprecated,
187
- readOnly: input.readOnly,
188
- writeOnly: input.writeOnly,
189
- example: input.example,
190
- examples: input.examples,
191
- ...Object.fromEntries(
192
- Object.entries(input).filter(
193
- ([key, value]) => key.startsWith("x-") && value !== undefined,
194
- ),
195
- ),
196
- };
197
- const visit = (schema: OpenApi.IJsonSchema): void => {
198
- if (OpenApiTypeChecker.isBoolean(schema))
199
- union.push({ type: "boolean" });
200
- else if (
201
- OpenApiTypeChecker.isBoolean(schema) ||
202
- OpenApiTypeChecker.isInteger(schema) ||
203
- OpenApiTypeChecker.isNumber(schema) ||
204
- OpenApiTypeChecker.isString(schema) ||
205
- OpenApiTypeChecker.isReference(schema)
206
- )
207
- union.push({ ...schema });
208
- else if (OpenApiTypeChecker.isArray(schema))
209
- union.push({
210
- ...schema,
211
- items: downgradeSchema(collection)(schema.items),
212
- });
213
- else if (OpenApiTypeChecker.isTuple(schema))
214
- union.push({
215
- ...schema,
216
- items: ((): OpenApiV3.IJsonSchema => {
217
- if (schema.additionalItems === true) return {};
218
- const elements = [
219
- ...schema.prefixItems,
220
- ...(typeof schema.additionalItems === "object"
221
- ? [downgradeSchema(collection)(schema.additionalItems)]
222
- : []),
223
- ];
224
- if (elements.length === 0) return {};
225
- return {
226
- oneOf: elements.map(downgradeSchema(collection) as any),
227
- };
228
- })(),
229
- minItems: schema.prefixItems.length,
230
- maxItems:
231
- !!schema.additionalItems === true
232
- ? undefined
233
- : schema.prefixItems.length,
234
- ...{
235
- prefixItems: undefined,
236
- additionalItems: undefined,
237
- },
238
- });
239
- else if (OpenApiTypeChecker.isObject(schema))
240
- union.push({
241
- ...schema,
242
- properties: schema.properties
243
- ? Object.fromEntries(
244
- Object.entries(schema.properties)
245
- .filter(([_, v]) => v !== undefined)
246
- .map(([key, value]) => [
247
- key,
248
- downgradeSchema(collection)(value),
249
- ]),
250
- )
251
- : undefined,
252
- additionalProperties:
253
- typeof schema.additionalProperties === "object"
254
- ? downgradeSchema(collection)(schema.additionalProperties)
255
- : schema.additionalProperties,
256
- required: schema.required,
257
- });
258
- else if (OpenApiTypeChecker.isOneOf(schema))
259
- schema.oneOf.forEach(visit);
260
- };
261
- const visitConstant = (schema: OpenApi.IJsonSchema): void => {
262
- const insert = (value: any): void => {
263
- const matched: OpenApiV3.IJsonSchema.INumber | undefined = union.find(
264
- (u) => (u as OpenApiV3.IJsonSchema.INumber).type === typeof value,
265
- ) as OpenApiV3.IJsonSchema.INumber | undefined;
266
- if (matched !== undefined) {
267
- matched.enum ??= [];
268
- matched.enum.push(value);
269
- } else union.push({ type: typeof value as "number", enum: [value] });
270
- };
271
- if (OpenApiTypeChecker.isConstant(schema)) insert(schema.const);
272
- else if (OpenApiTypeChecker.isOneOf(schema))
273
- for (const u of schema.oneOf)
274
- if (OpenApiTypeChecker.isConstant(u)) insert(u.const);
275
- };
276
-
277
- visit(input);
278
- visitConstant(input);
279
- if (nullable === true)
280
- for (const u of union)
281
- if (OpenApiTypeChecker.isReference(u as any))
282
- downgradeNullableReference(new Set())(collection)(u as any);
283
- else (u as OpenApiV3.IJsonSchema.IArray).nullable = true;
284
- if (nullable === true && union.length === 0)
285
- return { type: "null", ...attribute };
286
- return {
287
- ...(union.length === 0
288
- ? { type: undefined }
289
- : union.length === 1
290
- ? { ...union[0] }
291
- : { oneOf: union }),
292
- ...attribute,
293
- };
294
- };
295
-
296
- const downgradeNullableReference =
297
- (visited: Set<string>) =>
298
- (collection: IComponentsCollection) =>
299
- (schema: OpenApiV3.IJsonSchema.IReference): void => {
300
- const key: string = schema.$ref.split("/").pop()!;
301
- if (key.endsWith(".Nullable")) return;
302
-
303
- const found: OpenApi.IJsonSchema | undefined =
304
- collection.original.schemas?.[key];
305
- if (found === undefined) return;
306
- else if (isNullable(visited)(collection.original)(found) === true) return;
307
- else if (
308
- collection.downgraded.schemas?.[`${key}.Nullable`] === undefined
309
- ) {
310
- collection.downgraded.schemas ??= {};
311
- collection.downgraded.schemas[`${key}.Nullable`] = {};
312
- collection.downgraded.schemas[`${key}.Nullable`] = downgradeSchema(
313
- collection,
314
- )(
315
- OpenApiTypeChecker.isOneOf(found)
316
- ? {
317
- ...found,
318
- oneOf: [...found.oneOf, { type: "null" }],
319
- }
320
- : {
321
- oneOf: [found, { type: "null" }],
322
- title: found.title,
323
- description: found.description,
324
- example: found.example,
325
- examples: found.examples,
326
- ...Object.fromEntries(
327
- Object.entries(found).filter(
328
- ([key, value]) =>
329
- key.startsWith("x-") && value !== undefined,
330
- ),
331
- ),
332
- },
333
- );
334
- }
335
- schema.$ref += ".Nullable";
336
- };
337
-
338
- const isNullable =
339
- (visited: Set<string>) =>
340
- (components: OpenApi.IComponents) =>
341
- (schema: OpenApi.IJsonSchema): boolean => {
342
- if (OpenApiTypeChecker.isNull(schema)) return true;
343
- else if (OpenApiTypeChecker.isReference(schema)) {
344
- if (visited.has(schema.$ref)) return false;
345
- visited.add(schema.$ref);
346
- const key: string = schema.$ref.split("/").pop()!;
347
- const next: OpenApi.IJsonSchema | undefined = components.schemas?.[key];
348
- return next ? isNullable(visited)(components)(next) : false;
349
- }
350
- return (
351
- OpenApiTypeChecker.isOneOf(schema) &&
352
- schema.oneOf.some(isNullable(visited)(components))
353
- );
354
- };
355
- }
1
+ import { OpenApi, OpenApiV3 } from "@typia/interface";
2
+
3
+ import { OpenApiTypeChecker } from "../../validators/OpenApiTypeChecker";
4
+
5
+ export namespace OpenApiV3Downgrader {
6
+ export interface IComponentsCollection {
7
+ original: OpenApi.IComponents;
8
+ downgraded: OpenApiV3.IComponents;
9
+ }
10
+
11
+ export const downgrade = (input: OpenApi.IDocument): OpenApiV3.IDocument => {
12
+ const collection: IComponentsCollection = downgradeComponents(
13
+ input.components,
14
+ );
15
+ return {
16
+ openapi: "3.0.0",
17
+ servers: input.servers,
18
+ info: input.info,
19
+ components: collection.downgraded,
20
+ paths: input.paths
21
+ ? Object.fromEntries(
22
+ Object.entries(input.paths)
23
+ .filter(([_, v]) => v !== undefined)
24
+ .map(
25
+ ([key, value]) =>
26
+ [key, downgradePathItem(collection)(value)] as const,
27
+ ),
28
+ )
29
+ : undefined,
30
+ security: input.security,
31
+ tags: input.tags,
32
+ };
33
+ };
34
+
35
+ /* -----------------------------------------------------------
36
+ OPERATORS
37
+ ----------------------------------------------------------- */
38
+ const downgradePathItem =
39
+ (collection: IComponentsCollection) =>
40
+ (pathItem: OpenApi.IPath): OpenApiV3.IPath => {
41
+ // Collect non-standard operations for x-additionalOperations
42
+ const xAdditionalOperations: Record<string, OpenApiV3.IOperation> = {};
43
+
44
+ // query method goes to x-additionalOperations
45
+ if (pathItem.query) {
46
+ xAdditionalOperations["query"] = downgradeOperation(collection)(pathItem.query);
47
+ }
48
+
49
+ // additionalOperations also go to x-additionalOperations
50
+ if (pathItem.additionalOperations) {
51
+ for (const [key, value] of Object.entries(pathItem.additionalOperations)) {
52
+ if (value !== undefined) {
53
+ xAdditionalOperations[key] = downgradeOperation(collection)(value);
54
+ }
55
+ }
56
+ }
57
+
58
+ return {
59
+ ...(pathItem as any),
60
+ ...(pathItem.get
61
+ ? { get: downgradeOperation(collection)(pathItem.get) }
62
+ : undefined),
63
+ ...(pathItem.put
64
+ ? { put: downgradeOperation(collection)(pathItem.put) }
65
+ : undefined),
66
+ ...(pathItem.post
67
+ ? { post: downgradeOperation(collection)(pathItem.post) }
68
+ : undefined),
69
+ ...(pathItem.delete
70
+ ? { delete: downgradeOperation(collection)(pathItem.delete) }
71
+ : undefined),
72
+ ...(pathItem.options
73
+ ? { options: downgradeOperation(collection)(pathItem.options) }
74
+ : undefined),
75
+ ...(pathItem.head
76
+ ? { head: downgradeOperation(collection)(pathItem.head) }
77
+ : undefined),
78
+ ...(pathItem.patch
79
+ ? { patch: downgradeOperation(collection)(pathItem.patch) }
80
+ : undefined),
81
+ ...(pathItem.trace
82
+ ? { trace: downgradeOperation(collection)(pathItem.trace) }
83
+ : undefined),
84
+ ...(Object.keys(xAdditionalOperations).length > 0
85
+ ? { "x-additionalOperations": xAdditionalOperations }
86
+ : undefined),
87
+ // Remove v3.2-only properties from spread
88
+ query: undefined,
89
+ additionalOperations: undefined,
90
+ };
91
+ };
92
+
93
+ const downgradeOperation =
94
+ (collection: IComponentsCollection) =>
95
+ (input: OpenApi.IOperation): OpenApiV3.IOperation => ({
96
+ ...input,
97
+ parameters: input.parameters
98
+ ? input.parameters.map(downgradeParameter(collection))
99
+ : undefined,
100
+ requestBody: input.requestBody
101
+ ? downgradeRequestBody(collection)(input.requestBody)
102
+ : undefined,
103
+ responses: input.responses
104
+ ? Object.fromEntries(
105
+ Object.entries(input.responses)
106
+ .filter(([_, v]) => v !== undefined)
107
+ .map(([key, value]) => [
108
+ key,
109
+ downgradeResponse(collection)(value),
110
+ ]),
111
+ )
112
+ : undefined,
113
+ });
114
+
115
+ const downgradeParameter =
116
+ (collection: IComponentsCollection) =>
117
+ (
118
+ input: OpenApi.IOperation.IParameter,
119
+ ): OpenApiV3.IOperation.IParameter => ({
120
+ ...input,
121
+ in: input.in === "querystring" ? "query" : input.in,
122
+ schema: downgradeSchema(collection)(input.schema),
123
+ });
124
+
125
+ const downgradeRequestBody =
126
+ (collection: IComponentsCollection) =>
127
+ (
128
+ input: OpenApi.IOperation.IRequestBody,
129
+ ): OpenApiV3.IOperation.IRequestBody => ({
130
+ ...input,
131
+ content: input.content
132
+ ? downgradeContent(collection)(input.content)
133
+ : undefined,
134
+ });
135
+
136
+ const downgradeResponse =
137
+ (collection: IComponentsCollection) =>
138
+ (input: OpenApi.IOperation.IResponse): OpenApiV3.IOperation.IResponse => ({
139
+ ...input,
140
+ content: input.content
141
+ ? downgradeContent(collection)(input.content)
142
+ : undefined,
143
+ headers: input.headers
144
+ ? Object.fromEntries(
145
+ Object.entries(input.headers)
146
+ .filter(([_, v]) => v !== undefined)
147
+ .map(([key, value]) => [
148
+ key,
149
+ {
150
+ ...value,
151
+ schema: downgradeSchema(collection)(value.schema),
152
+ },
153
+ ]),
154
+ )
155
+ : undefined,
156
+ });
157
+
158
+ const downgradeContent =
159
+ (collection: IComponentsCollection) =>
160
+ (
161
+ record: OpenApi.IOperation.IContent,
162
+ ): Record<string, OpenApiV3.IOperation.IMediaType> =>
163
+ Object.fromEntries(
164
+ Object.entries(record)
165
+ .filter(([_, v]) => v !== undefined)
166
+ .map(
167
+ ([key, value]) =>
168
+ [
169
+ key,
170
+ {
171
+ ...value,
172
+ schema: value?.schema
173
+ ? downgradeSchema(collection)(value.schema)
174
+ : undefined,
175
+ },
176
+ ] as const,
177
+ ),
178
+ );
179
+
180
+ /* -----------------------------------------------------------
181
+ DEFINITIONS
182
+ ----------------------------------------------------------- */
183
+ export const downgradeComponents = (
184
+ input: OpenApi.IComponents,
185
+ ): IComponentsCollection => {
186
+ const collection: IComponentsCollection = {
187
+ original: input,
188
+ downgraded: {
189
+ securitySchemes: input.securitySchemes,
190
+ },
191
+ };
192
+ if (input.schemas) {
193
+ collection.downgraded.schemas = {};
194
+ for (const [key, value] of Object.entries(input.schemas))
195
+ if (value !== undefined)
196
+ collection.downgraded.schemas[key] =
197
+ downgradeSchema(collection)(value);
198
+ }
199
+ return collection;
200
+ };
201
+
202
+ export const downgradeSchema =
203
+ (collection: IComponentsCollection) =>
204
+ (input: OpenApi.IJsonSchema): OpenApiV3.IJsonSchema => {
205
+ const nullable: boolean = isNullable(new Set())(collection.original)(
206
+ input,
207
+ );
208
+ const union: OpenApiV3.IJsonSchema[] = [];
209
+ const attribute: OpenApiV3.IJsonSchema.__IAttribute = {
210
+ title: input.title,
211
+ description: input.description,
212
+ deprecated: input.deprecated,
213
+ readOnly: input.readOnly,
214
+ writeOnly: input.writeOnly,
215
+ example: input.example,
216
+ examples: input.examples,
217
+ ...Object.fromEntries(
218
+ Object.entries(input).filter(
219
+ ([key, value]) => key.startsWith("x-") && value !== undefined,
220
+ ),
221
+ ),
222
+ };
223
+ const visit = (schema: OpenApi.IJsonSchema): void => {
224
+ if (OpenApiTypeChecker.isBoolean(schema))
225
+ union.push({ type: "boolean" });
226
+ else if (
227
+ OpenApiTypeChecker.isBoolean(schema) ||
228
+ OpenApiTypeChecker.isInteger(schema) ||
229
+ OpenApiTypeChecker.isNumber(schema) ||
230
+ OpenApiTypeChecker.isString(schema) ||
231
+ OpenApiTypeChecker.isReference(schema)
232
+ )
233
+ union.push({ ...schema });
234
+ else if (OpenApiTypeChecker.isArray(schema))
235
+ union.push({
236
+ ...schema,
237
+ items: downgradeSchema(collection)(schema.items),
238
+ });
239
+ else if (OpenApiTypeChecker.isTuple(schema))
240
+ union.push({
241
+ ...schema,
242
+ items: ((): OpenApiV3.IJsonSchema => {
243
+ if (schema.additionalItems === true) return {};
244
+ const elements = [
245
+ ...schema.prefixItems,
246
+ ...(typeof schema.additionalItems === "object"
247
+ ? [downgradeSchema(collection)(schema.additionalItems)]
248
+ : []),
249
+ ];
250
+ if (elements.length === 0) return {};
251
+ return {
252
+ oneOf: elements.map(downgradeSchema(collection) as any),
253
+ };
254
+ })(),
255
+ minItems: schema.prefixItems.length,
256
+ maxItems:
257
+ !!schema.additionalItems === true
258
+ ? undefined
259
+ : schema.prefixItems.length,
260
+ ...{
261
+ prefixItems: undefined,
262
+ additionalItems: undefined,
263
+ },
264
+ });
265
+ else if (OpenApiTypeChecker.isObject(schema))
266
+ union.push({
267
+ ...schema,
268
+ properties: schema.properties
269
+ ? Object.fromEntries(
270
+ Object.entries(schema.properties)
271
+ .filter(([_, v]) => v !== undefined)
272
+ .map(([key, value]) => [
273
+ key,
274
+ downgradeSchema(collection)(value),
275
+ ]),
276
+ )
277
+ : undefined,
278
+ additionalProperties:
279
+ typeof schema.additionalProperties === "object"
280
+ ? downgradeSchema(collection)(schema.additionalProperties)
281
+ : schema.additionalProperties,
282
+ required: schema.required,
283
+ });
284
+ else if (OpenApiTypeChecker.isOneOf(schema))
285
+ schema.oneOf.forEach(visit);
286
+ };
287
+ const visitConstant = (schema: OpenApi.IJsonSchema): void => {
288
+ const insert = (value: any): void => {
289
+ const matched: OpenApiV3.IJsonSchema.INumber | undefined = union.find(
290
+ (u) => (u as OpenApiV3.IJsonSchema.INumber).type === typeof value,
291
+ ) as OpenApiV3.IJsonSchema.INumber | undefined;
292
+ if (matched !== undefined) {
293
+ matched.enum ??= [];
294
+ matched.enum.push(value);
295
+ } else union.push({ type: typeof value as "number", enum: [value] });
296
+ };
297
+ if (OpenApiTypeChecker.isConstant(schema)) insert(schema.const);
298
+ else if (OpenApiTypeChecker.isOneOf(schema))
299
+ for (const u of schema.oneOf)
300
+ if (OpenApiTypeChecker.isConstant(u)) insert(u.const);
301
+ };
302
+
303
+ visit(input);
304
+ visitConstant(input);
305
+ if (nullable === true)
306
+ for (const u of union)
307
+ if (OpenApiTypeChecker.isReference(u as any))
308
+ downgradeNullableReference(new Set())(collection)(u as any);
309
+ else (u as OpenApiV3.IJsonSchema.IArray).nullable = true;
310
+ if (nullable === true && union.length === 0)
311
+ return { type: "null", ...attribute };
312
+ return {
313
+ ...(union.length === 0
314
+ ? { type: undefined }
315
+ : union.length === 1
316
+ ? { ...union[0] }
317
+ : { oneOf: union }),
318
+ ...attribute,
319
+ };
320
+ };
321
+
322
+ const downgradeNullableReference =
323
+ (visited: Set<string>) =>
324
+ (collection: IComponentsCollection) =>
325
+ (schema: OpenApiV3.IJsonSchema.IReference): void => {
326
+ const key: string = schema.$ref.split("/").pop()!;
327
+ if (key.endsWith(".Nullable")) return;
328
+
329
+ const found: OpenApi.IJsonSchema | undefined =
330
+ collection.original.schemas?.[key];
331
+ if (found === undefined) return;
332
+ else if (isNullable(visited)(collection.original)(found) === true) return;
333
+ else if (
334
+ collection.downgraded.schemas?.[`${key}.Nullable`] === undefined
335
+ ) {
336
+ collection.downgraded.schemas ??= {};
337
+ collection.downgraded.schemas[`${key}.Nullable`] = {};
338
+ collection.downgraded.schemas[`${key}.Nullable`] = downgradeSchema(
339
+ collection,
340
+ )(
341
+ OpenApiTypeChecker.isOneOf(found)
342
+ ? {
343
+ ...found,
344
+ oneOf: [...found.oneOf, { type: "null" }],
345
+ }
346
+ : {
347
+ oneOf: [found, { type: "null" }],
348
+ title: found.title,
349
+ description: found.description,
350
+ example: found.example,
351
+ examples: found.examples,
352
+ ...Object.fromEntries(
353
+ Object.entries(found).filter(
354
+ ([key, value]) =>
355
+ key.startsWith("x-") && value !== undefined,
356
+ ),
357
+ ),
358
+ },
359
+ );
360
+ }
361
+ schema.$ref += ".Nullable";
362
+ };
363
+
364
+ const isNullable =
365
+ (visited: Set<string>) =>
366
+ (components: OpenApi.IComponents) =>
367
+ (schema: OpenApi.IJsonSchema): boolean => {
368
+ if (OpenApiTypeChecker.isNull(schema)) return true;
369
+ else if (OpenApiTypeChecker.isReference(schema)) {
370
+ if (visited.has(schema.$ref)) return false;
371
+ visited.add(schema.$ref);
372
+ const key: string = schema.$ref.split("/").pop()!;
373
+ const next: OpenApi.IJsonSchema | undefined = components.schemas?.[key];
374
+ return next ? isNullable(visited)(components)(next) : false;
375
+ }
376
+ return (
377
+ OpenApiTypeChecker.isOneOf(schema) &&
378
+ schema.oneOf.some(isNullable(visited)(components))
379
+ );
380
+ };
381
+ }