@nestia/migrate 11.2.0 → 12.0.0-dev.20260520.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/lib/NestiaMigrateApplication.d.ts +1 -12
  2. package/lib/NestiaMigrateApplication.js +22 -24873
  3. package/lib/NestiaMigrateApplication.js.map +1 -1
  4. package/lib/analyzers/NestiaMigrateControllerAnalyzer.js.map +1 -1
  5. package/lib/archivers/NestiaMigrateFileArchiver.js.map +1 -1
  6. package/lib/bundles/NEST_TEMPLATE.js +5 -5
  7. package/lib/bundles/NEST_TEMPLATE.js.map +1 -1
  8. package/lib/bundles/SDK_TEMPLATE.js +4 -4
  9. package/lib/bundles/SDK_TEMPLATE.js.map +1 -1
  10. package/lib/executable/NestiaMigrateCommander.js +14 -5
  11. package/lib/executable/NestiaMigrateCommander.js.map +1 -1
  12. package/lib/executable/NestiaMigrateInquirer.js.map +1 -1
  13. package/lib/factories/TypeLiteralFactory.d.ts +1 -1
  14. package/lib/factories/TypeLiteralFactory.js +49 -13
  15. package/lib/factories/TypeLiteralFactory.js.map +1 -1
  16. package/lib/index.js.map +1 -1
  17. package/lib/index.mjs +177 -24588
  18. package/lib/index.mjs.map +1 -1
  19. package/lib/internal/ts.d.ts +32 -0
  20. package/lib/internal/ts.js +37 -0
  21. package/lib/internal/ts.js.map +1 -0
  22. package/lib/programmers/NestiaMigrateApiFileProgrammer.d.ts +1 -1
  23. package/lib/programmers/NestiaMigrateApiFileProgrammer.js +2 -5
  24. package/lib/programmers/NestiaMigrateApiFileProgrammer.js.map +1 -1
  25. package/lib/programmers/NestiaMigrateApiFunctionProgrammer.d.ts +1 -1
  26. package/lib/programmers/NestiaMigrateApiFunctionProgrammer.js +87 -49
  27. package/lib/programmers/NestiaMigrateApiFunctionProgrammer.js.map +1 -1
  28. package/lib/programmers/NestiaMigrateApiNamespaceProgrammer.d.ts +2 -2
  29. package/lib/programmers/NestiaMigrateApiNamespaceProgrammer.js +80 -82
  30. package/lib/programmers/NestiaMigrateApiNamespaceProgrammer.js.map +1 -1
  31. package/lib/programmers/NestiaMigrateApiProgrammer.js +3 -2
  32. package/lib/programmers/NestiaMigrateApiProgrammer.js.map +1 -1
  33. package/lib/programmers/NestiaMigrateApiSimulationProgrammer.d.ts +2 -2
  34. package/lib/programmers/NestiaMigrateApiSimulationProgrammer.js +39 -37
  35. package/lib/programmers/NestiaMigrateApiSimulationProgrammer.js.map +1 -1
  36. package/lib/programmers/NestiaMigrateApiStartProgrammer.js +33 -35
  37. package/lib/programmers/NestiaMigrateApiStartProgrammer.js.map +1 -1
  38. package/lib/programmers/NestiaMigrateDtoProgrammer.d.ts +1 -1
  39. package/lib/programmers/NestiaMigrateDtoProgrammer.js +17 -19
  40. package/lib/programmers/NestiaMigrateDtoProgrammer.js.map +1 -1
  41. package/lib/programmers/NestiaMigrateE2eFileProgrammer.d.ts +1 -1
  42. package/lib/programmers/NestiaMigrateE2eFileProgrammer.js +26 -23
  43. package/lib/programmers/NestiaMigrateE2eFileProgrammer.js.map +1 -1
  44. package/lib/programmers/NestiaMigrateE2eProgrammer.js.map +1 -1
  45. package/lib/programmers/NestiaMigrateImportProgrammer.d.ts +1 -1
  46. package/lib/programmers/NestiaMigrateImportProgrammer.js +11 -14
  47. package/lib/programmers/NestiaMigrateImportProgrammer.js.map +1 -1
  48. package/lib/programmers/NestiaMigrateNestControllerProgrammer.d.ts +1 -1
  49. package/lib/programmers/NestiaMigrateNestControllerProgrammer.js +16 -18
  50. package/lib/programmers/NestiaMigrateNestControllerProgrammer.js.map +1 -1
  51. package/lib/programmers/NestiaMigrateNestMethodProgrammer.d.ts +1 -1
  52. package/lib/programmers/NestiaMigrateNestMethodProgrammer.js +128 -132
  53. package/lib/programmers/NestiaMigrateNestMethodProgrammer.js.map +1 -1
  54. package/lib/programmers/NestiaMigrateNestModuleProgrammer.d.ts +1 -1
  55. package/lib/programmers/NestiaMigrateNestModuleProgrammer.js +10 -9
  56. package/lib/programmers/NestiaMigrateNestModuleProgrammer.js.map +1 -1
  57. package/lib/programmers/NestiaMigrateNestProgrammer.js +3 -2
  58. package/lib/programmers/NestiaMigrateNestProgrammer.js.map +1 -1
  59. package/lib/programmers/NestiaMigrateSchemaProgrammer.d.ts +2 -2
  60. package/lib/programmers/NestiaMigrateSchemaProgrammer.js +114 -81
  61. package/lib/programmers/NestiaMigrateSchemaProgrammer.js.map +1 -1
  62. package/lib/structures/INestiaMigrateConfig.d.ts +1 -1
  63. package/lib/utils/FilePrinter.d.ts +2 -2
  64. package/lib/utils/FilePrinter.js +8 -10
  65. package/lib/utils/FilePrinter.js.map +1 -1
  66. package/lib/utils/MapUtil.js.map +1 -1
  67. package/lib/utils/StringUtil.d.ts +0 -1
  68. package/lib/utils/StringUtil.js +0 -1
  69. package/lib/utils/StringUtil.js.map +1 -1
  70. package/package.json +9 -11
  71. package/src/NestiaMigrateApplication.ts +29 -30
  72. package/src/bundles/NEST_TEMPLATE.ts +5 -5
  73. package/src/bundles/SDK_TEMPLATE.ts +4 -4
  74. package/src/executable/NestiaMigrateCommander.ts +15 -4
  75. package/src/executable/bundle.js +237 -4
  76. package/src/factories/TypeLiteralFactory.ts +22 -16
  77. package/src/internal/ts.ts +94 -0
  78. package/src/programmers/NestiaMigrateApiFileProgrammer.ts +7 -4
  79. package/src/programmers/NestiaMigrateApiFunctionProgrammer.ts +88 -68
  80. package/src/programmers/NestiaMigrateApiNamespaceProgrammer.ts +122 -111
  81. package/src/programmers/NestiaMigrateApiProgrammer.ts +6 -5
  82. package/src/programmers/NestiaMigrateApiSimulationProgrammer.ts +67 -61
  83. package/src/programmers/NestiaMigrateApiStartProgrammer.ts +46 -45
  84. package/src/programmers/NestiaMigrateDtoProgrammer.ts +4 -3
  85. package/src/programmers/NestiaMigrateE2eFileProgrammer.ts +33 -30
  86. package/src/programmers/NestiaMigrateE2eProgrammer.ts +1 -1
  87. package/src/programmers/NestiaMigrateImportProgrammer.ts +20 -19
  88. package/src/programmers/NestiaMigrateNestControllerProgrammer.ts +8 -7
  89. package/src/programmers/NestiaMigrateNestMethodProgrammer.ts +82 -77
  90. package/src/programmers/NestiaMigrateNestModuleProgrammer.ts +18 -17
  91. package/src/programmers/NestiaMigrateNestProgrammer.ts +6 -5
  92. package/src/programmers/NestiaMigrateSchemaProgrammer.ts +54 -42
  93. package/src/structures/INestiaMigrateConfig.ts +1 -1
  94. package/src/utils/FilePrinter.ts +9 -14
  95. package/src/utils/StringUtil.ts +0 -5
  96. package/lib/structures/INestiaMigrateDto.d.ts +0 -7
  97. package/lib/structures/INestiaMigrateDto.js +0 -3
  98. package/lib/structures/INestiaMigrateDto.js.map +0 -1
  99. package/lib/structures/INestiaMigrateProgram.d.ts +0 -9
  100. package/lib/structures/INestiaMigrateProgram.js +0 -3
  101. package/lib/structures/INestiaMigrateProgram.js.map +0 -1
  102. package/lib/structures/INestiaMigrateSchema.d.ts +0 -4
  103. package/lib/structures/INestiaMigrateSchema.js +0 -3
  104. package/lib/structures/INestiaMigrateSchema.js.map +0 -1
  105. package/lib/utils/SetupWizard.d.ts +0 -3
  106. package/lib/utils/SetupWizard.js +0 -18
  107. package/lib/utils/SetupWizard.js.map +0 -1
  108. package/lib/utils/openapi-down-convert/RefVisitor.d.ts +0 -52
  109. package/lib/utils/openapi-down-convert/RefVisitor.js +0 -102
  110. package/lib/utils/openapi-down-convert/RefVisitor.js.map +0 -1
  111. package/lib/utils/openapi-down-convert/converter.d.ts +0 -146
  112. package/lib/utils/openapi-down-convert/converter.js +0 -441
  113. package/lib/utils/openapi-down-convert/converter.js.map +0 -1
  114. package/src/structures/INestiaMigrateDto.ts +0 -8
  115. package/src/structures/INestiaMigrateProgram.ts +0 -11
  116. package/src/structures/INestiaMigrateSchema.ts +0 -4
  117. package/src/utils/SetupWizard.ts +0 -12
  118. package/src/utils/openapi-down-convert/RefVisitor.ts +0 -134
  119. package/src/utils/openapi-down-convert/converter.ts +0 -536
