@nestia/migrate 7.3.0 → 7.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/bundles/NEST_TEMPLATE.js +2 -2
- package/lib/bundles/NEST_TEMPLATE.js.map +1 -1
- package/lib/bundles/SDK_TEMPLATE.js +1 -1
- package/lib/index.mjs +3 -3
- package/lib/utils/openapi-down-convert/RefVisitor.d.ts +28 -33
- package/lib/utils/openapi-down-convert/RefVisitor.js +20 -16
- package/lib/utils/openapi-down-convert/RefVisitor.js.map +1 -1
- package/lib/utils/openapi-down-convert/converter.d.ts +66 -58
- package/lib/utils/openapi-down-convert/converter.js +59 -49
- package/lib/utils/openapi-down-convert/converter.js.map +1 -1
- package/package.json +8 -8
- package/src/bundles/NEST_TEMPLATE.ts +2 -2
- package/src/bundles/SDK_TEMPLATE.ts +1 -1
- package/src/utils/openapi-down-convert/RefVisitor.ts +28 -33
- package/src/utils/openapi-down-convert/converter.ts +71 -62
@@ -1,44 +1,33 @@
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2
2
|
|
3
3
|
/**
|
4
|
-
* Recursively walk a JSON object and invoke a callback function
|
5
|
-
*
|
4
|
+
* Recursively walk a JSON object and invoke a callback function on each `{
|
5
|
+
* "$ref" : "path" }` object found
|
6
6
|
*/
|
7
7
|
|
8
8
|
/**
|
9
|
-
* Represents a JSON Reference object, such as
|
10
|
-
*
|
9
|
+
* Represents a JSON Reference object, such as `{"$ref":
|
10
|
+
* "#/components/schemas/problemResponse" }`
|
11
11
|
*/
|
12
12
|
export interface RefObject {
|
13
13
|
$ref: string;
|
14
14
|
}
|
15
15
|
|
16
|
-
/**
|
17
|
-
* JsonNode represents a node within the OpenAPI object
|
18
|
-
*/
|
16
|
+
/** JsonNode represents a node within the OpenAPI object */
|
19
17
|
export type JsonNode = object | [] | string | boolean | null | number;
|
20
18
|
|
21
19
|
/** A JSON Schema object in an API def */
|
22
20
|
export type SchemaObject = object;
|
23
21
|
|
24
|
-
/**
|
25
|
-
* Function signature for the visitRefObjects callback
|
26
|
-
*/
|
22
|
+
/** Function signature for the visitRefObjects callback */
|
27
23
|
export type RefVisitor = (node: RefObject) => JsonNode;
|
28
|
-
/**
|
29
|
-
* Function signature for the visitSchemaObjects callback
|
30
|
-
*/
|
24
|
+
/** Function signature for the visitSchemaObjects callback */
|
31
25
|
export type SchemaVisitor = (node: SchemaObject) => SchemaObject;
|
32
26
|
|
33
|
-
/**
|
34
|
-
/**
|
35
|
-
* Function signature for the walkObject callback
|
36
|
-
*/
|
27
|
+
/** /** Function signature for the walkObject callback */
|
37
28
|
export type ObjectVisitor = (node: object) => JsonNode;
|
38
29
|
|
39
|
-
/**
|
40
|
-
* Test if a JSON node is a `{ $ref: "uri" }` object
|
41
|
-
*/
|
30
|
+
/** Test if a JSON node is a `{ $ref: "uri" }` object */
|
42
31
|
export function isRef(node: any): boolean {
|
43
32
|
return (
|
44
33
|
node !== null &&
|
@@ -50,10 +39,12 @@ export function isRef(node: any): boolean {
|
|
50
39
|
|
51
40
|
/**
|
52
41
|
* Walk a JSON object and apply `schemaCallback` when a JSON schema is found.
|
53
|
-
* JSON Schema objects are items in components/schemas or in an item named
|
54
|
-
*
|
55
|
-
*
|
56
|
-
* @
|
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
|
57
48
|
*/
|
58
49
|
export function visitSchemaObjects(
|
59
50
|
node: any,
|
@@ -81,10 +72,12 @@ export function visitSchemaObjects(
|
|
81
72
|
}
|
82
73
|
|
83
74
|
/**
|
84
|
-
* Walk a JSON object and apply `refCallback` when a JSON `{$ref: url }` is
|
85
|
-
*
|
86
|
-
*
|
87
|
-
* @
|
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
|
88
81
|
*/
|
89
82
|
export function visitRefObjects(node: any, refCallback: RefVisitor): any {
|
90
83
|
const objectVisitor = (node: object): JsonNode => {
|
@@ -97,11 +90,13 @@ export function visitRefObjects(node: any, refCallback: RefVisitor): any {
|
|
97
90
|
}
|
98
91
|
|
99
92
|
/**
|
100
|
-
* Walk a JSON object or array and apply objectCallback when a JSON object is
|
101
|
-
*
|
102
|
-
*
|
103
|
-
* @param
|
104
|
-
* @
|
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
|
105
100
|
*/
|
106
101
|
export function walkObject(
|
107
102
|
node: object,
|
@@ -19,30 +19,29 @@ interface OpenAPI3 {
|
|
19
19
|
|
20
20
|
/** Options for the converter instantiation */
|
21
21
|
export interface ConverterOptions {
|
22
|
-
/**
|
22
|
+
/** If `true`, log conversion transformations to stderr */
|
23
23
|
verbose?: boolean;
|
24
|
-
/**
|
25
|
-
* [Spectral issue
|
24
|
+
/**
|
25
|
+
* If `true`, remove `id` values in schema examples, to bypass [Spectral issue
|
26
|
+
* 2081](https://github.com/stoplightio/spectral/issues/2081)
|
26
27
|
*/
|
27
28
|
deleteExampleWithId?: boolean;
|
28
29
|
/** If `true`, replace a `$ref` object that has siblings into an `allOf` */
|
29
30
|
allOfTransform?: boolean;
|
30
31
|
|
31
|
-
/**
|
32
|
-
* The authorizationUrl for openIdConnect -> oauth2 transformation
|
33
|
-
*/
|
32
|
+
/** The authorizationUrl for openIdConnect -> oauth2 transformation */
|
34
33
|
authorizationUrl?: string;
|
35
34
|
/** The tokenUrl for openIdConnect -> oauth2 transformation */
|
36
35
|
tokenUrl?: string;
|
37
|
-
/**
|
38
|
-
* This is a simple map in the
|
39
|
-
* `{ scope1: "description of scope1", ... }`
|
36
|
+
/**
|
37
|
+
* Name of YAML/JSON file with scope descriptions. This is a simple map in the
|
38
|
+
* format `{ scope1: "description of scope1", ... }`
|
40
39
|
*/
|
41
40
|
scopeDescriptionFile?: string;
|
42
|
-
/**
|
43
|
-
*
|
44
|
-
*
|
45
|
-
* comments.
|
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.
|
46
45
|
*/
|
47
46
|
convertSchemaComments?: boolean;
|
48
47
|
}
|
@@ -61,7 +60,9 @@ export class Converter {
|
|
61
60
|
|
62
61
|
/**
|
63
62
|
* Construct a new Converter
|
64
|
-
*
|
63
|
+
*
|
64
|
+
* @throws Error if the scopeDescriptionFile (if specified) cannot be read or
|
65
|
+
* parsed as YAML/JSON
|
65
66
|
*/
|
66
67
|
constructor(openapiDocument: object, options?: ConverterOptions) {
|
67
68
|
this.openapi30 = Converter.deepClone(openapiDocument) as OpenAPI3;
|
@@ -75,7 +76,9 @@ export class Converter {
|
|
75
76
|
this.convertSchemaComments = !!options?.convertSchemaComments;
|
76
77
|
}
|
77
78
|
|
78
|
-
/**
|
79
|
+
/**
|
80
|
+
* Load the scopes.yaml file and save in this.scopeDescriptions
|
81
|
+
*
|
79
82
|
* @throws Error if the file cannot be read or parsed as YAML/JSON
|
80
83
|
*/
|
81
84
|
private loadScopeDescriptions(scopeDescriptionFile?: string) {
|
@@ -85,8 +88,9 @@ export class Converter {
|
|
85
88
|
}
|
86
89
|
|
87
90
|
/**
|
88
|
-
* Log a message
|
89
|
-
*
|
91
|
+
* Log a message to console.warn stream if verbose is true
|
92
|
+
*
|
93
|
+
* @param message Parameters for console.warn
|
90
94
|
*/
|
91
95
|
private log(...message: any[]) {
|
92
96
|
if (this.verbose) {
|
@@ -95,9 +99,10 @@ export class Converter {
|
|
95
99
|
}
|
96
100
|
|
97
101
|
/**
|
98
|
-
* Log a message
|
99
|
-
* if it does not already have that text.
|
100
|
-
*
|
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
|
101
106
|
*/
|
102
107
|
private warn(...message: any[]) {
|
103
108
|
if (!message[0].startsWith("Warning")) {
|
@@ -107,10 +112,11 @@ export class Converter {
|
|
107
112
|
}
|
108
113
|
|
109
114
|
/**
|
110
|
-
* Log an error message to `console.error` stream. Prefix the message string
|
111
|
-
* if it does not already start with `'Error'`. Increments the
|
112
|
-
* the CLI to throw an Error when done.
|
113
|
-
*
|
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`
|
114
120
|
*/
|
115
121
|
private error(...message: any[]) {
|
116
122
|
if (!message[0].startsWith("Error")) {
|
@@ -122,7 +128,8 @@ export class Converter {
|
|
122
128
|
|
123
129
|
/**
|
124
130
|
* Convert the OpenAPI document to 3.0
|
125
|
-
*
|
131
|
+
*
|
132
|
+
* @returns The converted document. The input is not modified.
|
126
133
|
*/
|
127
134
|
public convert(): object {
|
128
135
|
this.log("Converting from OpenAPI 3.1 to 3.0");
|
@@ -153,8 +160,8 @@ export class Converter {
|
|
153
160
|
|
154
161
|
/**
|
155
162
|
* OpenAPI 3.1 uses JSON Schema 2020-12 which allows schema `examples`;
|
156
|
-
* OpenAPI 3.0 uses JSON Scheme Draft 7 which only allows `example`.
|
157
|
-
*
|
163
|
+
* OpenAPI 3.0 uses JSON Scheme Draft 7 which only allows `example`. Replace
|
164
|
+
* all `examples` with `example`, using `examples[0]`
|
158
165
|
*/
|
159
166
|
convertJsonSchemaExamples() {
|
160
167
|
const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
|
@@ -205,9 +212,9 @@ export class Converter {
|
|
205
212
|
}
|
206
213
|
|
207
214
|
/**
|
208
|
-
* OpenAPI 3.1 uses JSON Schema 2020-12 which allows `const`
|
209
|
-
*
|
210
|
-
*
|
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 ]`
|
211
218
|
*/
|
212
219
|
convertConstToEnum() {
|
213
220
|
const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
|
@@ -223,8 +230,8 @@ export class Converter {
|
|
223
230
|
}
|
224
231
|
|
225
232
|
/**
|
226
|
-
* Convert 2-element type arrays containing 'null' to
|
227
|
-
*
|
233
|
+
* Convert 2-element type arrays containing 'null' to string type and
|
234
|
+
* `nullable: true`
|
228
235
|
*/
|
229
236
|
convertNullableTypeArray() {
|
230
237
|
const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
|
@@ -298,15 +305,15 @@ export class Converter {
|
|
298
305
|
|
299
306
|
/**
|
300
307
|
* Convert
|
301
|
-
*
|
302
|
-
*
|
303
|
-
*
|
304
|
-
*
|
305
|
-
*
|
306
|
-
*
|
307
|
-
*
|
308
|
-
*
|
309
|
-
*
|
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`.
|
310
317
|
*/
|
311
318
|
convertJsonSchemaContentMediaType() {
|
312
319
|
const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
|
@@ -342,15 +349,15 @@ export class Converter {
|
|
342
349
|
|
343
350
|
/**
|
344
351
|
* Convert
|
345
|
-
*
|
346
|
-
*
|
347
|
-
*
|
348
|
-
*
|
349
|
-
*
|
350
|
-
*
|
351
|
-
*
|
352
|
-
*
|
353
|
-
* and it is not `byte`.
|
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`.
|
354
361
|
*/
|
355
362
|
convertJsonSchemaContentEncoding() {
|
356
363
|
const schemaVisitor: SchemaVisitor = (schema: any): SchemaObject => {
|
@@ -394,11 +401,10 @@ export class Converter {
|
|
394
401
|
}
|
395
402
|
|
396
403
|
/**
|
397
|
-
* OpenAPI 3.1 defines a new `openIdConnect` security scheme.
|
398
|
-
*
|
399
|
-
*
|
400
|
-
*
|
401
|
-
* URLs to the `authorizationUrl` and `tokenUrl` of `oauth2`.
|
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`.
|
402
408
|
*/
|
403
409
|
convertSecuritySchemes() {
|
404
410
|
const oauth2Scopes = (schemeName: string): object => {
|
@@ -454,9 +460,11 @@ export class Converter {
|
|
454
460
|
}
|
455
461
|
|
456
462
|
/**
|
457
|
-
* Find remaining OpenAPI 3.0 [Reference
|
458
|
-
*
|
459
|
-
*
|
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.
|
460
468
|
*/
|
461
469
|
simplifyNonSchemaRef() {
|
462
470
|
visitRefObjects(this.openapi30, (node: any): JsonNode => {
|
@@ -489,10 +497,11 @@ export class Converter {
|
|
489
497
|
|
490
498
|
convertSchemaRef() {
|
491
499
|
/**
|
492
|
-
* In a JSON Schema, replace `{ blah blah, $ref: "uri"}`
|
493
|
-
*
|
494
|
-
*
|
495
|
-
* or in
|
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)
|
496
505
|
*/
|
497
506
|
const simplifyRefObjectsInSchemas = (
|
498
507
|
object: SchemaObject,
|