@scalar/openapi-upgrader 0.2.0 → 0.2.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.
@@ -1,631 +1,717 @@
1
- import { traverse } from "../helpers/traverse.js";
2
- const DEFAULT_MEDIA_TYPE = "application/json";
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
- const xExample = obj["x-example"];
5
- const xExamples = obj["x-examples"];
6
- delete obj["x-example"];
7
- delete obj["x-examples"];
8
- return { xExample, xExamples };
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
- return typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value).length > 0;
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
- return isNonEmptyObject(value) && Object.values(value).every((v) => typeof v === "object" && v !== null && !Array.isArray(v));
22
+ return (isNonEmptyObject(value) &&
23
+ Object.values(value).every((v) => typeof v === 'object' && v !== null && !Array.isArray(v)));
15
24
  }
16
- const EXAMPLE_OBJECT_PROPERTIES = /* @__PURE__ */ new Set(["summary", "description", "value", "externalValue"]);
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
- if (typeof value !== "object" || value === null) {
19
- return false;
20
- }
21
- const obj = value;
22
- const hasValueOrExternalValue = "value" in obj || "externalValue" in obj;
23
- const onlyHasAllowedProperties = Object.keys(obj).every((key) => EXAMPLE_OBJECT_PROPERTIES.has(key));
24
- return hasValueOrExternalValue && onlyHasAllowedProperties;
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
- if (isExampleObject(value)) {
28
- return value;
29
- }
30
- return { value };
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
- return MEDIA_TYPE_KEY_PATTERN.test(key);
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
- return Object.entries(xExample).reduce(
38
- (acc, [key, value]) => {
39
- acc[key] = { value };
40
- return acc;
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
- switch (flow) {
47
- case "application":
48
- return "clientCredentials";
49
- case "accessCode":
50
- return "authorizationCode";
51
- case "implicit":
52
- return "implicit";
53
- case "password":
54
- return "password";
55
- default:
56
- return flow;
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
- function upgradeFromTwoToThree(originalSpecification) {
60
- let document = originalSpecification;
61
- if (document !== null && typeof document === "object" && typeof document.swagger === "string" && document.swagger?.startsWith("2.0")) {
62
- document.openapi = "3.0.4";
63
- delete document.swagger;
64
- } else {
65
- return document;
66
- }
67
- if (document.host) {
68
- const schemes = Array.isArray(document.schemes) && document.schemes?.length ? document.schemes : ["http"];
69
- document.servers = schemes.map((scheme) => ({
70
- url: `${scheme}://${document.host}${document.basePath ?? ""}`
71
- }));
72
- delete document.basePath;
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
- return schema;
97
- });
98
- if (Object.hasOwn(document, "parameters")) {
99
- document = traverse(document, (schema) => {
100
- if (typeof schema.$ref === "string" && schema.$ref.startsWith("#/parameters/")) {
101
- const schemaName = schema.$ref.split("/")[2];
102
- if (!schemaName) {
103
- return schema;
104
- }
105
- const param = document.parameters && typeof document.parameters === "object" && schemaName in document.parameters ? document.parameters[schemaName] : void 0;
106
- if (param && typeof param === "object" && "in" in param && (param.in === "body" || param.in === "formData")) {
107
- schema.$ref = schema.$ref.replace(/^#\/parameters\//, "#/components/requestBodies/");
108
- } else {
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 (Object.keys(params).length > 0) {
142
- ;
143
- document.components.parameters = params;
114
+ else if (document.basePath) {
115
+ document.servers = [{ url: document.basePath }];
116
+ delete document.basePath;
144
117
  }
145
- if (Object.keys(bodyParams).length > 0) {
146
- ;
147
- document.components.requestBodies = bodyParams;
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
- delete document.parameters;
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
- if (typeof schema.$ref === "string" && schema.$ref.startsWith("#/responses/")) {
154
- schema.$ref = schema.$ref.replace(/^#\/responses\//, "#/components/responses/");
155
- }
156
- return schema;
135
+ if (schema.type === 'file') {
136
+ schema.type = 'string';
137
+ schema.format = 'binary';
138
+ }
139
+ return schema;
157
140
  });
158
- document.components ??= {};
159
- const migratedResponses = {};
160
- const responses = document.responses;
161
- for (const [name, response] of Object.entries(responses)) {
162
- if (response && typeof response === "object") {
163
- if ("$ref" in response) {
164
- migratedResponses[name] = response;
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
- responseObj.content[key].example = exampleValue;
193
- } else {
194
- if (typeof responseObj.content[defaultMediaType] !== "object") {
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
- const mediaEntry = responseObj.content[defaultMediaType];
199
- if (typeof mediaEntry.examples !== "object") {
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
- delete responseObj.examples;
207
- }
208
- if (responseObj.headers && typeof responseObj.headers === "object") {
209
- responseObj.headers = Object.entries(responseObj.headers).reduce(
210
- (acc, [headerName, header]) => {
211
- if (header && typeof header === "object") {
212
- return {
213
- [headerName]: transformResponseHeader(header),
214
- ...acc
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
- return acc;
218
- },
219
- {}
220
- );
221
- }
222
- migratedResponses[name] = responseObj;
186
+ }
223
187
  }
224
- }
225
- }
226
- if (Object.keys(migratedResponses).length > 0) {
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
- let requestBodyObject;
240
- for (const methodOrParameters in pathItem) {
241
- if (methodOrParameters === "parameters" && Object.hasOwn(pathItem, methodOrParameters)) {
242
- const pathItemParameters = migrateParameters(
243
- pathItem.parameters,
244
- document.consumes ?? [DEFAULT_MEDIA_TYPE]
245
- );
246
- pathItem.parameters = pathItemParameters.parameters;
247
- requestBodyObject = pathItemParameters.requestBody;
248
- } else if (Object.hasOwn(pathItem, methodOrParameters)) {
249
- const operationItem = pathItem[methodOrParameters];
250
- if (requestBodyObject) {
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
- delete operationItem.consumes;
264
- if (operationItem.responses) {
265
- for (const response in operationItem.responses) {
266
- if (Object.hasOwn(operationItem.responses, response)) {
267
- const responseItem = operationItem.responses[response];
268
- if (responseItem.headers && typeof responseItem.headers === "object") {
269
- responseItem.headers = Object.entries(responseItem.headers).reduce(
270
- (acc, [name, header]) => {
271
- if (header && typeof header === "object") {
272
- return {
273
- [name]: transformResponseHeader(header),
274
- ...acc
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
- return acc;
278
- },
279
- {}
280
- );
281
- }
282
- if (responseItem.schema) {
283
- const produces = document.produces ?? operationItem.produces ?? [DEFAULT_MEDIA_TYPE];
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;
232
+ }
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;
286
265
  }
287
- for (const type of produces) {
288
- responseItem.content[type] = {
289
- schema: responseItem.schema
290
- };
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
+ }, {});
291
277
  }
292
- delete responseItem.schema;
293
- }
294
- if (responseItem.examples && typeof responseItem.examples === "object") {
295
- if (typeof responseItem.content !== "object") {
296
- responseItem.content = {};
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;
297
304
  }
298
- const produces = document.produces ?? operationItem.produces ?? [DEFAULT_MEDIA_TYPE];
299
- const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
300
- for (const [key, exampleValue] of Object.entries(responseItem.examples)) {
301
- if (isMediaTypeKey(key)) {
302
- if (typeof responseItem.content[key] !== "object") {
303
- responseItem.content[key] = {};
305
+ else if (Object.hasOwn(pathItem, methodOrParameters)) {
306
+ const operationItem = pathItem[methodOrParameters];
307
+ if (requestBodyObject) {
308
+ operationItem.requestBody = requestBodyObject;
304
309
  }
305
- responseItem.content[key].example = exampleValue;
306
- } else {
307
- if (typeof responseItem.content[defaultMediaType] !== "object") {
308
- responseItem.content[defaultMediaType] = {};
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
+ }
309
316
  }
310
- const mediaEntry = responseItem.content[defaultMediaType];
311
- if (typeof mediaEntry.examples !== "object") {
312
- mediaEntry.examples = {};
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
+ }
378
+ }
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
- }
330
- }
331
- if (document.securityDefinitions) {
332
- if (typeof document.components !== "object" || document.components === null) {
333
- document.components = {};
334
388
  }
335
- if (document.components && typeof document.components === "object") {
336
- Object.assign(document.components, { securitySchemes: {} });
337
- }
338
- for (const [key, securityScheme] of Object.entries(document.securityDefinitions)) {
339
- if (typeof securityScheme === "object") {
340
- if ("type" in securityScheme && securityScheme.type === "oauth2") {
341
- const { flow, authorizationUrl, tokenUrl, scopes } = securityScheme;
342
- if (document.components && typeof document.components === "object" && "securitySchemes" in document.components && document.components.securitySchemes) {
343
- Object.assign(document.components.securitySchemes, {
344
- [key]: {
345
- type: "oauth2",
346
- flows: {
347
- [upgradeFlow(flow || "implicit")]: Object.assign(
348
- {},
349
- authorizationUrl && { authorizationUrl },
350
- tokenUrl && { tokenUrl },
351
- scopes && { scopes }
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
+ }
415
+ }
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
+ }
353
428
  }
354
- }
355
- });
356
- }
357
- } else if ("type" in securityScheme && securityScheme.type === "basic") {
358
- if (document.components && typeof document.components === "object" && "securitySchemes" in document.components && document.components.securitySchemes) {
359
- Object.assign(document.components.securitySchemes, {
360
- [key]: {
361
- type: "http",
362
- scheme: "basic"
363
- }
364
- });
365
- }
366
- } else {
367
- if (document.components && typeof document.components === "object" && "securitySchemes" in document.components && document.components.securitySchemes) {
368
- Object.assign(document.components.securitySchemes, {
369
- [key]: securityScheme
370
- });
371
- }
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.securityDefinitions;
376
- }
377
- delete document.consumes;
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
- const schemaProperties = [
383
- "type",
384
- "format",
385
- "default",
386
- "items",
387
- "maximum",
388
- "exclusiveMaximum",
389
- "minimum",
390
- "exclusiveMinimum",
391
- "maxLength",
392
- "minLength",
393
- "pattern",
394
- "maxItems",
395
- "minItems",
396
- "uniqueItems",
397
- "enum",
398
- "multipleOf"
399
- ];
400
- return schemaProperties.reduce((acc, property) => {
401
- if (Object.hasOwn(obj, property)) {
402
- acc[property] = obj[property];
403
- delete obj[property];
404
- }
405
- return acc;
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
- if (location === "formData") {
410
- throw new Error("Encountered a formData parameter which should have been filtered out by the caller");
411
- }
412
- if (location === "body") {
413
- throw new Error("Encountered a body parameter which should have been filtered out by the caller");
414
- }
415
- return location;
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
- if (Object.hasOwn(parameter, "$ref") && typeof parameter.$ref === "string") {
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
- $ref: parameter.$ref
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
- if (Object.hasOwn(header, "$ref") && typeof header.$ref === "string") {
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
- $ref: header.$ref
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
- ssv: {
463
- style: "spaceDelimited",
464
- explode: false
465
- },
466
- pipes: {
467
- style: "pipeDelimited",
468
- explode: false
469
- },
470
- multi: {
471
- style: "form",
472
- explode: true
473
- },
474
- csv: {
475
- style: "form",
476
- explode: false
477
- },
478
- tsv: {}
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
- ssv: {},
482
- pipes: {},
483
- multi: {},
484
- csv: {
485
- style: "simple",
486
- explode: false
487
- },
488
- tsv: {}
565
+ ssv: {},
566
+ pipes: {},
567
+ multi: {},
568
+ csv: {
569
+ style: 'simple',
570
+ explode: false,
571
+ },
572
+ tsv: {},
489
573
  };
490
574
  const serializationStyles = {
491
- header: pathAndHeaderSerialization,
492
- query: querySerialization,
493
- path: pathAndHeaderSerialization
575
+ header: pathAndHeaderSerialization,
576
+ query: querySerialization,
577
+ path: pathAndHeaderSerialization,
494
578
  };
495
579
  function getParameterSerializationStyle(parameter) {
496
- if (parameter.type !== "array" || !(parameter.in === "query" || parameter.in === "path" || parameter.in === "header")) {
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
- const { xExample, xExamples } = extractXExampleExtensions(bodyParameter);
507
- delete bodyParameter.name;
508
- delete bodyParameter.in;
509
- const { schema, ...requestBody } = bodyParameter;
510
- const requestBodyObject = {
511
- content: {},
512
- ...requestBody
513
- };
514
- if (requestBodyObject.content) {
515
- for (const type of consumes) {
516
- requestBodyObject.content[type] = {
517
- schema
518
- };
519
- if (isNonEmptyObject(xExample) && type in xExample) {
520
- requestBodyObject.content[type].example = xExample[type];
521
- }
522
- if (isNonEmptyObject(xExamples) && type in xExamples) {
523
- const examples = xExamples[type];
524
- const isExamplesCollection = isNonEmptyObject(examples) && Object.values(examples).every((example) => isExampleObject(example));
525
- if (isExamplesCollection) {
526
- requestBodyObject.content[type].examples = examples;
527
- } else if (isNamedExamplesCollection(examples)) {
528
- requestBodyObject.content[type].examples = Object.entries(examples).reduce(
529
- (acc, [key, exampleValue]) => {
530
- acc[key] = wrapAsExampleObject(exampleValue);
531
- return acc;
532
- },
533
- {}
534
- );
535
- } else {
536
- requestBodyObject.content[type].examples = {
537
- default: wrapAsExampleObject(examples)
538
- };
539
- }
540
- } else if (isNonEmptyObject(xExamples) && !Object.keys(xExamples).some(isMediaTypeKey)) {
541
- if (isNamedExamplesCollection(xExamples)) {
542
- requestBodyObject.content[type].examples = Object.entries(xExamples).reduce(
543
- (acc, [key, exampleValue]) => {
544
- acc[key] = wrapAsExampleObject(exampleValue);
545
- return acc;
546
- },
547
- {}
548
- );
549
- } else {
550
- requestBodyObject.content[type].examples = {
551
- default: wrapAsExampleObject(xExamples)
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-examples (plural) first - Redocly extension for Swagger 2.0
608
+ // Transforms to OpenAPI 3.x `examples` field (named examples with summary/value).
609
+ // When both x-example and x-examples exist, we prefer x-examples so each media type
610
+ // has only one of `example` or `examples` (OpenAPI 3 does not use both on the same type).
611
+ if (isNonEmptyObject(xExamples) && type in xExamples) {
612
+ const examples = xExamples[type];
613
+ // Check if examples is already in proper OpenAPI 3.x format (object with entries that are valid ExampleObjects)
614
+ const isExamplesCollection = isNonEmptyObject(examples) && Object.values(examples).every((example) => isExampleObject(example));
615
+ if (isExamplesCollection) {
616
+ requestBodyObject.content[type].examples = examples;
617
+ }
618
+ // Named examples without value wrappers - wrap each individually
619
+ else if (isNamedExamplesCollection(examples)) {
620
+ requestBodyObject.content[type].examples = Object.entries(examples).reduce((acc, [key, exampleValue]) => {
621
+ acc[key] = wrapAsExampleObject(exampleValue);
622
+ return acc;
623
+ }, {});
624
+ }
625
+ // Single example value - wrap as default
626
+ else {
627
+ requestBodyObject.content[type].examples = {
628
+ default: wrapAsExampleObject(examples),
629
+ };
630
+ }
631
+ }
632
+ // Fallback: x-examples keyed by example name instead of media type
633
+ // e.g. x-examples: { Request: { email: "test@example.com" } }
634
+ else if (isNonEmptyObject(xExamples) && !Object.keys(xExamples).some(isMediaTypeKey)) {
635
+ requestBodyObject.content[type].examples = Object.entries(xExamples).reduce((acc, [key, exampleValue]) => {
636
+ acc[key] = wrapAsExampleObject(exampleValue);
637
+ return acc;
638
+ }, {});
639
+ }
640
+ // Handle x-example (singular) only when we did not set examples for this type
641
+ // (avoids having both example and examples on the same media type)
642
+ if (!requestBodyObject.content[type].examples && isNonEmptyObject(xExample) && type in xExample) {
643
+ requestBodyObject.content[type].example = xExample[type];
644
+ }
553
645
  }
554
- }
555
646
  }
556
- }
557
- return requestBodyObject;
647
+ return requestBodyObject;
558
648
  }
559
- function migrateFormDataParameter(parameters, consumes = ["multipart/form-data"]) {
560
- const requestBodyObject = {
561
- content: {}
562
- };
563
- const filtered = consumes.filter(
564
- (type) => type === "multipart/form-data" || type === "application/x-www-form-urlencoded"
565
- );
566
- const contentTypes = filtered.length > 0 ? filtered : ["multipart/form-data"];
567
- if (requestBodyObject.content) {
568
- for (const contentType of contentTypes) {
569
- requestBodyObject.content[contentType] = {
570
- schema: {
571
- type: "object",
572
- properties: {},
573
- required: []
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 } : {}
649
+ function migrateFormDataParameter(parameters, consumes = ['multipart/form-data']) {
650
+ const requestBodyObject = {
651
+ content: {},
652
+ };
653
+ // Ensure consumes contains a form data content type
654
+ const filtered = consumes.filter((type) => type === 'multipart/form-data' || type === 'application/x-www-form-urlencoded');
655
+ const contentTypes = filtered.length > 0 ? filtered : ['multipart/form-data'];
656
+ if (requestBodyObject.content) {
657
+ for (const contentType of contentTypes) {
658
+ requestBodyObject.content[contentType] = {
659
+ schema: {
660
+ type: 'object',
661
+ properties: {},
662
+ required: [], // Initialize required array
663
+ },
585
664
  };
586
- if (param.required && Array.isArray(formContent.schema.required)) {
587
- formContent.schema.required.push(param.name);
665
+ const formContent = requestBodyObject.content?.[contentType];
666
+ if (formContent?.schema && typeof formContent.schema === 'object' && 'properties' in formContent.schema) {
667
+ for (const param of parameters) {
668
+ if (param.name && formContent.schema.properties) {
669
+ formContent.schema.properties[param.name] = {
670
+ type: param.type,
671
+ description: param.description,
672
+ ...(param.format ? { format: param.format } : {}),
673
+ };
674
+ // Add to required array if param is required
675
+ if (param.required && Array.isArray(formContent.schema.required)) {
676
+ formContent.schema.required.push(param.name);
677
+ }
678
+ }
679
+ }
588
680
  }
589
- }
590
681
  }
591
- }
592
682
  }
593
- }
594
- return requestBodyObject;
683
+ return requestBodyObject;
595
684
  }
596
685
  function migrateParameters(parameters, consumes) {
597
- const result = {
598
- parameters: parameters.filter((parameter) => !(parameter.in === "body" || parameter.in === "formData")).map((parameter) => transformParameterObject(parameter))
599
- };
600
- const bodyParameter = structuredClone(
601
- parameters.find((parameter) => parameter.in === "body") ?? {}
602
- );
603
- if (bodyParameter && Object.keys(bodyParameter).length) {
604
- result.requestBody = migrateBodyParameter(bodyParameter, consumes);
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
- };
686
+ const result = {
687
+ parameters: parameters
688
+ .filter((parameter) => !(parameter.in === 'body' || parameter.in === 'formData'))
689
+ .map((parameter) => transformParameterObject(parameter)),
690
+ };
691
+ const bodyParameter = structuredClone(parameters.find((parameter) => parameter.in === 'body') ?? {});
692
+ if (bodyParameter && Object.keys(bodyParameter).length) {
693
+ result.requestBody = migrateBodyParameter(bodyParameter, consumes);
619
694
  }
620
- if (typeof result.requestBody !== "object") {
621
- result.requestBody = {
622
- content: {}
623
- };
695
+ const formDataParameters = parameters.filter((parameter) => parameter.in === 'formData');
696
+ if (formDataParameters.length > 0) {
697
+ const requestBodyObject = migrateFormDataParameter(formDataParameters, consumes);
698
+ if (typeof result.requestBody !== 'object') {
699
+ result.requestBody = requestBodyObject;
700
+ }
701
+ else {
702
+ result.requestBody = {
703
+ ...result.requestBody,
704
+ content: {
705
+ ...result.requestBody.content,
706
+ ...requestBodyObject.content,
707
+ },
708
+ };
709
+ }
710
+ if (typeof result.requestBody !== 'object') {
711
+ result.requestBody = {
712
+ content: {},
713
+ };
714
+ }
624
715
  }
625
- }
626
- return result;
716
+ return result;
627
717
  }
628
- export {
629
- upgradeFromTwoToThree
630
- };
631
- //# sourceMappingURL=upgrade-from-two-to-three.js.map