@scalar/openapi-upgrader 0.2.0 → 0.2.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.
- package/CHANGELOG.md +6 -0
- package/dist/2.0-to-3.0/index.js +1 -5
- package/dist/2.0-to-3.0/upgrade-from-two-to-three.js +641 -550
- package/dist/3.0-to-3.1/index.js +1 -5
- package/dist/3.0-to-3.1/upgrade-from-three-to-three-one.js +139 -111
- package/dist/3.1-to-3.2/index.js +1 -5
- package/dist/3.1-to-3.2/upgrade-from-three-one-to-three-two.js +94 -64
- package/dist/helpers/traverse.js +21 -22
- package/dist/index.js +1 -5
- package/dist/upgrade.bench.js +42 -27
- package/dist/upgrade.js +19 -20
- package/package.json +6 -8
- package/dist/2.0-to-3.0/index.js.map +0 -7
- package/dist/2.0-to-3.0/upgrade-from-two-to-three.js.map +0 -7
- package/dist/3.0-to-3.1/index.js.map +0 -7
- package/dist/3.0-to-3.1/upgrade-from-three-to-three-one.js.map +0 -7
- package/dist/3.1-to-3.2/index.js.map +0 -7
- package/dist/3.1-to-3.2/upgrade-from-three-one-to-three-two.js.map +0 -7
- package/dist/helpers/traverse.js.map +0 -7
- package/dist/index.js.map +0 -7
- package/dist/upgrade.bench.js.map +0 -7
- package/dist/upgrade.js.map +0 -7
|
@@ -1,631 +1,722 @@
|
|
|
1
|
-
import { traverse } from
|
|
2
|
-
const DEFAULT_MEDIA_TYPE =
|
|
1
|
+
import { traverse } from '../helpers/traverse.js';
|
|
2
|
+
const DEFAULT_MEDIA_TYPE = 'application/json';
|
|
3
|
+
/** Extracts and removes x-example and x-examples extensions from an object */
|
|
3
4
|
function extractXExampleExtensions(obj) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const xExample = obj['x-example'];
|
|
6
|
+
const xExamples = obj['x-examples'];
|
|
7
|
+
delete obj['x-example'];
|
|
8
|
+
delete obj['x-examples'];
|
|
9
|
+
return { xExample, xExamples };
|
|
9
10
|
}
|
|
11
|
+
/** Checks if a value is a non-null, non-array object with at least one entry */
|
|
10
12
|
function isNonEmptyObject(value) {
|
|
11
|
-
|
|
13
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) && Object.keys(value).length > 0;
|
|
12
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Checks if a value looks like a collection of named examples (all values are objects).
|
|
17
|
+
* This helps distinguish between:
|
|
18
|
+
* - A single example: { message: 'OK', type: 'success' } - values are primitives
|
|
19
|
+
* - Named examples: { 'my-example': { message: 'OK' } } - values are objects
|
|
20
|
+
*/
|
|
13
21
|
function isNamedExamplesCollection(value) {
|
|
14
|
-
|
|
22
|
+
return (isNonEmptyObject(value) &&
|
|
23
|
+
Object.values(value).every((v) => typeof v === 'object' && v !== null && !Array.isArray(v)));
|
|
15
24
|
}
|
|
16
|
-
|
|
25
|
+
/** The allowed properties for an OpenAPI 3.x ExampleObject */
|
|
26
|
+
const EXAMPLE_OBJECT_PROPERTIES = new Set(['summary', 'description', 'value', 'externalValue']);
|
|
27
|
+
/**
|
|
28
|
+
* Checks if a value is a valid OpenAPI 3.x ExampleObject.
|
|
29
|
+
*
|
|
30
|
+
* An ExampleObject must have a `value` (or `externalValue`) property and can only contain
|
|
31
|
+
* properties from the allowed set: `summary`, `description`, `value`, `externalValue`.
|
|
32
|
+
*
|
|
33
|
+
* This prevents false positives when user's example data happens to have a `value` property
|
|
34
|
+
* (e.g., `{ value: "some data", count: 5 }` should NOT be treated as an ExampleObject).
|
|
35
|
+
*/
|
|
17
36
|
function isExampleObject(value) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
37
|
+
if (typeof value !== 'object' || value === null) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
const obj = value;
|
|
41
|
+
const hasValueOrExternalValue = 'value' in obj || 'externalValue' in obj;
|
|
42
|
+
const onlyHasAllowedProperties = Object.keys(obj).every((key) => EXAMPLE_OBJECT_PROPERTIES.has(key));
|
|
43
|
+
return hasValueOrExternalValue && onlyHasAllowedProperties;
|
|
25
44
|
}
|
|
45
|
+
/** Wraps a value as an ExampleObject, preserving existing structure if valid */
|
|
26
46
|
function wrapAsExampleObject(value) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
47
|
+
if (isExampleObject(value)) {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
return { value };
|
|
31
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* True if the key looks like a MIME media type (e.g. application/json, text/plain).
|
|
54
|
+
* Used to distinguish media-type example keys from named example keys when migrating
|
|
55
|
+
* Swagger 2.0 examples to OpenAPI 3.0 content. Requires exactly one slash and
|
|
56
|
+
* token-style type/subtype (no spaces or semicolons) to avoid false positives
|
|
57
|
+
* for named keys that contain a slash (e.g. "Error 404/Not Found").
|
|
58
|
+
*/
|
|
32
59
|
const MEDIA_TYPE_KEY_PATTERN = /^[a-zA-Z0-9*+.-]+\/[a-zA-Z0-9*+.+-]+$/;
|
|
33
60
|
function isMediaTypeKey(key) {
|
|
34
|
-
|
|
61
|
+
return MEDIA_TYPE_KEY_PATTERN.test(key);
|
|
35
62
|
}
|
|
63
|
+
/** Transforms x-example entries to OpenAPI 3.x examples format */
|
|
36
64
|
function transformXExampleToExamples(xExample) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
},
|
|
42
|
-
{}
|
|
43
|
-
);
|
|
65
|
+
return Object.entries(xExample).reduce((acc, [key, value]) => {
|
|
66
|
+
acc[key] = { value };
|
|
67
|
+
return acc;
|
|
68
|
+
}, {});
|
|
44
69
|
}
|
|
70
|
+
/** Update the flow names to OpenAPI 3.1.0 format */
|
|
45
71
|
const upgradeFlow = (flow) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
72
|
+
switch (flow) {
|
|
73
|
+
case 'application':
|
|
74
|
+
return 'clientCredentials';
|
|
75
|
+
case 'accessCode':
|
|
76
|
+
return 'authorizationCode';
|
|
77
|
+
case 'implicit':
|
|
78
|
+
return 'implicit';
|
|
79
|
+
case 'password':
|
|
80
|
+
return 'password';
|
|
81
|
+
default:
|
|
82
|
+
return flow;
|
|
83
|
+
}
|
|
58
84
|
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
delete document.schemes;
|
|
74
|
-
delete document.host;
|
|
75
|
-
} else if (document.basePath) {
|
|
76
|
-
document.servers = [{ url: document.basePath }];
|
|
77
|
-
delete document.basePath;
|
|
78
|
-
}
|
|
79
|
-
if (document.definitions) {
|
|
80
|
-
document.components = Object.assign({}, document.components, {
|
|
81
|
-
schemas: document.definitions
|
|
82
|
-
});
|
|
83
|
-
delete document.definitions;
|
|
84
|
-
document = traverse(document, (schema) => {
|
|
85
|
-
if (typeof schema.$ref === "string" && schema.$ref.startsWith("#/definitions/")) {
|
|
86
|
-
schema.$ref = schema.$ref.replace(/^#\/definitions\//, "#/components/schemas/");
|
|
87
|
-
}
|
|
88
|
-
return schema;
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
document = traverse(document, (schema) => {
|
|
92
|
-
if (schema.type === "file") {
|
|
93
|
-
schema.type = "string";
|
|
94
|
-
schema.format = "binary";
|
|
85
|
+
/**
|
|
86
|
+
* Upgrade Swagger 2.0 to OpenAPI 3.0
|
|
87
|
+
*
|
|
88
|
+
* https://swagger.io/blog/news/whats-new-in-openapi-3-0/
|
|
89
|
+
*/
|
|
90
|
+
export function upgradeFromTwoToThree(originalSpecification) {
|
|
91
|
+
let document = originalSpecification;
|
|
92
|
+
// Version
|
|
93
|
+
if (document !== null &&
|
|
94
|
+
typeof document === 'object' &&
|
|
95
|
+
typeof document.swagger === 'string' &&
|
|
96
|
+
document.swagger?.startsWith('2.0')) {
|
|
97
|
+
document.openapi = '3.0.4';
|
|
98
|
+
delete document.swagger;
|
|
95
99
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
schema.$ref = schema.$ref.replace(/^#\/parameters\//, "#/components/parameters/");
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return schema;
|
|
113
|
-
});
|
|
114
|
-
document.components ??= {};
|
|
115
|
-
const params = {};
|
|
116
|
-
const bodyParams = {};
|
|
117
|
-
const parameters = document.parameters && typeof document.parameters === "object" ? document.parameters : {};
|
|
118
|
-
for (const [name, param] of Object.entries(parameters)) {
|
|
119
|
-
if (param && typeof param === "object") {
|
|
120
|
-
if ("$ref" in param) {
|
|
121
|
-
const convertedParam = transformParameterObject(param);
|
|
122
|
-
params[name] = convertedParam;
|
|
123
|
-
} else if ("in" in param) {
|
|
124
|
-
if (param.in === "body") {
|
|
125
|
-
bodyParams[name] = migrateBodyParameter(
|
|
126
|
-
param,
|
|
127
|
-
document.consumes ?? [DEFAULT_MEDIA_TYPE]
|
|
128
|
-
);
|
|
129
|
-
} else if (param.in === "formData") {
|
|
130
|
-
bodyParams[name] = migrateFormDataParameter(
|
|
131
|
-
[param],
|
|
132
|
-
document.consumes
|
|
133
|
-
);
|
|
134
|
-
} else {
|
|
135
|
-
const convertedParam = transformParameterObject(param);
|
|
136
|
-
params[name] = convertedParam;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
100
|
+
else {
|
|
101
|
+
// Skip if it's something else than 3.0.x
|
|
102
|
+
return document;
|
|
103
|
+
}
|
|
104
|
+
// Servers
|
|
105
|
+
if (document.host) {
|
|
106
|
+
const schemes = Array.isArray(document.schemes) && document.schemes?.length ? document.schemes : ['http'];
|
|
107
|
+
document.servers = schemes.map((scheme) => ({
|
|
108
|
+
url: `${scheme}://${document.host}${document.basePath ?? ''}`,
|
|
109
|
+
}));
|
|
110
|
+
delete document.basePath;
|
|
111
|
+
delete document.schemes;
|
|
112
|
+
delete document.host;
|
|
140
113
|
}
|
|
141
|
-
if (
|
|
142
|
-
|
|
143
|
-
|
|
114
|
+
else if (document.basePath) {
|
|
115
|
+
document.servers = [{ url: document.basePath }];
|
|
116
|
+
delete document.basePath;
|
|
144
117
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
118
|
+
// Schemas
|
|
119
|
+
if (document.definitions) {
|
|
120
|
+
document.components = Object.assign({}, document.components, {
|
|
121
|
+
schemas: document.definitions,
|
|
122
|
+
});
|
|
123
|
+
delete document.definitions;
|
|
124
|
+
// Rewrite $refs to definitions
|
|
125
|
+
document = traverse(document, (schema) => {
|
|
126
|
+
// Rewrite $refs to components
|
|
127
|
+
if (typeof schema.$ref === 'string' && schema.$ref.startsWith('#/definitions/')) {
|
|
128
|
+
schema.$ref = schema.$ref.replace(/^#\/definitions\//, '#/components/schemas/');
|
|
129
|
+
}
|
|
130
|
+
return schema;
|
|
131
|
+
});
|
|
148
132
|
}
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
if (Object.hasOwn(document, "responses") && typeof document.responses === "object" && document.responses !== null) {
|
|
133
|
+
// Transform file type to string with binary format
|
|
152
134
|
document = traverse(document, (schema) => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
135
|
+
if (schema.type === 'file') {
|
|
136
|
+
schema.type = 'string';
|
|
137
|
+
schema.format = 'binary';
|
|
138
|
+
}
|
|
139
|
+
return schema;
|
|
157
140
|
});
|
|
158
|
-
document
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
} else {
|
|
166
|
-
const responseObj = response;
|
|
167
|
-
const produces = document.produces ?? [DEFAULT_MEDIA_TYPE];
|
|
168
|
-
if (responseObj.schema) {
|
|
169
|
-
if (typeof responseObj.content !== "object") {
|
|
170
|
-
responseObj.content = {};
|
|
171
|
-
}
|
|
172
|
-
for (const type of produces) {
|
|
173
|
-
;
|
|
174
|
-
responseObj.content[type] = {
|
|
175
|
-
schema: responseObj.schema
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
delete responseObj.schema;
|
|
179
|
-
}
|
|
180
|
-
if (responseObj.examples && typeof responseObj.examples === "object") {
|
|
181
|
-
if (typeof responseObj.content !== "object") {
|
|
182
|
-
responseObj.content = {};
|
|
183
|
-
}
|
|
184
|
-
const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
|
|
185
|
-
for (const [key, exampleValue] of Object.entries(responseObj.examples)) {
|
|
186
|
-
if (isMediaTypeKey(key)) {
|
|
187
|
-
if (typeof responseObj.content[key] !== "object") {
|
|
188
|
-
;
|
|
189
|
-
responseObj.content[key] = {};
|
|
141
|
+
if (Object.hasOwn(document, 'parameters')) {
|
|
142
|
+
// update all the $refs before we do any transformations
|
|
143
|
+
document = traverse(document, (schema) => {
|
|
144
|
+
if (typeof schema.$ref === 'string' && schema.$ref.startsWith('#/parameters/')) {
|
|
145
|
+
const schemaName = schema.$ref.split('/')[2];
|
|
146
|
+
if (!schemaName) {
|
|
147
|
+
return schema;
|
|
190
148
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if (typeof
|
|
195
|
-
|
|
196
|
-
responseObj.content[defaultMediaType] = {};
|
|
149
|
+
const param = document.parameters && typeof document.parameters === 'object' && schemaName in document.parameters
|
|
150
|
+
? document.parameters[schemaName]
|
|
151
|
+
: undefined;
|
|
152
|
+
if (param && typeof param === 'object' && 'in' in param && (param.in === 'body' || param.in === 'formData')) {
|
|
153
|
+
schema.$ref = schema.$ref.replace(/^#\/parameters\//, '#/components/requestBodies/');
|
|
197
154
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
mediaEntry.examples = {};
|
|
155
|
+
else {
|
|
156
|
+
schema.$ref = schema.$ref.replace(/^#\/parameters\//, '#/components/parameters/');
|
|
201
157
|
}
|
|
202
|
-
;
|
|
203
|
-
mediaEntry.examples[key] = wrapAsExampleObject(exampleValue);
|
|
204
|
-
}
|
|
205
158
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
159
|
+
return schema;
|
|
160
|
+
});
|
|
161
|
+
document.components ??= {};
|
|
162
|
+
const params = {};
|
|
163
|
+
const bodyParams = {};
|
|
164
|
+
const parameters = document.parameters && typeof document.parameters === 'object'
|
|
165
|
+
? document.parameters
|
|
166
|
+
: {};
|
|
167
|
+
for (const [name, param] of Object.entries(parameters)) {
|
|
168
|
+
if (param && typeof param === 'object') {
|
|
169
|
+
// Handle reference objects
|
|
170
|
+
if ('$ref' in param) {
|
|
171
|
+
const convertedParam = transformParameterObject(param);
|
|
172
|
+
params[name] = convertedParam;
|
|
173
|
+
}
|
|
174
|
+
else if ('in' in param) {
|
|
175
|
+
if (param.in === 'body') {
|
|
176
|
+
bodyParams[name] = migrateBodyParameter(param, document.consumes ?? [DEFAULT_MEDIA_TYPE]);
|
|
177
|
+
}
|
|
178
|
+
else if (param.in === 'formData') {
|
|
179
|
+
bodyParams[name] = migrateFormDataParameter([param], document.consumes);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
const convertedParam = transformParameterObject(param);
|
|
183
|
+
params[name] = convertedParam;
|
|
184
|
+
}
|
|
216
185
|
}
|
|
217
|
-
|
|
218
|
-
},
|
|
219
|
-
{}
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
migratedResponses[name] = responseObj;
|
|
186
|
+
}
|
|
223
187
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
;
|
|
228
|
-
document.components.responses = migratedResponses;
|
|
229
|
-
}
|
|
230
|
-
delete document.responses;
|
|
231
|
-
}
|
|
232
|
-
if (typeof document.paths === "object") {
|
|
233
|
-
for (const path in document.paths) {
|
|
234
|
-
if (Object.hasOwn(document.paths, path)) {
|
|
235
|
-
const pathItem = document.paths && typeof document.paths === "object" && path in document.paths ? document.paths[path] : void 0;
|
|
236
|
-
if (!pathItem || typeof pathItem !== "object") {
|
|
237
|
-
continue;
|
|
188
|
+
if (Object.keys(params).length > 0) {
|
|
189
|
+
;
|
|
190
|
+
document.components.parameters = params;
|
|
238
191
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
operationItem.requestBody = requestBodyObject;
|
|
252
|
-
}
|
|
253
|
-
if (operationItem.parameters) {
|
|
254
|
-
const migrationResult = migrateParameters(
|
|
255
|
-
operationItem.parameters,
|
|
256
|
-
operationItem.consumes ?? document.consumes ?? [DEFAULT_MEDIA_TYPE]
|
|
257
|
-
);
|
|
258
|
-
operationItem.parameters = migrationResult.parameters;
|
|
259
|
-
if (migrationResult.requestBody) {
|
|
260
|
-
operationItem.requestBody = migrationResult.requestBody;
|
|
261
|
-
}
|
|
192
|
+
if (Object.keys(bodyParams).length > 0) {
|
|
193
|
+
;
|
|
194
|
+
document.components.requestBodies = bodyParams;
|
|
195
|
+
}
|
|
196
|
+
delete document.parameters;
|
|
197
|
+
}
|
|
198
|
+
// Handle global responses defined in #/responses
|
|
199
|
+
if (Object.hasOwn(document, 'responses') && typeof document.responses === 'object' && document.responses !== null) {
|
|
200
|
+
// Update all $refs from #/responses/ to #/components/responses/
|
|
201
|
+
document = traverse(document, (schema) => {
|
|
202
|
+
if (typeof schema.$ref === 'string' && schema.$ref.startsWith('#/responses/')) {
|
|
203
|
+
schema.$ref = schema.$ref.replace(/^#\/responses\//, '#/components/responses/');
|
|
262
204
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
205
|
+
return schema;
|
|
206
|
+
});
|
|
207
|
+
document.components ??= {};
|
|
208
|
+
const migratedResponses = {};
|
|
209
|
+
const responses = document.responses;
|
|
210
|
+
for (const [name, response] of Object.entries(responses)) {
|
|
211
|
+
if (response && typeof response === 'object') {
|
|
212
|
+
// Handle reference objects
|
|
213
|
+
if ('$ref' in response) {
|
|
214
|
+
migratedResponses[name] = response;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
// Transform the response object
|
|
218
|
+
const responseObj = response;
|
|
219
|
+
const produces = document.produces ?? [DEFAULT_MEDIA_TYPE];
|
|
220
|
+
// Transform schema to content
|
|
221
|
+
if (responseObj.schema) {
|
|
222
|
+
if (typeof responseObj.content !== 'object') {
|
|
223
|
+
responseObj.content = {};
|
|
276
224
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
if (typeof responseItem.content !== "object") {
|
|
285
|
-
responseItem.content = {};
|
|
225
|
+
for (const type of produces) {
|
|
226
|
+
;
|
|
227
|
+
responseObj.content[type] = {
|
|
228
|
+
schema: responseObj.schema,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
delete responseObj.schema;
|
|
286
232
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
233
|
+
// Transform examples from Swagger 2.0 to OpenAPI 3.0 format
|
|
234
|
+
if (responseObj.examples && typeof responseObj.examples === 'object') {
|
|
235
|
+
if (typeof responseObj.content !== 'object') {
|
|
236
|
+
responseObj.content = {};
|
|
237
|
+
}
|
|
238
|
+
const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
|
|
239
|
+
for (const [key, exampleValue] of Object.entries(responseObj.examples)) {
|
|
240
|
+
// Media-type keys (e.g. application/json) go to content[key]; named example keys go to content[defaultMediaType].examples[key].
|
|
241
|
+
if (isMediaTypeKey(key)) {
|
|
242
|
+
if (typeof responseObj.content[key] !== 'object') {
|
|
243
|
+
;
|
|
244
|
+
responseObj.content[key] = {};
|
|
245
|
+
}
|
|
246
|
+
;
|
|
247
|
+
responseObj.content[key].example =
|
|
248
|
+
exampleValue;
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
if (typeof responseObj.content[defaultMediaType] !== 'object') {
|
|
252
|
+
;
|
|
253
|
+
responseObj.content[defaultMediaType] = {};
|
|
254
|
+
}
|
|
255
|
+
const mediaEntry = responseObj.content[defaultMediaType];
|
|
256
|
+
if (typeof mediaEntry.examples !== 'object') {
|
|
257
|
+
mediaEntry.examples = {};
|
|
258
|
+
}
|
|
259
|
+
;
|
|
260
|
+
mediaEntry.examples[key] =
|
|
261
|
+
wrapAsExampleObject(exampleValue);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
delete responseObj.examples;
|
|
291
265
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
266
|
+
// Transform headers if present
|
|
267
|
+
if (responseObj.headers && typeof responseObj.headers === 'object') {
|
|
268
|
+
responseObj.headers = Object.entries(responseObj.headers).reduce((acc, [headerName, header]) => {
|
|
269
|
+
if (header && typeof header === 'object') {
|
|
270
|
+
return {
|
|
271
|
+
[headerName]: transformResponseHeader(header),
|
|
272
|
+
...acc,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
return acc;
|
|
276
|
+
}, {});
|
|
297
277
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
278
|
+
migratedResponses[name] = responseObj;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
if (Object.keys(migratedResponses).length > 0) {
|
|
283
|
+
;
|
|
284
|
+
document.components.responses = migratedResponses;
|
|
285
|
+
}
|
|
286
|
+
delete document.responses;
|
|
287
|
+
}
|
|
288
|
+
// Paths
|
|
289
|
+
if (typeof document.paths === 'object') {
|
|
290
|
+
for (const path in document.paths) {
|
|
291
|
+
if (Object.hasOwn(document.paths, path)) {
|
|
292
|
+
const pathItem = document.paths && typeof document.paths === 'object' && path in document.paths
|
|
293
|
+
? document.paths[path]
|
|
294
|
+
: undefined;
|
|
295
|
+
if (!pathItem || typeof pathItem !== 'object') {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
let requestBodyObject;
|
|
299
|
+
for (const methodOrParameters in pathItem) {
|
|
300
|
+
if (methodOrParameters === 'parameters' && Object.hasOwn(pathItem, methodOrParameters)) {
|
|
301
|
+
const pathItemParameters = migrateParameters(pathItem.parameters, document.consumes ?? [DEFAULT_MEDIA_TYPE]);
|
|
302
|
+
pathItem.parameters = pathItemParameters.parameters;
|
|
303
|
+
requestBodyObject = pathItemParameters.requestBody;
|
|
304
|
+
}
|
|
305
|
+
else if (Object.hasOwn(pathItem, methodOrParameters)) {
|
|
306
|
+
const operationItem = pathItem[methodOrParameters];
|
|
307
|
+
if (requestBodyObject) {
|
|
308
|
+
operationItem.requestBody = requestBodyObject;
|
|
309
|
+
}
|
|
310
|
+
if (operationItem.parameters) {
|
|
311
|
+
const migrationResult = migrateParameters(operationItem.parameters, operationItem.consumes ?? document.consumes ?? [DEFAULT_MEDIA_TYPE]);
|
|
312
|
+
operationItem.parameters = migrationResult.parameters;
|
|
313
|
+
if (migrationResult.requestBody) {
|
|
314
|
+
operationItem.requestBody = migrationResult.requestBody;
|
|
315
|
+
}
|
|
304
316
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
if (
|
|
308
|
-
|
|
317
|
+
delete operationItem.consumes;
|
|
318
|
+
// Responses
|
|
319
|
+
if (operationItem.responses) {
|
|
320
|
+
for (const response in operationItem.responses) {
|
|
321
|
+
if (Object.hasOwn(operationItem.responses, response)) {
|
|
322
|
+
const responseItem = operationItem.responses[response];
|
|
323
|
+
if (responseItem.headers && typeof responseItem.headers === 'object') {
|
|
324
|
+
responseItem.headers = Object.entries(responseItem.headers).reduce((acc, [name, header]) => {
|
|
325
|
+
if (header && typeof header === 'object') {
|
|
326
|
+
return {
|
|
327
|
+
[name]: transformResponseHeader(header),
|
|
328
|
+
...acc,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
return acc;
|
|
332
|
+
}, {});
|
|
333
|
+
}
|
|
334
|
+
if (responseItem.schema) {
|
|
335
|
+
const produces = document.produces ?? operationItem.produces ?? [DEFAULT_MEDIA_TYPE];
|
|
336
|
+
if (typeof responseItem.content !== 'object') {
|
|
337
|
+
responseItem.content = {};
|
|
338
|
+
}
|
|
339
|
+
for (const type of produces) {
|
|
340
|
+
responseItem.content[type] = {
|
|
341
|
+
schema: responseItem.schema,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
delete responseItem.schema;
|
|
345
|
+
}
|
|
346
|
+
// Transform response examples from Swagger 2.0 to OpenAPI 3.0 format
|
|
347
|
+
// In Swagger 2.0, examples are at response level: examples: { DEFAULT_MEDIA_TYPE: {...} } or named: { 'Example': {...} }
|
|
348
|
+
// In OpenAPI 3.0, examples move inside content: content: { DEFAULT_MEDIA_TYPE: { example: {...} } } or examples: { Example: { value: {...} } }
|
|
349
|
+
if (responseItem.examples && typeof responseItem.examples === 'object') {
|
|
350
|
+
if (typeof responseItem.content !== 'object') {
|
|
351
|
+
responseItem.content = {};
|
|
352
|
+
}
|
|
353
|
+
const produces = document.produces ?? operationItem.produces ?? [DEFAULT_MEDIA_TYPE];
|
|
354
|
+
const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
|
|
355
|
+
for (const [key, exampleValue] of Object.entries(responseItem.examples)) {
|
|
356
|
+
// Media-type keys (e.g. application/json) go to content[key]; named example keys go to content[defaultMediaType].examples[key].
|
|
357
|
+
if (isMediaTypeKey(key)) {
|
|
358
|
+
if (typeof responseItem.content[key] !== 'object') {
|
|
359
|
+
responseItem.content[key] = {};
|
|
360
|
+
}
|
|
361
|
+
responseItem.content[key].example = exampleValue;
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
if (typeof responseItem.content[defaultMediaType] !== 'object') {
|
|
365
|
+
responseItem.content[defaultMediaType] = {};
|
|
366
|
+
}
|
|
367
|
+
const mediaEntry = responseItem.content[defaultMediaType];
|
|
368
|
+
if (typeof mediaEntry.examples !== 'object') {
|
|
369
|
+
mediaEntry.examples = {};
|
|
370
|
+
}
|
|
371
|
+
mediaEntry.examples[key] = wrapAsExampleObject(exampleValue);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
delete responseItem.examples;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
309
378
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
379
|
+
delete operationItem.produces;
|
|
380
|
+
// Delete empty parameters
|
|
381
|
+
if (operationItem.parameters?.length === 0) {
|
|
382
|
+
delete operationItem.parameters;
|
|
313
383
|
}
|
|
314
|
-
mediaEntry.examples[key] = wrapAsExampleObject(exampleValue);
|
|
315
|
-
}
|
|
316
384
|
}
|
|
317
|
-
delete responseItem.examples;
|
|
318
|
-
}
|
|
319
385
|
}
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
delete operationItem.produces;
|
|
323
|
-
if (operationItem.parameters?.length === 0) {
|
|
324
|
-
delete operationItem.parameters;
|
|
325
386
|
}
|
|
326
|
-
}
|
|
327
387
|
}
|
|
328
|
-
}
|
|
329
388
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
389
|
+
// Upgrade securityDefinitions
|
|
390
|
+
if (document.securityDefinitions) {
|
|
391
|
+
if (typeof document.components !== 'object' || document.components === null) {
|
|
392
|
+
document.components = {};
|
|
393
|
+
}
|
|
394
|
+
if (document.components && typeof document.components === 'object') {
|
|
395
|
+
Object.assign(document.components, { securitySchemes: {} });
|
|
396
|
+
}
|
|
397
|
+
for (const [key, securityScheme] of Object.entries(document.securityDefinitions)) {
|
|
398
|
+
if (typeof securityScheme === 'object') {
|
|
399
|
+
if ('type' in securityScheme && securityScheme.type === 'oauth2') {
|
|
400
|
+
const { flow, authorizationUrl, tokenUrl, scopes } = securityScheme;
|
|
401
|
+
// Convert flow values to OpenAPI 3.1.0 format
|
|
402
|
+
if (document.components &&
|
|
403
|
+
typeof document.components === 'object' &&
|
|
404
|
+
'securitySchemes' in document.components &&
|
|
405
|
+
document.components.securitySchemes) {
|
|
406
|
+
Object.assign(document.components.securitySchemes, {
|
|
407
|
+
[key]: {
|
|
408
|
+
type: 'oauth2',
|
|
409
|
+
flows: {
|
|
410
|
+
[upgradeFlow(flow || 'implicit')]: Object.assign({}, authorizationUrl && { authorizationUrl }, tokenUrl && { tokenUrl }, scopes && { scopes }),
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
});
|
|
414
|
+
}
|
|
353
415
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
416
|
+
else if ('type' in securityScheme && securityScheme.type === 'basic') {
|
|
417
|
+
if (document.components &&
|
|
418
|
+
typeof document.components === 'object' &&
|
|
419
|
+
'securitySchemes' in document.components &&
|
|
420
|
+
document.components.securitySchemes) {
|
|
421
|
+
Object.assign(document.components.securitySchemes, {
|
|
422
|
+
[key]: {
|
|
423
|
+
type: 'http',
|
|
424
|
+
scheme: 'basic',
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
if (document.components &&
|
|
431
|
+
typeof document.components === 'object' &&
|
|
432
|
+
'securitySchemes' in document.components &&
|
|
433
|
+
document.components.securitySchemes) {
|
|
434
|
+
Object.assign(document.components.securitySchemes, {
|
|
435
|
+
[key]: securityScheme,
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
372
440
|
}
|
|
373
|
-
|
|
441
|
+
delete document.securityDefinitions;
|
|
374
442
|
}
|
|
375
|
-
delete document.
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
delete document.produces;
|
|
379
|
-
return document;
|
|
443
|
+
delete document.consumes;
|
|
444
|
+
delete document.produces;
|
|
445
|
+
return document;
|
|
380
446
|
}
|
|
381
447
|
function transformItemsObject(obj) {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
448
|
+
const schemaProperties = [
|
|
449
|
+
'type',
|
|
450
|
+
'format',
|
|
451
|
+
'default',
|
|
452
|
+
'items',
|
|
453
|
+
'maximum',
|
|
454
|
+
'exclusiveMaximum',
|
|
455
|
+
'minimum',
|
|
456
|
+
'exclusiveMinimum',
|
|
457
|
+
'maxLength',
|
|
458
|
+
'minLength',
|
|
459
|
+
'pattern',
|
|
460
|
+
'maxItems',
|
|
461
|
+
'minItems',
|
|
462
|
+
'uniqueItems',
|
|
463
|
+
'enum',
|
|
464
|
+
'multipleOf',
|
|
465
|
+
];
|
|
466
|
+
return schemaProperties.reduce((acc, property) => {
|
|
467
|
+
if (Object.hasOwn(obj, property)) {
|
|
468
|
+
acc[property] = obj[property];
|
|
469
|
+
delete obj[property];
|
|
470
|
+
}
|
|
471
|
+
return acc;
|
|
472
|
+
}, {});
|
|
407
473
|
}
|
|
408
474
|
function getParameterLocation(location) {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
475
|
+
if (location === 'formData') {
|
|
476
|
+
throw new Error('Encountered a formData parameter which should have been filtered out by the caller');
|
|
477
|
+
}
|
|
478
|
+
if (location === 'body') {
|
|
479
|
+
throw new Error('Encountered a body parameter which should have been filtered out by the caller');
|
|
480
|
+
}
|
|
481
|
+
return location;
|
|
416
482
|
}
|
|
417
483
|
function transformParameterObject(parameter) {
|
|
418
|
-
|
|
484
|
+
if (Object.hasOwn(parameter, '$ref') && typeof parameter.$ref === 'string') {
|
|
485
|
+
return {
|
|
486
|
+
$ref: parameter.$ref,
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
// it is important to call getParameterSerializationStyle first because transformItemsObject modifies properties on which getParameterSerializationStyle rely on
|
|
490
|
+
const serializationStyle = getParameterSerializationStyle(parameter);
|
|
491
|
+
const schema = transformItemsObject(parameter);
|
|
492
|
+
const { xExample, xExamples } = extractXExampleExtensions(parameter);
|
|
493
|
+
// Input:
|
|
494
|
+
// x-example:
|
|
495
|
+
// application/json:
|
|
496
|
+
// message: OK
|
|
497
|
+
// type: success
|
|
498
|
+
// text/plain: 'OK'
|
|
499
|
+
// Output:
|
|
500
|
+
// examples:
|
|
501
|
+
// application/json:
|
|
502
|
+
// value:
|
|
503
|
+
// message: OK
|
|
504
|
+
// type: success
|
|
505
|
+
// text/plain:
|
|
506
|
+
// value: 'OK'
|
|
507
|
+
// We need to transform the x-example to an examples object and add "value" to the structure
|
|
508
|
+
if (isNonEmptyObject(xExample)) {
|
|
509
|
+
parameter.examples = transformXExampleToExamples(xExample);
|
|
510
|
+
}
|
|
511
|
+
else if (isNonEmptyObject(xExamples)) {
|
|
512
|
+
parameter.examples = Object.entries(xExamples).reduce((acc, [key, exampleValue]) => {
|
|
513
|
+
acc[key] = wrapAsExampleObject(exampleValue);
|
|
514
|
+
return acc;
|
|
515
|
+
}, {});
|
|
516
|
+
}
|
|
517
|
+
delete parameter.collectionFormat;
|
|
518
|
+
delete parameter.default;
|
|
519
|
+
if (!parameter.in) {
|
|
520
|
+
throw new Error('Parameter object must have an "in" property');
|
|
521
|
+
}
|
|
419
522
|
return {
|
|
420
|
-
|
|
523
|
+
schema,
|
|
524
|
+
...serializationStyle,
|
|
525
|
+
...parameter,
|
|
526
|
+
in: getParameterLocation(parameter.in),
|
|
421
527
|
};
|
|
422
|
-
}
|
|
423
|
-
const serializationStyle = getParameterSerializationStyle(parameter);
|
|
424
|
-
const schema = transformItemsObject(parameter);
|
|
425
|
-
const { xExample, xExamples } = extractXExampleExtensions(parameter);
|
|
426
|
-
if (isNonEmptyObject(xExample)) {
|
|
427
|
-
parameter.examples = transformXExampleToExamples(xExample);
|
|
428
|
-
} else if (isNonEmptyObject(xExamples)) {
|
|
429
|
-
parameter.examples = Object.entries(xExamples).reduce(
|
|
430
|
-
(acc, [key, exampleValue]) => {
|
|
431
|
-
acc[key] = wrapAsExampleObject(exampleValue);
|
|
432
|
-
return acc;
|
|
433
|
-
},
|
|
434
|
-
{}
|
|
435
|
-
);
|
|
436
|
-
}
|
|
437
|
-
delete parameter.collectionFormat;
|
|
438
|
-
delete parameter.default;
|
|
439
|
-
if (!parameter.in) {
|
|
440
|
-
throw new Error('Parameter object must have an "in" property');
|
|
441
|
-
}
|
|
442
|
-
return {
|
|
443
|
-
schema,
|
|
444
|
-
...serializationStyle,
|
|
445
|
-
...parameter,
|
|
446
|
-
in: getParameterLocation(parameter.in)
|
|
447
|
-
};
|
|
448
528
|
}
|
|
529
|
+
/**
|
|
530
|
+
* Transform OpenAPI 2.0 response header to OpenAPI 3.0 format.
|
|
531
|
+
* Response headers do not have "in", "name", "style", or "explode" properties.
|
|
532
|
+
*/
|
|
449
533
|
function transformResponseHeader(header) {
|
|
450
|
-
|
|
534
|
+
if (Object.hasOwn(header, '$ref') && typeof header.$ref === 'string') {
|
|
535
|
+
return {
|
|
536
|
+
$ref: header.$ref,
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
const schema = transformItemsObject(header);
|
|
451
540
|
return {
|
|
452
|
-
|
|
541
|
+
...header,
|
|
542
|
+
schema,
|
|
453
543
|
};
|
|
454
|
-
}
|
|
455
|
-
const schema = transformItemsObject(header);
|
|
456
|
-
return {
|
|
457
|
-
...header,
|
|
458
|
-
schema
|
|
459
|
-
};
|
|
460
544
|
}
|
|
461
545
|
const querySerialization = {
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
546
|
+
ssv: {
|
|
547
|
+
style: 'spaceDelimited',
|
|
548
|
+
explode: false,
|
|
549
|
+
},
|
|
550
|
+
pipes: {
|
|
551
|
+
style: 'pipeDelimited',
|
|
552
|
+
explode: false,
|
|
553
|
+
},
|
|
554
|
+
multi: {
|
|
555
|
+
style: 'form',
|
|
556
|
+
explode: true,
|
|
557
|
+
},
|
|
558
|
+
csv: {
|
|
559
|
+
style: 'form',
|
|
560
|
+
explode: false,
|
|
561
|
+
},
|
|
562
|
+
tsv: {},
|
|
479
563
|
};
|
|
480
564
|
const pathAndHeaderSerialization = {
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
565
|
+
ssv: {},
|
|
566
|
+
pipes: {},
|
|
567
|
+
multi: {},
|
|
568
|
+
csv: {
|
|
569
|
+
style: 'simple',
|
|
570
|
+
explode: false,
|
|
571
|
+
},
|
|
572
|
+
tsv: {},
|
|
489
573
|
};
|
|
490
574
|
const serializationStyles = {
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
575
|
+
header: pathAndHeaderSerialization,
|
|
576
|
+
query: querySerialization,
|
|
577
|
+
path: pathAndHeaderSerialization,
|
|
494
578
|
};
|
|
495
579
|
function getParameterSerializationStyle(parameter) {
|
|
496
|
-
|
|
580
|
+
if (parameter.type !== 'array' ||
|
|
581
|
+
!(parameter.in === 'query' || parameter.in === 'path' || parameter.in === 'header')) {
|
|
582
|
+
return {};
|
|
583
|
+
}
|
|
584
|
+
const collectionFormat = parameter.collectionFormat ?? 'csv';
|
|
585
|
+
if (parameter.in in serializationStyles &&
|
|
586
|
+
collectionFormat in serializationStyles[parameter.in]) {
|
|
587
|
+
return serializationStyles[parameter.in][collectionFormat];
|
|
588
|
+
}
|
|
497
589
|
return {};
|
|
498
|
-
}
|
|
499
|
-
const collectionFormat = parameter.collectionFormat ?? "csv";
|
|
500
|
-
if (parameter.in in serializationStyles && collectionFormat in serializationStyles[parameter.in]) {
|
|
501
|
-
return serializationStyles[parameter.in][collectionFormat];
|
|
502
|
-
}
|
|
503
|
-
return {};
|
|
504
590
|
}
|
|
505
591
|
function migrateBodyParameter(bodyParameter, consumes) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
592
|
+
// Extract x-example and x-examples before deleting other properties
|
|
593
|
+
// @see https://redocly.com/docs-legacy/api-reference-docs/specification-extensions/x-examples
|
|
594
|
+
const { xExample, xExamples } = extractXExampleExtensions(bodyParameter);
|
|
595
|
+
delete bodyParameter.name;
|
|
596
|
+
delete bodyParameter.in;
|
|
597
|
+
const { schema, ...requestBody } = bodyParameter;
|
|
598
|
+
const requestBodyObject = {
|
|
599
|
+
content: {},
|
|
600
|
+
...requestBody,
|
|
601
|
+
};
|
|
602
|
+
if (requestBodyObject.content) {
|
|
603
|
+
for (const type of consumes) {
|
|
604
|
+
requestBodyObject.content[type] = {
|
|
605
|
+
schema: schema,
|
|
606
|
+
};
|
|
607
|
+
// Handle x-example (singular) - Redocly extension for Swagger 2.0
|
|
608
|
+
// Transforms to OpenAPI 3.x `example` field
|
|
609
|
+
if (isNonEmptyObject(xExample) && type in xExample) {
|
|
610
|
+
requestBodyObject.content[type].example = xExample[type];
|
|
611
|
+
}
|
|
612
|
+
// Handle x-examples (plural) - Redocly extension for Swagger 2.0
|
|
613
|
+
// Transforms to OpenAPI 3.x `examples` field (named examples with summary/value)
|
|
614
|
+
if (isNonEmptyObject(xExamples) && type in xExamples) {
|
|
615
|
+
const examples = xExamples[type];
|
|
616
|
+
// Check if examples is already in proper OpenAPI 3.x format (object with entries that are valid ExampleObjects)
|
|
617
|
+
const isExamplesCollection = isNonEmptyObject(examples) && Object.values(examples).every((example) => isExampleObject(example));
|
|
618
|
+
if (isExamplesCollection) {
|
|
619
|
+
requestBodyObject.content[type].examples = examples;
|
|
620
|
+
}
|
|
621
|
+
// Named examples without value wrappers - wrap each individually
|
|
622
|
+
else if (isNamedExamplesCollection(examples)) {
|
|
623
|
+
requestBodyObject.content[type].examples = Object.entries(examples).reduce((acc, [key, exampleValue]) => {
|
|
624
|
+
acc[key] = wrapAsExampleObject(exampleValue);
|
|
625
|
+
return acc;
|
|
626
|
+
}, {});
|
|
627
|
+
}
|
|
628
|
+
// Single example value - wrap as default
|
|
629
|
+
else {
|
|
630
|
+
requestBodyObject.content[type].examples = {
|
|
631
|
+
default: wrapAsExampleObject(examples),
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
// Fallback: x-examples keyed by example name instead of media type
|
|
636
|
+
// e.g. x-examples: { Request: { email: "test@example.com" } }
|
|
637
|
+
else if (isNonEmptyObject(xExamples) && !Object.keys(xExamples).some(isMediaTypeKey)) {
|
|
638
|
+
if (isNamedExamplesCollection(xExamples)) {
|
|
639
|
+
requestBodyObject.content[type].examples = Object.entries(xExamples).reduce((acc, [key, exampleValue]) => {
|
|
640
|
+
acc[key] = wrapAsExampleObject(exampleValue);
|
|
641
|
+
return acc;
|
|
642
|
+
}, {});
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
requestBodyObject.content[type].examples = {
|
|
646
|
+
default: wrapAsExampleObject(xExamples),
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
}
|
|
553
650
|
}
|
|
554
|
-
}
|
|
555
651
|
}
|
|
556
|
-
|
|
557
|
-
return requestBodyObject;
|
|
652
|
+
return requestBodyObject;
|
|
558
653
|
}
|
|
559
|
-
function migrateFormDataParameter(parameters, consumes = [
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
(type) => type ===
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
// Initialize required array
|
|
575
|
-
}
|
|
576
|
-
};
|
|
577
|
-
const formContent = requestBodyObject.content?.[contentType];
|
|
578
|
-
if (formContent?.schema && typeof formContent.schema === "object" && "properties" in formContent.schema) {
|
|
579
|
-
for (const param of parameters) {
|
|
580
|
-
if (param.name && formContent.schema.properties) {
|
|
581
|
-
formContent.schema.properties[param.name] = {
|
|
582
|
-
type: param.type,
|
|
583
|
-
description: param.description,
|
|
584
|
-
...param.format ? { format: param.format } : {}
|
|
654
|
+
function migrateFormDataParameter(parameters, consumes = ['multipart/form-data']) {
|
|
655
|
+
const requestBodyObject = {
|
|
656
|
+
content: {},
|
|
657
|
+
};
|
|
658
|
+
// Ensure consumes contains a form data content type
|
|
659
|
+
const filtered = consumes.filter((type) => type === 'multipart/form-data' || type === 'application/x-www-form-urlencoded');
|
|
660
|
+
const contentTypes = filtered.length > 0 ? filtered : ['multipart/form-data'];
|
|
661
|
+
if (requestBodyObject.content) {
|
|
662
|
+
for (const contentType of contentTypes) {
|
|
663
|
+
requestBodyObject.content[contentType] = {
|
|
664
|
+
schema: {
|
|
665
|
+
type: 'object',
|
|
666
|
+
properties: {},
|
|
667
|
+
required: [], // Initialize required array
|
|
668
|
+
},
|
|
585
669
|
};
|
|
586
|
-
|
|
587
|
-
|
|
670
|
+
const formContent = requestBodyObject.content?.[contentType];
|
|
671
|
+
if (formContent?.schema && typeof formContent.schema === 'object' && 'properties' in formContent.schema) {
|
|
672
|
+
for (const param of parameters) {
|
|
673
|
+
if (param.name && formContent.schema.properties) {
|
|
674
|
+
formContent.schema.properties[param.name] = {
|
|
675
|
+
type: param.type,
|
|
676
|
+
description: param.description,
|
|
677
|
+
...(param.format ? { format: param.format } : {}),
|
|
678
|
+
};
|
|
679
|
+
// Add to required array if param is required
|
|
680
|
+
if (param.required && Array.isArray(formContent.schema.required)) {
|
|
681
|
+
formContent.schema.required.push(param.name);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
588
685
|
}
|
|
589
|
-
}
|
|
590
686
|
}
|
|
591
|
-
}
|
|
592
687
|
}
|
|
593
|
-
|
|
594
|
-
return requestBodyObject;
|
|
688
|
+
return requestBodyObject;
|
|
595
689
|
}
|
|
596
690
|
function migrateParameters(parameters, consumes) {
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
const formDataParameters = parameters.filter((parameter) => parameter.in === "formData");
|
|
607
|
-
if (formDataParameters.length > 0) {
|
|
608
|
-
const requestBodyObject = migrateFormDataParameter(formDataParameters, consumes);
|
|
609
|
-
if (typeof result.requestBody !== "object") {
|
|
610
|
-
result.requestBody = requestBodyObject;
|
|
611
|
-
} else {
|
|
612
|
-
result.requestBody = {
|
|
613
|
-
...result.requestBody,
|
|
614
|
-
content: {
|
|
615
|
-
...result.requestBody.content,
|
|
616
|
-
...requestBodyObject.content
|
|
617
|
-
}
|
|
618
|
-
};
|
|
691
|
+
const result = {
|
|
692
|
+
parameters: parameters
|
|
693
|
+
.filter((parameter) => !(parameter.in === 'body' || parameter.in === 'formData'))
|
|
694
|
+
.map((parameter) => transformParameterObject(parameter)),
|
|
695
|
+
};
|
|
696
|
+
const bodyParameter = structuredClone(parameters.find((parameter) => parameter.in === 'body') ?? {});
|
|
697
|
+
if (bodyParameter && Object.keys(bodyParameter).length) {
|
|
698
|
+
result.requestBody = migrateBodyParameter(bodyParameter, consumes);
|
|
619
699
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
700
|
+
const formDataParameters = parameters.filter((parameter) => parameter.in === 'formData');
|
|
701
|
+
if (formDataParameters.length > 0) {
|
|
702
|
+
const requestBodyObject = migrateFormDataParameter(formDataParameters, consumes);
|
|
703
|
+
if (typeof result.requestBody !== 'object') {
|
|
704
|
+
result.requestBody = requestBodyObject;
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
result.requestBody = {
|
|
708
|
+
...result.requestBody,
|
|
709
|
+
content: {
|
|
710
|
+
...result.requestBody.content,
|
|
711
|
+
...requestBodyObject.content,
|
|
712
|
+
},
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
if (typeof result.requestBody !== 'object') {
|
|
716
|
+
result.requestBody = {
|
|
717
|
+
content: {},
|
|
718
|
+
};
|
|
719
|
+
}
|
|
624
720
|
}
|
|
625
|
-
|
|
626
|
-
return result;
|
|
721
|
+
return result;
|
|
627
722
|
}
|
|
628
|
-
export {
|
|
629
|
-
upgradeFromTwoToThree
|
|
630
|
-
};
|
|
631
|
-
//# sourceMappingURL=upgrade-from-two-to-three.js.map
|