@nestia/migrate 11.0.0-dev.20260305 → 11.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 (140) hide show
  1. package/lib/NestiaMigrateApplication.d.ts +24 -0
  2. package/lib/NestiaMigrateApplication.js +21136 -0
  3. package/lib/NestiaMigrateApplication.js.map +1 -0
  4. package/lib/analyzers/NestiaMigrateControllerAnalyzer.d.ts +5 -0
  5. package/lib/analyzers/NestiaMigrateControllerAnalyzer.js +46 -0
  6. package/lib/analyzers/NestiaMigrateControllerAnalyzer.js.map +1 -0
  7. package/lib/archivers/NestiaMigrateFileArchiver.d.ts +8 -0
  8. package/lib/archivers/NestiaMigrateFileArchiver.js +36 -0
  9. package/lib/archivers/NestiaMigrateFileArchiver.js.map +1 -0
  10. package/lib/bundles/NEST_TEMPLATE.d.ts +1 -0
  11. package/lib/bundles/NEST_TEMPLATE.js +55 -0
  12. package/lib/bundles/NEST_TEMPLATE.js.map +1 -0
  13. package/lib/bundles/SDK_TEMPLATE.d.ts +1 -0
  14. package/lib/bundles/SDK_TEMPLATE.js +27 -0
  15. package/lib/bundles/SDK_TEMPLATE.js.map +1 -0
  16. package/lib/executable/NestiaMigrateCommander.d.ts +4 -0
  17. package/lib/executable/NestiaMigrateCommander.js +125 -0
  18. package/lib/executable/NestiaMigrateCommander.js.map +1 -0
  19. package/lib/executable/NestiaMigrateInquirer.d.ts +12 -0
  20. package/lib/executable/NestiaMigrateInquirer.js +91 -0
  21. package/lib/executable/NestiaMigrateInquirer.js.map +1 -0
  22. package/lib/executable/migrate.d.ts +2 -0
  23. package/lib/executable/migrate.js +9 -0
  24. package/lib/executable/migrate.js.map +1 -0
  25. package/lib/factories/TypeLiteralFactory.d.ts +4 -0
  26. package/lib/factories/TypeLiteralFactory.js +35 -0
  27. package/lib/factories/TypeLiteralFactory.js.map +1 -0
  28. package/lib/index.d.ts +3 -0
  29. package/lib/index.js +42 -0
  30. package/lib/index.js.map +1 -0
  31. package/lib/index.mjs +21834 -0
  32. package/lib/index.mjs.map +1 -0
  33. package/lib/module.d.ts +5 -0
  34. package/lib/module.js +22 -0
  35. package/lib/module.js.map +1 -0
  36. package/lib/programmers/NestiaMigrateApiFileProgrammer.d.ts +13 -0
  37. package/lib/programmers/NestiaMigrateApiFileProgrammer.js +40 -0
  38. package/lib/programmers/NestiaMigrateApiFileProgrammer.js.map +1 -0
  39. package/lib/programmers/NestiaMigrateApiFunctionProgrammer.d.ts +14 -0
  40. package/lib/programmers/NestiaMigrateApiFunctionProgrammer.js +180 -0
  41. package/lib/programmers/NestiaMigrateApiFunctionProgrammer.js.map +1 -0
  42. package/lib/programmers/NestiaMigrateApiNamespaceProgrammer.d.ts +15 -0
  43. package/lib/programmers/NestiaMigrateApiNamespaceProgrammer.js +223 -0
  44. package/lib/programmers/NestiaMigrateApiNamespaceProgrammer.js.map +1 -0
  45. package/lib/programmers/NestiaMigrateApiProgrammer.d.ts +4 -0
  46. package/lib/programmers/NestiaMigrateApiProgrammer.js +82 -0
  47. package/lib/programmers/NestiaMigrateApiProgrammer.js.map +1 -0
  48. package/lib/programmers/NestiaMigrateApiSimulationProgrammer.d.ts +15 -0
  49. package/lib/programmers/NestiaMigrateApiSimulationProgrammer.js +140 -0
  50. package/lib/programmers/NestiaMigrateApiSimulationProgrammer.js.map +1 -0
  51. package/lib/programmers/NestiaMigrateApiStartProgrammer.d.ts +4 -0
  52. package/lib/programmers/NestiaMigrateApiStartProgrammer.js +79 -0
  53. package/lib/programmers/NestiaMigrateApiStartProgrammer.js.map +1 -0
  54. package/lib/programmers/NestiaMigrateDtoProgrammer.d.ts +15 -0
  55. package/lib/programmers/NestiaMigrateDtoProgrammer.js +65 -0
  56. package/lib/programmers/NestiaMigrateDtoProgrammer.js.map +1 -0
  57. package/lib/programmers/NestiaMigrateE2eFileProgrammer.d.ts +15 -0
  58. package/lib/programmers/NestiaMigrateE2eFileProgrammer.js +80 -0
  59. package/lib/programmers/NestiaMigrateE2eFileProgrammer.js.map +1 -0
  60. package/lib/programmers/NestiaMigrateE2eProgrammer.d.ts +4 -0
  61. package/lib/programmers/NestiaMigrateE2eProgrammer.js +32 -0
  62. package/lib/programmers/NestiaMigrateE2eProgrammer.js.map +1 -0
  63. package/lib/programmers/NestiaMigrateImportProgrammer.d.ts +18 -0
  64. package/lib/programmers/NestiaMigrateImportProgrammer.js +65 -0
  65. package/lib/programmers/NestiaMigrateImportProgrammer.js.map +1 -0
  66. package/lib/programmers/NestiaMigrateNestControllerProgrammer.d.ts +12 -0
  67. package/lib/programmers/NestiaMigrateNestControllerProgrammer.js +46 -0
  68. package/lib/programmers/NestiaMigrateNestControllerProgrammer.js.map +1 -0
  69. package/lib/programmers/NestiaMigrateNestMethodProgrammer.d.ts +16 -0
  70. package/lib/programmers/NestiaMigrateNestMethodProgrammer.js +221 -0
  71. package/lib/programmers/NestiaMigrateNestMethodProgrammer.js.map +1 -0
  72. package/lib/programmers/NestiaMigrateNestModuleProgrammer.d.ts +5 -0
  73. package/lib/programmers/NestiaMigrateNestModuleProgrammer.js +29 -0
  74. package/lib/programmers/NestiaMigrateNestModuleProgrammer.js.map +1 -0
  75. package/lib/programmers/NestiaMigrateNestProgrammer.d.ts +4 -0
  76. package/lib/programmers/NestiaMigrateNestProgrammer.js +65 -0
  77. package/lib/programmers/NestiaMigrateNestProgrammer.js.map +1 -0
  78. package/lib/programmers/NestiaMigrateSchemaProgrammer.d.ts +10 -0
  79. package/lib/programmers/NestiaMigrateSchemaProgrammer.js +339 -0
  80. package/lib/programmers/NestiaMigrateSchemaProgrammer.js.map +1 -0
  81. package/lib/structures/INestiaMigrateConfig.d.ts +15 -0
  82. package/lib/structures/INestiaMigrateConfig.js +3 -0
  83. package/lib/structures/INestiaMigrateConfig.js.map +1 -0
  84. package/lib/structures/INestiaMigrateContext.d.ts +7 -0
  85. package/lib/structures/INestiaMigrateContext.js +3 -0
  86. package/lib/structures/INestiaMigrateContext.js.map +1 -0
  87. package/lib/structures/INestiaMigrateController.d.ts +7 -0
  88. package/lib/structures/INestiaMigrateController.js +3 -0
  89. package/lib/structures/INestiaMigrateController.js.map +1 -0
  90. package/lib/structures/INestiaMigrateDto.d.ts +7 -0
  91. package/lib/structures/INestiaMigrateDto.js +3 -0
  92. package/lib/structures/INestiaMigrateDto.js.map +1 -0
  93. package/lib/structures/INestiaMigrateFile.d.ts +5 -0
  94. package/lib/structures/INestiaMigrateFile.js +3 -0
  95. package/lib/structures/INestiaMigrateFile.js.map +1 -0
  96. package/lib/structures/INestiaMigrateProgram.d.ts +9 -0
  97. package/lib/structures/INestiaMigrateProgram.js +3 -0
  98. package/lib/structures/INestiaMigrateProgram.js.map +1 -0
  99. package/lib/structures/INestiaMigrateSchema.d.ts +4 -0
  100. package/lib/structures/INestiaMigrateSchema.js +3 -0
  101. package/lib/structures/INestiaMigrateSchema.js.map +1 -0
  102. package/lib/utils/FilePrinter.d.ts +9 -0
  103. package/lib/utils/FilePrinter.js +35 -0
  104. package/lib/utils/FilePrinter.js.map +1 -0
  105. package/lib/utils/MapUtil.d.ts +3 -0
  106. package/lib/utils/MapUtil.js +15 -0
  107. package/lib/utils/MapUtil.js.map +1 -0
  108. package/lib/utils/SetupWizard.d.ts +3 -0
  109. package/lib/utils/SetupWizard.js +18 -0
  110. package/lib/utils/SetupWizard.js.map +1 -0
  111. package/lib/utils/StringUtil.d.ts +6 -0
  112. package/lib/utils/StringUtil.js +108 -0
  113. package/lib/utils/StringUtil.js.map +1 -0
  114. package/lib/utils/openapi-down-convert/RefVisitor.d.ts +52 -0
  115. package/lib/utils/openapi-down-convert/RefVisitor.js +102 -0
  116. package/lib/utils/openapi-down-convert/RefVisitor.js.map +1 -0
  117. package/lib/utils/openapi-down-convert/converter.d.ts +146 -0
  118. package/lib/utils/openapi-down-convert/converter.js +441 -0
  119. package/lib/utils/openapi-down-convert/converter.js.map +1 -0
  120. package/package.json +5 -5
  121. package/src/NestiaMigrateApplication.ts +4 -0
  122. package/src/archivers/NestiaMigrateFileArchiver.ts +28 -28
  123. package/src/bundles/NEST_TEMPLATE.ts +1 -1
  124. package/src/bundles/SDK_TEMPLATE.ts +1 -1
  125. package/src/executable/NestiaMigrateCommander.ts +8 -2
  126. package/src/executable/migrate.ts +7 -7
  127. package/src/index.ts +4 -4
  128. package/src/module.ts +3 -2
  129. package/src/programmers/NestiaMigrateApiProgrammer.ts +107 -107
  130. package/src/programmers/NestiaMigrateNestModuleProgrammer.ts +65 -65
  131. package/src/programmers/NestiaMigrateNestProgrammer.ts +88 -88
  132. package/src/programmers/index.ts +15 -0
  133. package/src/structures/INestiaMigrateConfig.ts +19 -19
  134. package/src/structures/INestiaMigrateFile.ts +5 -5
  135. package/src/structures/INestiaMigrateSchema.ts +4 -4
  136. package/src/structures/index.ts +4 -0
  137. package/src/utils/MapUtil.ts +13 -13
  138. package/src/utils/SetupWizard.ts +12 -12
  139. package/src/utils/openapi-down-convert/RefVisitor.ts +134 -134
  140. package/src/utils/openapi-down-convert/converter.ts +536 -536