@@ -1,134 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
-
3
- /**
4
- * Recursively walk a JSON object and invoke a callback function on each `{
5
- * "$ref" : "path" }` object found
6
- */
7
-
8
- /**
9
- * Represents a JSON Reference object, such as `{"$ref":
10
- * "#/components/schemas/problemResponse" }`
11
- */
12
- export interface RefObject {
13
- $ref: string;
14
- }
15
-
16
- /** JsonNode represents a node within the OpenAPI object */
17
- export type JsonNode = object | [] | string | boolean | null | number;
18
-
19
- /** A JSON Schema object in an API def */
20
- export type SchemaObject = object;
21
-
22
- /** Function signature for the visitRefObjects callback */
23
- export type RefVisitor = (node: RefObject) => JsonNode;
24
- /** Function signature for the visitSchemaObjects callback */
25
- export type SchemaVisitor = (node: SchemaObject) => SchemaObject;
26
-
27
- /** /** Function signature for the walkObject callback */
28
- export type ObjectVisitor = (node: object) => JsonNode;
29
-
30
- /** Test if a JSON node is a `{ $ref: "uri" }` object */
31
- export function isRef(node: any): boolean {
32
- return (
33
- node !== null &&
34
- typeof node === "object" &&
35
- node.hasOwnProperty("$ref") &&
36
- typeof node["$ref"] === "string"
37
- );
38
- }
39
-
40
- /**
41
- * Walk a JSON object and apply `schemaCallback` when a JSON schema is found.
42
- * JSON Schema objects are items in components/schemas or in an item named
43
- * `schema`
44
- *
45
- * @param node A node in the OpenAPI document
46
- * @param schemaCallback The function to call on JSON schema objects
47
- * @returns The modified (annotated) node
48
- */
49
- export function visitSchemaObjects(
50
- node: any,
51
- schemaCallback: SchemaVisitor,
52
- ): any {
53
- const objectVisitor = (node: any): JsonNode => {
54
- if (node.hasOwnProperty("schema")) {
55
- const schema = node["schema"];
56
- if (schema != null && typeof schema === "object") {
57
- node["schema"] = schemaCallback(schema);
58
- }
59
- } else if (node.hasOwnProperty("schemas")) {
60
- const schemas = node["schemas"];
61
- if (schemas != null && typeof schemas === "object") {
62
- for (const schemaName in schemas) {
63
- const schema = schemas[schemaName];
64
- const newSchema = schemaCallback(schema);
65
- schemas[schemaName] = newSchema;
66
- }
67
- }
68
- }
69
- return node;
70
- };
71
- return walkObject(node, objectVisitor);
72
- }
73
-
74
- /**
75
- * Walk a JSON object and apply `refCallback` when a JSON `{$ref: url }` is
76
- * found
77
- *
78
- * @param node A node in the OpenAPI document
79
- * @param refCallback The function to call on JSON `$ref` objects
80
- * @returns The modified (annotated) node
81
- */
82
- export function visitRefObjects(node: any, refCallback: RefVisitor): any {
83
- const objectVisitor = (node: object): JsonNode => {
84
- if (isRef(node)) {
85
- return refCallback(node as RefObject);
86
- }
87
- return node;
88
- };
89
- return walkObject(node, objectVisitor);
90
- }
91
-
92
- /**
93
- * Walk a JSON object or array and apply objectCallback when a JSON object is
94
- * found
95
- *
96
- * @param node A node in the OpenAPI document
97
- * @param objectCallback The function to call on JSON objects
98
- * @param nav Tracks where we are in the original document
99
- * @returns The modified (annotated) node
100
- */
101
- export function walkObject(
102
- node: object,
103
- objectCallback: ObjectVisitor,
104
- ): JsonNode {
105
- return walkObj(node);
106
-
107
- function walkObj(node: any): JsonNode {
108
- const object = objectCallback(node);
109
- if (object !== null && typeof object === "object") {
110
- const keys = [...Object.keys(node)]; // make copy since this code may re-enter objects
111
- for (const key of keys) {
112
- const val = node[key];
113
- if (Array.isArray(val)) {
114
- node[key] = walkArray(val as []);
115
- } else if (val !== null && typeof val === "object") {
116
- node[key] = walkObj(val);
117
- }
118
- }
119
- }
120
- return object;
121
- }
122
-
123
- function walkArray(array: JsonNode[]): JsonNode[] {
124
- for (let index = 0; index < array.length; index += 1) {
125
- const val = array[index] as JsonNode;
126
- if (val !== null && typeof val === "object") {
127
- array[index] = walkObj(val) as object;
128
- } else if (Array.isArray(val)) {
129
- array[index] = walkArray(val as JsonNode[]) as [];
130
- }
131
- }
132
- return array;
133
- }
134
- }
@@ -1,536 +0,0 @@
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
- }