@@ -1,536 +1,536 @@
1
- /** OpenAPI Down Converted - convert an OAS document from OAS 3.1 to OAS 3.0 */
2
- import {
3
- JsonNode,
4
- SchemaObject,
5
- SchemaVisitor,
6
- visitRefObjects,
7
- visitSchemaObjects,
8
- walkObject,
9
- } from "./RefVisitor";
10
-
11
- /** Lightweight OAS document top-level fields */
12
- interface OpenAPI3 {
13
- openapi: string;
14
- info: object;
15
- paths: object;
16
- components: object;
17
- tags: object;
18
- }
19
-
20
- /** Options for the converter instantiation */
21
- export interface ConverterOptions {
22
- /** If `true`, log conversion transformations to stderr */
23
- verbose?: boolean;
24
- /**
25
- * If `true`, remove `id` values in schema examples, to bypass [Spectral issue
26
- * 2081](https://github.com/stoplightio/spectral/issues/2081)
27
- */
28
- deleteExampleWithId?: boolean;
29
- /** If `true`, replace a `$ref` object that has siblings into an `allOf` */
30
- allOfTransform?: boolean;
31
-
32
- /** The authorizationUrl for openIdConnect -> oauth2 transformation */
33
- authorizationUrl?: string;
34
- /** The tokenUrl for openIdConnect -> oauth2 transformation */
35
- tokenUrl?: string;
36
- /**
37
- * Name of YAML/JSON file with scope descriptions. This is a simple map in the
38
- * format `{ scope1: "description of scope1", ... }`
39
- */
40
- scopeDescriptionFile?: string;
41
- /**
42
- * Earlier versions of the tool converted $comment to x-comment in JSON
43
- * Schemas. The tool now deletes $comment values by default. Use this option
44
- * to preserve the conversion and not delete comments.
45
- */
46
- convertSchemaComments?: boolean;
47
- }
48
-
49
- export class Converter {
50
- private openapi30: OpenAPI3;
51
- private verbose = false;
52
- private deleteExampleWithId = false;
53
- private allOfTransform = false;
54
- private authorizationUrl: string;
55
- /** The tokenUrl for openIdConnect -> oauth2 transformation */
56
- private tokenUrl: string;
57
- private scopeDescriptions = undefined;
58
- private convertSchemaComments = false;
59
- private returnCode = 0;
60
-
61
- /**
62
- * Construct a new Converter
63
- *
64
- * @throws Error if the scopeDescriptionFile (if specified) cannot be read or
65
- * parsed as YAML/JSON
66
- */
67
- constructor(openapiDocument: object, options?: ConverterOptions) {
68
- this.openapi30 = Converter.deepClone(openapiDocument) as OpenAPI3;
69
- this.verbose = Boolean(options?.verbose);
70
- this.deleteExampleWithId = Boolean(options?.deleteExampleWithId);
71
- this.allOfTransform = Boolean(options?.allOfTransform);
72
- this.authorizationUrl =
73
- options?.authorizationUrl || "https://www.example.com/oauth2/authorize";
74
- this.tokenUrl = options?.tokenUrl || "https://www.example.com/oauth2/token";
75
- this.loadScopeDescriptions(options?.scopeDescriptionFile);
76
- this.convertSchemaComments = !!options?.convertSchemaComments;
77
- }
78
-
79
- /**
80
- * Load the scopes.yaml file and save in this.scopeDescriptions
81
- *
82
- * @throws Error if the file cannot be read or parsed as YAML/JSON
83
- */
84
- private loadScopeDescriptions(scopeDescriptionFile?: string) {
85
- if (!scopeDescriptionFile) {
86
- return;
87
- }
88
- }
89
-
90
- /**
91
- * Log a message to console.warn stream if verbose is true
92
- *
93
- * @param message Parameters for console.warn
94
- */
95
- private log(...message: any[]) {
96
- if (this.verbose) {
97
- this.warn(...message);
98
- }
99
- }
100
-
101
- /**
102
- * Log a message to console.warn stream. Prefix the message string with
103
- * `Warning: ` if it does not already have that text.
104
- *
105
- * @param message Parameters for console.warn
106
- */
107
- private warn(...message: any[]) {
108
- if (!message[0].startsWith("Warning")) {
109
- message[0] = `Warning: ${message[0]}`;
110
- }
111
- console.warn(...message);
112
- }
113
-
114
- /**
115
- * Log an error message to `console.error` stream. Prefix the message string
116
- * with `Error: ` if it does not already start with `'Error'`. Increments the
117
- * `returnCode`, causing the CLI to throw an Error when done.
118
- *
119
- * @param message Parameters for `console.error`
120
- */
121
- private error(...message: any[]) {
122
- if (!message[0].startsWith("Error")) {
123
- message[0] = `Error: ${message[0]}`;
124
- }
125
- this.returnCode++;
126
- console.error(...message);
127
- }
128
-
129
- /**
130
- * Convert the OpenAPI document to 3.0
131
- *
132
- * @returns The converted document. The input is not modified.
133
- */
134
- public convert(): object {
135
- this.log("Converting from OpenAPI 3.1 to 3.0");
136
- this.openapi30.openapi = "3.0.3";
137
- this.removeLicenseIdentifier();
138
- this.convertSchemaRef();
139
- this.simplifyNonSchemaRef();
140
- if (this.scopeDescriptions) {
141
- this.convertSecuritySchemes();
142
- }
143
- this.convertJsonSchemaExamples();
144
- this.convertJsonSchemaContentEncoding();
145
- this.convertJsonSchemaContentMediaType();
146
- this.convertConstToEnum();
147
- this.convertNullableTypeArray();
148
- this.removeWebhooksObject();
149
- this.removeUnsupportedSchemaKeywords();
150
- if (this.convertSchemaComments) {
151
- this.renameSchema$comment();
152
- } else {
153
- this.deleteSchema$comment();
154
- }
155
- if (this.returnCode > 0) {
156
- throw new Error("Cannot down convert this OpenAPI definition.");
157
- }
158
- return this.openapi30;
159
- }
160
-
161
- /**
162
- * OpenAPI 3.1 uses JSON Schema 2020-12 which allows schema `examples`;
163
- * OpenAPI 3.0 uses JSON Scheme Draft 7 which only allows `example`. Replace
164
- * all `examples` with `example`, using `examples[0]`
165
- */
166
- convertJsonSchemaExamples() {
167
- const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
168
- for (const key in schema) {
169
- const subSchema = schema[key];
170
- if (subSchema !== null && typeof subSchema === "object") {
171
- if (key === "examples") {
172
- const examples = schema["examples"];
173
- if (Array.isArray(examples) && examples.length > 0) {
174
- delete schema["examples"];
175
- const first = examples[0];
176
- if (
177
- this.deleteExampleWithId &&
178
- first != null &&
179
- typeof first === "object" &&
180
- first.hasOwnProperty("id")
181
- ) {
182
- this.log(
183
- `Deleted schema example with \`id\` property:\n${this.json(examples)}`,
184
- );
185
- } else {
186
- schema["example"] = first;
187
- this.log(
188
- `Replaces examples with examples[0]. Old examples:\n${this.json(examples)}`,
189
- );
190
- }
191
- // TODO: Add an else here to check example for `id` and delete the example if this.deleteExampleWithId
192
- // We've put most of those in `examples` so this is probably not needed, but it would be more robust.
193
- }
194
- } else {
195
- schema[key] = walkObject(subSchema, schemaVisitor);
196
- }
197
- }
198
- }
199
- return schema;
200
- };
201
- visitSchemaObjects(this.openapi30, schemaVisitor);
202
- }
203
-
204
- private walkNestedSchemaObjects(schema: any, schemaVisitor: any) {
205
- for (const key in schema) {
206
- const subSchema = schema[key];
207
- if (subSchema !== null && typeof subSchema === "object") {
208
- schema[key] = walkObject(subSchema, schemaVisitor);
209
- }
210
- }
211
- return schema;
212
- }
213
-
214
- /**
215
- * OpenAPI 3.1 uses JSON Schema 2020-12 which allows `const` OpenAPI 3.0 uses
216
- * JSON Scheme Draft 7 which only allows `enum`. Replace all `const: value`
217
- * with `enum: [ value ]`
218
- */
219
- convertConstToEnum() {
220
- const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
221
- if (schema["const"]) {
222
- const constant = schema["const"];
223
- delete schema["const"];
224
- schema["enum"] = [constant];
225
- this.log(`Converted const: ${constant} to enum`);
226
- }
227
- return this.walkNestedSchemaObjects(schema, schemaVisitor);
228
- };
229
- visitSchemaObjects(this.openapi30, schemaVisitor);
230
- }
231
-
232
- /**
233
- * Convert 2-element type arrays containing 'null' to string type and
234
- * `nullable: true`
235
- */
236
- convertNullableTypeArray() {
237
- const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
238
- if (schema.hasOwnProperty("type")) {
239
- const schemaType = schema["type"];
240
- if (
241
- Array.isArray(schemaType) &&
242
- schemaType.length === 2 &&
243
- schemaType.includes("null")
244
- ) {
245
- const nonNull = schemaType.filter((_) => _ !== "null")[0];
246
- schema["type"] = nonNull;
247
- schema["nullable"] = true;
248
- this.log(`Converted schema type array to nullable`);
249
- }
250
- }
251
- return this.walkNestedSchemaObjects(schema, schemaVisitor);
252
- };
253
- visitSchemaObjects(this.openapi30, schemaVisitor);
254
- }
255
-
256
- removeWebhooksObject() {
257
- if (Object.hasOwnProperty.call(this.openapi30, "webhooks")) {
258
- this.log(`Deleted webhooks object`);
259
- delete (this.openapi30 as any)["webhooks"];
260
- }
261
- }
262
- removeUnsupportedSchemaKeywords() {
263
- const keywordsToRemove = [
264
- "$id",
265
- "$schema",
266
- "unevaluatedProperties",
267
- "contentMediaType",
268
- "patternProperties",
269
- ];
270
- const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
271
- keywordsToRemove.forEach((key) => {
272
- if (schema.hasOwnProperty(key)) {
273
- delete schema[key];
274
- this.log(`Removed unsupported schema keyword ${key}`);
275
- }
276
- });
277
- return this.walkNestedSchemaObjects(schema, schemaVisitor);
278
- };
279
- visitSchemaObjects(this.openapi30, schemaVisitor);
280
- }
281
-
282
- renameSchema$comment() {
283
- const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
284
- if (schema.hasOwnProperty("$comment")) {
285
- schema["x-comment"] = schema["$comment"];
286
- delete schema["$comment"];
287
- this.log(`schema $comment renamed to x-comment`);
288
- }
289
- return this.walkNestedSchemaObjects(schema, schemaVisitor);
290
- };
291
- visitSchemaObjects(this.openapi30, schemaVisitor);
292
- }
293
-
294
- private deleteSchema$comment() {
295
- const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
296
- if (schema.hasOwnProperty("$comment")) {
297
- const comment = schema["$comment"];
298
- delete schema["$comment"];
299
- this.log(`schema $comment deleted: ${comment}`);
300
- }
301
- return this.walkNestedSchemaObjects(schema, schemaVisitor);
302
- };
303
- visitSchemaObjects(this.openapi30, schemaVisitor);
304
- }
305
-
306
- /**
307
- * Convert
308
- *
309
- * contentMediaType: "application/octet-stream";
310
- *
311
- * To
312
- *
313
- * format: binary;
314
- *
315
- * In `type: string` schemas. Warn if schema has a `format` already and it is
316
- * not `binary`.
317
- */
318
- convertJsonSchemaContentMediaType() {
319
- const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
320
- if (
321
- schema.hasOwnProperty("type") &&
322
- schema["type"] === "string" &&
323
- schema.hasOwnProperty("contentMediaType") &&
324
- schema["contentMediaType"] === "application/octet-stream"
325
- ) {
326
- if (schema.hasOwnProperty("format")) {
327
- if (schema["format"] === "binary") {
328
- this.log(
329
- `Deleted schema contentMediaType: application/octet-stream (leaving format: binary)`,
330
- );
331
- delete schema["contentMediaType"];
332
- } else {
333
- this.error(
334
- `Unable to down-convert schema with contentMediaType: application/octet-stream to format: binary because the schema already has a format (${schema["format"]})`,
335
- );
336
- }
337
- } else {
338
- delete schema["contentMediaType"];
339
- schema["format"] = "binary";
340
- this.log(
341
- `Converted schema contentMediaType: application/octet-stream to format: binary`,
342
- );
343
- }
344
- }
345
- return this.walkNestedSchemaObjects(schema, schemaVisitor);
346
- };
347
- visitSchemaObjects(this.openapi30, schemaVisitor);
348
- }
349
-
350
- /**
351
- * Convert
352
- *
353
- * contentEncoding: base64;
354
- *
355
- * To
356
- *
357
- * format: byte;
358
- *
359
- * In `type: string` schemas. It is an error if the schema has a `format`
360
- * already and it is not `byte`.
361
- */
362
- convertJsonSchemaContentEncoding() {
363
- const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
364
- if (
365
- schema.hasOwnProperty("type") &&
366
- schema["type"] === "string" &&
367
- schema.hasOwnProperty("contentEncoding")
368
- ) {
369
- if (schema["contentEncoding"] === "base64") {
370
- if (schema.hasOwnProperty("format")) {
371
- if (schema["format"] === "byte") {
372
- this.log(
373
- `Deleted schema contentEncoding: base64 (leaving format: byte)`,
374
- );
375
- delete schema["contentEncoding"];
376
- } else {
377
- this.error(
378
- `Unable to down-convert schema contentEncoding: base64 to format: byte because the schema already has a format (${schema["format"]})`,
379
- );
380
- }
381
- } else {
382
- delete schema["contentEncoding"];
383
- schema["format"] = "byte";
384
- this.log(
385
- `Converted schema: 'contentEncoding: base64' to 'format: byte'`,
386
- );
387
- }
388
- } else {
389
- this.error(
390
- `Unable to down-convert contentEncoding: ${schema["contentEncoding"]}`,
391
- );
392
- }
393
- }
394
- return this.walkNestedSchemaObjects(schema, schemaVisitor);
395
- };
396
- visitSchemaObjects(this.openapi30, schemaVisitor);
397
- }
398
-
399
- private json(x: any) {
400
- return JSON.stringify(x, null, 2);
401
- }
402
-
403
- /**
404
- * OpenAPI 3.1 defines a new `openIdConnect` security scheme. Down-convert the
405
- * scheme to `oauth2` / authorization code flow. Collect all the scopes used
406
- * in any security requirements within operations and add them to the scheme.
407
- * Also define the URLs to the `authorizationUrl` and `tokenUrl` of `oauth2`.
408
- */
409
- convertSecuritySchemes() {
410
- const oauth2Scopes = (schemeName: string): object => {
411
- const scopes = {} as any;
412
- const paths: any = this.openapi30?.paths ?? {};
413
- for (const path in paths) {
414
- for (const op in paths[path]) {
415
- if (op === "parameters") {
416
- continue;
417
- }
418
- const operation = paths[path][op];
419
- const sec = operation?.security as object[];
420
- sec.forEach((s: any) => {
421
- const requirement = s?.[schemeName] as string[];
422
- if (requirement) {
423
- requirement.forEach((scope) => {
424
- scopes[scope] =
425
- this.scopeDescriptions?.[scope] ??
426
- `TODO: describe the '${scope}' scope`;
427
- });
428
- }
429
- });
430
- }
431
- }
432
- return scopes;
433
- };
434
- const schemes =
435
- (this.openapi30?.components as any)?.["securitySchemes"] ?? {};
436
- for (const schemeName in schemes) {
437
- const scheme = schemes[schemeName];
438
- const type = scheme.type;
439
- if (type === "openIdConnect") {
440
- this.log(
441
- `Converting openIdConnect security scheme to oauth2/authorizationCode`,
442
- );
443
- scheme.type = "oauth2";
444
- const openIdConnectUrl = scheme.openIdConnectUrl;
445
- scheme.description = `OAuth2 Authorization Code Flow. The client may
446
- GET the OpenID Connect configuration JSON from \`${openIdConnectUrl}\`
447
- to get the correct \`authorizationUrl\` and \`tokenUrl\`.`;
448
- delete scheme.openIdConnectUrl;
449
- const scopes = oauth2Scopes(schemeName);
450
- scheme.flows = {
451
- authorizationCode: {
452
- // TODO: add options for these URLs
453
- authorizationUrl: this.authorizationUrl,
454
- tokenUrl: this.tokenUrl,
455
- scopes: scopes,
456
- },
457
- };
458
- }
459
- }
460
- }
461
-
462
- /**
463
- * Find remaining OpenAPI 3.0 [Reference
464
- * Objects](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#referenceObject)
465
- * and down convert them to [JSON
466
- * Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03)
467
- * objects with _only_ a `$ref` property.
468
- */
469
- simplifyNonSchemaRef() {
470
- visitRefObjects(this.openapi30, (node: any): JsonNode => {
471
- if (Object.keys(node).length === 1) {
472
- return node;
473
- } else {
474
- this.log(
475
- `Down convert reference object to JSON Reference:\n${JSON.stringify(node, null, 3)}`,
476
- );
477
- Object.keys(node)
478
- .filter((key) => key !== "$ref")
479
- .forEach((key) => delete node[key]);
480
- return node;
481
- }
482
- });
483
- }
484
-
485
- removeLicenseIdentifier() {
486
- if ((this.openapi30 as any)?.["info"]?.["license"]?.["identifier"]) {
487
- this.log(
488
- `Removed info.license.identifier: ${(this.openapi30 as any)["info"]["license"]["identifier"]}`,
489
- );
490
- delete (this.openapi30 as any)["info"]["license"]["identifier"];
491
- }
492
- }
493
-
494
- // This transformation ends up breaking openapi-generator
495
- // SDK gen (typescript-axios, typescript-angular)
496
- // so it is disabled unless the `allOfTransform` option is `true`.
497
-
498
- convertSchemaRef() {
499
- /**
500
- * In a JSON Schema, replace `{ blah blah, $ref: "uri"}` with `{ blah blah,
501
- * allOf: [ $ref: "uri" ]}`
502
- *
503
- * @param object An object that may contain JSON schemas (directly or in
504
- * sub-objects)
505
- */
506
- const simplifyRefObjectsInSchemas = (
507
- object: SchemaObject,
508
- ): SchemaObject => {
509
- return visitRefObjects(object, (node: any): JsonNode => {
510
- if (Object.keys(node).length === 1) {
511
- return node;
512
- } else {
513
- this.log(
514
- `Converting JSON Schema $ref ${this.json(node)} to allOf: [ $ref ]`,
515
- );
516
- node["allOf"] = [{ $ref: node.$ref }];
517
- delete node.$ref;
518
- return node;
519
- }
520
- });
521
- };
522
-
523
- if (this.allOfTransform) {
524
- visitSchemaObjects(
525
- this.openapi30,
526
- (schema: SchemaObject): SchemaObject => {
527
- return simplifyRefObjectsInSchemas(schema);
528
- },
529
- );
530
- }
531
- }
532
-
533
- public static deepClone(obj: object): object {
534
- return JSON.parse(JSON.stringify(obj)); // kinda simple way to clone, but it works...
535
- }
536
- }
1
+ /** OpenAPI Down Converted - convert an OAS document from OAS 3.1 to OAS 3.0 */
2
+ import {
3
+ JsonNode,
4
+ SchemaObject,
5
+ SchemaVisitor,
6
+ visitRefObjects,
7
+ visitSchemaObjects,
8
+ walkObject,
9
+ } from "./RefVisitor";
10
+
11
+ /** Lightweight OAS document top-level fields */
12
+ interface OpenAPI3 {
13
+ openapi: string;
14
+ info: object;
15
+ paths: object;
16
+ components: object;
17
+ tags: object;
18
+ }
19
+
20
+ /** Options for the converter instantiation */
21
+ export interface ConverterOptions {
22
+ /** If `true`, log conversion transformations to stderr */
23
+ verbose?: boolean;
24
+ /**
25
+ * If `true`, remove `id` values in schema examples, to bypass [Spectral issue
26
+ * 2081](https://github.com/stoplightio/spectral/issues/2081)
27
+ */
28
+ deleteExampleWithId?: boolean;
29
+ /** If `true`, replace a `$ref` object that has siblings into an `allOf` */
30
+ allOfTransform?: boolean;
31
+
32
+ /** The authorizationUrl for openIdConnect -> oauth2 transformation */
33
+ authorizationUrl?: string;
34
+ /** The tokenUrl for openIdConnect -> oauth2 transformation */
35
+ tokenUrl?: string;
36
+ /**
37
+ * Name of YAML/JSON file with scope descriptions. This is a simple map in the
38
+ * format `{ scope1: "description of scope1", ... }`
39
+ */
40
+ scopeDescriptionFile?: string;
41
+ /**
42
+ * Earlier versions of the tool converted $comment to x-comment in JSON
43
+ * Schemas. The tool now deletes $comment values by default. Use this option
44
+ * to preserve the conversion and not delete comments.
45
+ */
46
+ convertSchemaComments?: boolean;
47
+ }
48
+
49
+ export class Converter {
50
+ private openapi30: OpenAPI3;
51
+ private verbose = false;
52
+ private deleteExampleWithId = false;
53
+ private allOfTransform = false;
54
+ private authorizationUrl: string;
55
+ /** The tokenUrl for openIdConnect -> oauth2 transformation */
56
+ private tokenUrl: string;
57
+ private scopeDescriptions = undefined;
58
+ private convertSchemaComments = false;
59
+ private returnCode = 0;
60
+
61
+ /**
62
+ * Construct a new Converter
63
+ *
64
+ * @throws Error if the scopeDescriptionFile (if specified) cannot be read or
65
+ * parsed as YAML/JSON
66
+ */
67
+ constructor(openapiDocument: object, options?: ConverterOptions) {
68
+ this.openapi30 = Converter.deepClone(openapiDocument) as OpenAPI3;
69
+ this.verbose = Boolean(options?.verbose);
70
+ this.deleteExampleWithId = Boolean(options?.deleteExampleWithId);
71
+ this.allOfTransform = Boolean(options?.allOfTransform);
72
+ this.authorizationUrl =
73
+ options?.authorizationUrl || "https://www.example.com/oauth2/authorize";
74
+ this.tokenUrl = options?.tokenUrl || "https://www.example.com/oauth2/token";
75
+ this.loadScopeDescriptions(options?.scopeDescriptionFile);
76
+ this.convertSchemaComments = !!options?.convertSchemaComments;
77
+ }
78
+
79
+ /**
80
+ * Load the scopes.yaml file and save in this.scopeDescriptions
81
+ *
82
+ * @throws Error if the file cannot be read or parsed as YAML/JSON
83
+ */
84
+ private loadScopeDescriptions(scopeDescriptionFile?: string) {
85
+ if (!scopeDescriptionFile) {
86
+ return;
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Log a message to console.warn stream if verbose is true
92
+ *
93
+ * @param message Parameters for console.warn
94
+ */
95
+ private log(...message: any[]) {
96
+ if (this.verbose) {
97
+ this.warn(...message);
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Log a message to console.warn stream. Prefix the message string with
103
+ * `Warning: ` if it does not already have that text.
104
+ *
105
+ * @param message Parameters for console.warn
106
+ */
107
+ private warn(...message: any[]) {
108
+ if (!message[0].startsWith("Warning")) {
109
+ message[0] = `Warning: ${message[0]}`;
110
+ }
111
+ console.warn(...message);
112
+ }
113
+
114
+ /**
115
+ * Log an error message to `console.error` stream. Prefix the message string
116
+ * with `Error: ` if it does not already start with `'Error'`. Increments the
117
+ * `returnCode`, causing the CLI to throw an Error when done.
118
+ *
119
+ * @param message Parameters for `console.error`
120
+ */
121
+ private error(...message: any[]) {
122
+ if (!message[0].startsWith("Error")) {
123
+ message[0] = `Error: ${message[0]}`;
124
+ }
125
+ this.returnCode++;
126
+ console.error(...message);
127
+ }
128
+
129
+ /**
130
+ * Convert the OpenAPI document to 3.0
131
+ *
132
+ * @returns The converted document. The input is not modified.
133
+ */
134
+ public convert(): object {
135
+ this.log("Converting from OpenAPI 3.1 to 3.0");
136
+ this.openapi30.openapi = "3.0.3";
137
+ this.removeLicenseIdentifier();
138
+ this.convertSchemaRef();
139
+ this.simplifyNonSchemaRef();
140
+ if (this.scopeDescriptions) {
141
+ this.convertSecuritySchemes();
142
+ }
143
+ this.convertJsonSchemaExamples();
144
+ this.convertJsonSchemaContentEncoding();
145
+ this.convertJsonSchemaContentMediaType();
146
+ this.convertConstToEnum();
147
+ this.convertNullableTypeArray();
148
+ this.removeWebhooksObject();
149
+ this.removeUnsupportedSchemaKeywords();
150
+ if (this.convertSchemaComments) {
151
+ this.renameSchema$comment();
152
+ } else {
153
+ this.deleteSchema$comment();
154
+ }
155
+ if (this.returnCode > 0) {
156
+ throw new Error("Cannot down convert this OpenAPI definition.");
157
+ }
158
+ return this.openapi30;
159
+ }
160
+
161
+ /**
162
+ * OpenAPI 3.1 uses JSON Schema 2020-12 which allows schema `examples`;
163
+ * OpenAPI 3.0 uses JSON Scheme Draft 7 which only allows `example`. Replace
164
+ * all `examples` with `example`, using `examples[0]`
165
+ */
166
+ convertJsonSchemaExamples() {
167
+ const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
168
+ for (const key in schema) {
169
+ const subSchema = schema[key];
170
+ if (subSchema !== null && typeof subSchema === "object") {
171
+ if (key === "examples") {
172
+ const examples = schema["examples"];
173
+ if (Array.isArray(examples) && examples.length > 0) {
174
+ delete schema["examples"];
175
+ const first = examples[0];
176
+ if (
177
+ this.deleteExampleWithId &&
178
+ first != null &&
179
+ typeof first === "object" &&
180
+ first.hasOwnProperty("id")
181
+ ) {
182
+ this.log(
183
+ `Deleted schema example with \`id\` property:\n${this.json(examples)}`,
184
+ );
185
+ } else {
186
+ schema["example"] = first;
187
+ this.log(
188
+ `Replaces examples with examples[0]. Old examples:\n${this.json(examples)}`,
189
+ );
190
+ }
191
+ // TODO: Add an else here to check example for `id` and delete the example if this.deleteExampleWithId
192
+ // We've put most of those in `examples` so this is probably not needed, but it would be more robust.
193
+ }
194
+ } else {
195
+ schema[key] = walkObject(subSchema, schemaVisitor);
196
+ }
197
+ }
198
+ }
199
+ return schema;
200
+ };
201
+ visitSchemaObjects(this.openapi30, schemaVisitor);
202
+ }
203
+
204
+ private walkNestedSchemaObjects(schema: any, schemaVisitor: any) {
205
+ for (const key in schema) {
206
+ const subSchema = schema[key];
207
+ if (subSchema !== null && typeof subSchema === "object") {
208
+ schema[key] = walkObject(subSchema, schemaVisitor);
209
+ }
210
+ }
211
+ return schema;
212
+ }
213
+
214
+ /**
215
+ * OpenAPI 3.1 uses JSON Schema 2020-12 which allows `const` OpenAPI 3.0 uses
216
+ * JSON Scheme Draft 7 which only allows `enum`. Replace all `const: value`
217
+ * with `enum: [ value ]`
218
+ */
219
+ convertConstToEnum() {
220
+ const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
221
+ if (schema["const"]) {
222
+ const constant = schema["const"];
223
+ delete schema["const"];
224
+ schema["enum"] = [constant];
225
+ this.log(`Converted const: ${constant} to enum`);
226
+ }
227
+ return this.walkNestedSchemaObjects(schema, schemaVisitor);
228
+ };
229
+ visitSchemaObjects(this.openapi30, schemaVisitor);
230
+ }
231
+
232
+ /**
233
+ * Convert 2-element type arrays containing 'null' to string type and
234
+ * `nullable: true`
235
+ */
236
+ convertNullableTypeArray() {
237
+ const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
238
+ if (schema.hasOwnProperty("type")) {
239
+ const schemaType = schema["type"];
240
+ if (
241
+ Array.isArray(schemaType) &&
242
+ schemaType.length === 2 &&
243
+ schemaType.includes("null")
244
+ ) {
245
+ const nonNull = schemaType.filter((_) => _ !== "null")[0];
246
+ schema["type"] = nonNull;
247
+ schema["nullable"] = true;
248
+ this.log(`Converted schema type array to nullable`);
249
+ }
250
+ }
251
+ return this.walkNestedSchemaObjects(schema, schemaVisitor);
252
+ };
253
+ visitSchemaObjects(this.openapi30, schemaVisitor);
254
+ }
255
+
256
+ removeWebhooksObject() {
257
+ if (Object.hasOwnProperty.call(this.openapi30, "webhooks")) {
258
+ this.log(`Deleted webhooks object`);
259
+ delete (this.openapi30 as any)["webhooks"];
260
+ }
261
+ }
262
+ removeUnsupportedSchemaKeywords() {
263
+ const keywordsToRemove = [
264
+ "$id",
265
+ "$schema",
266
+ "unevaluatedProperties",
267
+ "contentMediaType",
268
+ "patternProperties",
269
+ ];
270
+ const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
271
+ keywordsToRemove.forEach((key) => {
272
+ if (schema.hasOwnProperty(key)) {
273
+ delete schema[key];
274
+ this.log(`Removed unsupported schema keyword ${key}`);
275
+ }
276
+ });
277
+ return this.walkNestedSchemaObjects(schema, schemaVisitor);
278
+ };
279
+ visitSchemaObjects(this.openapi30, schemaVisitor);
280
+ }
281
+
282
+ renameSchema$comment() {
283
+ const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
284
+ if (schema.hasOwnProperty("$comment")) {
285
+ schema["x-comment"] = schema["$comment"];
286
+ delete schema["$comment"];
287
+ this.log(`schema $comment renamed to x-comment`);
288
+ }
289
+ return this.walkNestedSchemaObjects(schema, schemaVisitor);
290
+ };
291
+ visitSchemaObjects(this.openapi30, schemaVisitor);
292
+ }
293
+
294
+ private deleteSchema$comment() {
295
+ const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
296
+ if (schema.hasOwnProperty("$comment")) {
297
+ const comment = schema["$comment"];
298
+ delete schema["$comment"];
299
+ this.log(`schema $comment deleted: ${comment}`);
300
+ }
301
+ return this.walkNestedSchemaObjects(schema, schemaVisitor);
302
+ };
303
+ visitSchemaObjects(this.openapi30, schemaVisitor);
304
+ }
305
+
306
+ /**
307
+ * Convert
308
+ *
309
+ * contentMediaType: "application/octet-stream";
310
+ *
311
+ * To
312
+ *
313
+ * format: binary;
314
+ *
315
+ * In `type: string` schemas. Warn if schema has a `format` already and it is
316
+ * not `binary`.
317
+ */
318
+ convertJsonSchemaContentMediaType() {
319
+ const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
320
+ if (
321
+ schema.hasOwnProperty("type") &&
322
+ schema["type"] === "string" &&
323
+ schema.hasOwnProperty("contentMediaType") &&
324
+ schema["contentMediaType"] === "application/octet-stream"
325
+ ) {
326
+ if (schema.hasOwnProperty("format")) {
327
+ if (schema["format"] === "binary") {
328
+ this.log(
329
+ `Deleted schema contentMediaType: application/octet-stream (leaving format: binary)`,
330
+ );
331
+ delete schema["contentMediaType"];
332
+ } else {
333
+ this.error(
334
+ `Unable to down-convert schema with contentMediaType: application/octet-stream to format: binary because the schema already has a format (${schema["format"]})`,
335
+ );
336
+ }
337
+ } else {
338
+ delete schema["contentMediaType"];
339
+ schema["format"] = "binary";
340
+ this.log(
341
+ `Converted schema contentMediaType: application/octet-stream to format: binary`,
342
+ );
343
+ }
344
+ }
345
+ return this.walkNestedSchemaObjects(schema, schemaVisitor);
346
+ };
347
+ visitSchemaObjects(this.openapi30, schemaVisitor);
348
+ }
349
+
350
+ /**
351
+ * Convert
352
+ *
353
+ * contentEncoding: base64;
354
+ *
355
+ * To
356
+ *
357
+ * format: byte;
358
+ *
359
+ * In `type: string` schemas. It is an error if the schema has a `format`
360
+ * already and it is not `byte`.
361
+ */
362
+ convertJsonSchemaContentEncoding() {
363
+ const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
364
+ if (
365
+ schema.hasOwnProperty("type") &&
366
+ schema["type"] === "string" &&
367
+ schema.hasOwnProperty("contentEncoding")
368
+ ) {
369
+ if (schema["contentEncoding"] === "base64") {
370
+ if (schema.hasOwnProperty("format")) {
371
+ if (schema["format"] === "byte") {
372
+ this.log(
373
+ `Deleted schema contentEncoding: base64 (leaving format: byte)`,
374
+ );
375
+ delete schema["contentEncoding"];
376
+ } else {
377
+ this.error(
378
+ `Unable to down-convert schema contentEncoding: base64 to format: byte because the schema already has a format (${schema["format"]})`,
379
+ );
380
+ }
381
+ } else {
382
+ delete schema["contentEncoding"];
383
+ schema["format"] = "byte";
384
+ this.log(
385
+ `Converted schema: 'contentEncoding: base64' to 'format: byte'`,
386
+ );
387
+ }
388
+ } else {
389
+ this.error(
390
+ `Unable to down-convert contentEncoding: ${schema["contentEncoding"]}`,
391
+ );
392
+ }
393
+ }
394
+ return this.walkNestedSchemaObjects(schema, schemaVisitor);
395
+ };
396
+ visitSchemaObjects(this.openapi30, schemaVisitor);
397
+ }
398
+
399
+ private json(x: any) {
400
+ return JSON.stringify(x, null, 2);
401
+ }
402
+
403
+ /**
404
+ * OpenAPI 3.1 defines a new `openIdConnect` security scheme. Down-convert the
405
+ * scheme to `oauth2` / authorization code flow. Collect all the scopes used
406
+ * in any security requirements within operations and add them to the scheme.
407
+ * Also define the URLs to the `authorizationUrl` and `tokenUrl` of `oauth2`.
408
+ */
409
+ convertSecuritySchemes() {
410
+ const oauth2Scopes = (schemeName: string): object => {
411
+ const scopes = {} as any;
412
+ const paths: any = this.openapi30?.paths ?? {};
413
+ for (const path in paths) {
414
+ for (const op in paths[path]) {
415
+ if (op === "parameters") {
416
+ continue;
417
+ }
418
+ const operation = paths[path][op];
419
+ const sec = operation?.security as object[];
420
+ sec.forEach((s: any) => {
421
+ const requirement = s?.[schemeName] as string[];
422
+ if (requirement) {
423
+ requirement.forEach((scope) => {
424
+ scopes[scope] =
425
+ this.scopeDescriptions?.[scope] ??
426
+ `TODO: describe the '${scope}' scope`;
427
+ });
428
+ }
429
+ });
430
+ }
431
+ }
432
+ return scopes;
433
+ };
434
+ const schemes =
435
+ (this.openapi30?.components as any)?.["securitySchemes"] ?? {};
436
+ for (const schemeName in schemes) {
437
+ const scheme = schemes[schemeName];
438
+ const type = scheme.type;
439
+ if (type === "openIdConnect") {
440
+ this.log(
441
+ `Converting openIdConnect security scheme to oauth2/authorizationCode`,
442
+ );
443
+ scheme.type = "oauth2";
444
+ const openIdConnectUrl = scheme.openIdConnectUrl;
445
+ scheme.description = `OAuth2 Authorization Code Flow. The client may
446
+ GET the OpenID Connect configuration JSON from \`${openIdConnectUrl}\`
447
+ to get the correct \`authorizationUrl\` and \`tokenUrl\`.`;
448
+ delete scheme.openIdConnectUrl;
449
+ const scopes = oauth2Scopes(schemeName);
450
+ scheme.flows = {
451
+ authorizationCode: {
452
+ // TODO: add options for these URLs
453
+ authorizationUrl: this.authorizationUrl,
454
+ tokenUrl: this.tokenUrl,
455
+ scopes: scopes,
456
+ },
457
+ };
458
+ }
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Find remaining OpenAPI 3.0 [Reference
464
+ * Objects](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#referenceObject)
465
+ * and down convert them to [JSON
466
+ * Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03)
467
+ * objects with _only_ a `$ref` property.
468
+ */
469
+ simplifyNonSchemaRef() {
470
+ visitRefObjects(this.openapi30, (node: any): JsonNode => {
471
+ if (Object.keys(node).length === 1) {
472
+ return node;
473
+ } else {
474
+ this.log(
475
+ `Down convert reference object to JSON Reference:\n${JSON.stringify(node, null, 3)}`,
476
+ );
477
+ Object.keys(node)
478
+ .filter((key) => key !== "$ref")
479
+ .forEach((key) => delete node[key]);
480
+ return node;
481
+ }
482
+ });
483
+ }
484
+
485
+ removeLicenseIdentifier() {
486
+ if ((this.openapi30 as any)?.["info"]?.["license"]?.["identifier"]) {
487
+ this.log(
488
+ `Removed info.license.identifier: ${(this.openapi30 as any)["info"]["license"]["identifier"]}`,
489
+ );
490
+ delete (this.openapi30 as any)["info"]["license"]["identifier"];
491
+ }
492
+ }
493
+
494
+ // This transformation ends up breaking openapi-generator
495
+ // SDK gen (typescript-axios, typescript-angular)
496
+ // so it is disabled unless the `allOfTransform` option is `true`.
497
+
498
+ convertSchemaRef() {
499
+ /**
500
+ * In a JSON Schema, replace `{ blah blah, $ref: "uri"}` with `{ blah blah,
501
+ * allOf: [ $ref: "uri" ]}`
502
+ *
503
+ * @param object An object that may contain JSON schemas (directly or in
504
+ * sub-objects)
505
+ */
506
+ const simplifyRefObjectsInSchemas = (
507
+ object: SchemaObject,
508
+ ): SchemaObject => {
509
+ return visitRefObjects(object, (node: any): JsonNode => {
510
+ if (Object.keys(node).length === 1) {
511
+ return node;
512
+ } else {
513
+ this.log(
514
+ `Converting JSON Schema $ref ${this.json(node)} to allOf: [ $ref ]`,
515
+ );
516
+ node["allOf"] = [{ $ref: node.$ref }];
517
+ delete node.$ref;
518
+ return node;
519
+ }
520
+ });
521
+ };
522
+
523
+ if (this.allOfTransform) {
524
+ visitSchemaObjects(
525
+ this.openapi30,
526
+ (schema: SchemaObject): SchemaObject => {
527
+ return simplifyRefObjectsInSchemas(schema);
528
+ },
529
+ );
530
+ }
531
+ }
532
+
533
+ public static deepClone(obj: object): object {
534
+ return JSON.parse(JSON.stringify(obj)); // kinda simple way to clone, but it works...
535
+ }
536
+ }