@scalar/openapi-upgrader 0.4.11 → 0.5.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 +16 -0
- package/dist/2.0-to-3.0/upgrade-from-two-to-three.d.ts +22 -1
- package/dist/2.0-to-3.0/upgrade-from-two-to-three.d.ts.map +1 -1
- package/dist/2.0-to-3.0/upgrade-from-two-to-three.js +103 -149
- package/dist/3.0-to-3.1/upgrade-from-three-to-three-one.d.ts +10 -0
- package/dist/3.0-to-3.1/upgrade-from-three-to-three-one.d.ts.map +1 -1
- package/dist/3.0-to-3.1/upgrade-from-three-to-three-one.js +194 -50
- package/dist/helpers/is-inside-data-value.d.ts +4 -0
- package/dist/helpers/is-inside-data-value.d.ts.map +1 -1
- package/dist/helpers/is-inside-data-value.js +58 -7
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/upgrade-fragment.d.ts +44 -0
- package/dist/upgrade-fragment.d.ts.map +1 -0
- package/dist/upgrade-fragment.js +466 -0
- package/package.json +4 -4
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import { HTTP_METHODS } from '@scalar/helpers/http/http-methods';
|
|
2
|
+
import { isObject } from '@scalar/helpers/object/is-object';
|
|
3
|
+
import { migrateDiscriminatorMappings } from './2.0-to-3.0/migrate-discriminator-mappings.js';
|
|
4
|
+
import { migrateBodyParameter, migrateFormDataParameter, migrateSchema, parameterSchemaFields, transformParameterObject, transformResponseHeader, transformResponseObject, upgradeFromTwoToThree, } from './2.0-to-3.0/upgrade-from-two-to-three.js';
|
|
5
|
+
import { applyChangesToDocument, getNullableComposition } from './3.0-to-3.1/upgrade-from-three-to-three-one.js';
|
|
6
|
+
const schemaMaps = new Set(['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas']);
|
|
7
|
+
const schemaChildren = new Set([
|
|
8
|
+
'items',
|
|
9
|
+
'additionalProperties',
|
|
10
|
+
'unevaluatedProperties',
|
|
11
|
+
'unevaluatedItems',
|
|
12
|
+
'contains',
|
|
13
|
+
'contentSchema',
|
|
14
|
+
'propertyNames',
|
|
15
|
+
'not',
|
|
16
|
+
'if',
|
|
17
|
+
'then',
|
|
18
|
+
'else',
|
|
19
|
+
'allOf',
|
|
20
|
+
'anyOf',
|
|
21
|
+
'oneOf',
|
|
22
|
+
'prefixItems',
|
|
23
|
+
]);
|
|
24
|
+
/** Define authored keys without invoking inherited setters. */
|
|
25
|
+
const setOwn = (target, key, value) => {
|
|
26
|
+
Object.defineProperty(target, key, { value, enumerable: true, configurable: true, writable: true });
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Upgrade a complete document or a typed fragment without inventing a wrapper document.
|
|
30
|
+
* Returns a copy; literal examples and extension payloads remain data.
|
|
31
|
+
* Authored references retain source pointers: use locations to resolve them against converted content.
|
|
32
|
+
* A standalone schema resource with $schema retains its declared dialect.
|
|
33
|
+
*/
|
|
34
|
+
export const upgradeFragment = async (input, options) => {
|
|
35
|
+
const locations = [];
|
|
36
|
+
const contexts = [];
|
|
37
|
+
const v2 = options.from === '2.0';
|
|
38
|
+
const resolvedParameters = new Map();
|
|
39
|
+
const resolveParameter = (parameter) => {
|
|
40
|
+
if (!v2 || !isObject(parameter) || typeof parameter.$ref !== 'string' || !options.resolveSource) {
|
|
41
|
+
return Promise.resolve(parameter);
|
|
42
|
+
}
|
|
43
|
+
const cached = resolvedParameters.get(parameter.$ref);
|
|
44
|
+
if (cached) {
|
|
45
|
+
return cached;
|
|
46
|
+
}
|
|
47
|
+
const resolved = Promise.resolve()
|
|
48
|
+
.then(() => options.resolveSource?.(parameter.$ref))
|
|
49
|
+
.then((value) => value ?? parameter)
|
|
50
|
+
.catch(() => parameter);
|
|
51
|
+
resolvedParameters.set(parameter.$ref, resolved);
|
|
52
|
+
return resolved;
|
|
53
|
+
};
|
|
54
|
+
const recordedSources = new Set();
|
|
55
|
+
const remember = (source, output) => {
|
|
56
|
+
const key = JSON.stringify(source);
|
|
57
|
+
// Reused schemas can have several destinations. Keep the first, including unchanged ones.
|
|
58
|
+
if (recordedSources.has(key)) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
recordedSources.add(key);
|
|
62
|
+
if (source.length !== output.length || source.some((part, index) => part !== output[index])) {
|
|
63
|
+
locations.push({ source, output });
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const rememberFormFields = (parameter, source, output, mediaTypes) => {
|
|
67
|
+
for (const media of mediaTypes) {
|
|
68
|
+
const property = [...output, 'content', media, 'schema', 'properties', String(parameter.name)];
|
|
69
|
+
for (const key of parameterSchemaFields) {
|
|
70
|
+
if (Object.hasOwn(parameter, key)) {
|
|
71
|
+
remember([...source, key], [...property, key]);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const visit = async (value, { kind, source, output, consumes, produces, semanticPath, parameterSources, generated }) => {
|
|
77
|
+
if (!generated) {
|
|
78
|
+
remember(source, output);
|
|
79
|
+
}
|
|
80
|
+
if (!isObject(value)) {
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
// A declared JSON Schema dialect must not be interpreted as an OpenAPI 3.0 schema.
|
|
84
|
+
if (kind === 'schema' && options.kind === 'schema' && source.length === 0 && typeof value.$schema === 'string') {
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
let result = { ...value };
|
|
88
|
+
const child = (entry, childKind, from, to = from) => visit(entry, {
|
|
89
|
+
kind: childKind,
|
|
90
|
+
source: [...source, ...from],
|
|
91
|
+
output: [...output, ...to],
|
|
92
|
+
consumes,
|
|
93
|
+
produces,
|
|
94
|
+
semanticPath: [...semanticPath, ...to],
|
|
95
|
+
generated,
|
|
96
|
+
});
|
|
97
|
+
const map = async (entries, childKind, from, to = from) => {
|
|
98
|
+
const converted = {};
|
|
99
|
+
if (!isObject(entries)) {
|
|
100
|
+
return converted;
|
|
101
|
+
}
|
|
102
|
+
for (const [name, entry] of Object.entries(entries)) {
|
|
103
|
+
setOwn(converted, name, await child(entry, childKind, [...from, name], [...to, name]));
|
|
104
|
+
}
|
|
105
|
+
return converted;
|
|
106
|
+
};
|
|
107
|
+
if (kind === 'document') {
|
|
108
|
+
const sourceConsumes = Array.isArray(value.consumes) ? value.consumes : consumes;
|
|
109
|
+
const sourceProduces = Array.isArray(value.produces) ? value.produces : produces;
|
|
110
|
+
if (value.consumes || value.produces) {
|
|
111
|
+
contexts.push({ path: output, consumes: sourceConsumes, produces: sourceProduces });
|
|
112
|
+
}
|
|
113
|
+
// The existing document converter owns root-only metadata such as servers and security schemes.
|
|
114
|
+
const { paths, definitions, parameters, responses, components, ...metadata } = result;
|
|
115
|
+
result = v2 ? upgradeFromTwoToThree(metadata) : result;
|
|
116
|
+
result.openapi = '3.1.1';
|
|
117
|
+
if (v2 && isObject(definitions)) {
|
|
118
|
+
Object.values(definitions).forEach((schema) => migrateSchema(schema));
|
|
119
|
+
migrateDiscriminatorMappings(definitions);
|
|
120
|
+
}
|
|
121
|
+
const convertedComponents = isObject(components) ? { ...components } : {};
|
|
122
|
+
const sections = v2
|
|
123
|
+
? [
|
|
124
|
+
[definitions, 'schema', ['definitions'], ['components', 'schemas']],
|
|
125
|
+
[responses, 'response', ['responses'], ['components', 'responses']],
|
|
126
|
+
]
|
|
127
|
+
: [
|
|
128
|
+
[convertedComponents.schemas, 'schema', ['components', 'schemas'], ['components', 'schemas']],
|
|
129
|
+
[convertedComponents.parameters, 'parameter', ['components', 'parameters'], ['components', 'parameters']],
|
|
130
|
+
[convertedComponents.responses, 'response', ['components', 'responses'], ['components', 'responses']],
|
|
131
|
+
[convertedComponents.headers, 'header', ['components', 'headers'], ['components', 'headers']],
|
|
132
|
+
[
|
|
133
|
+
convertedComponents.requestBodies,
|
|
134
|
+
'request-body',
|
|
135
|
+
['components', 'requestBodies'],
|
|
136
|
+
['components', 'requestBodies'],
|
|
137
|
+
],
|
|
138
|
+
[convertedComponents.pathItems, 'path-item', ['components', 'pathItems'], ['components', 'pathItems']],
|
|
139
|
+
[convertedComponents.callbacks, 'callback', ['components', 'callbacks'], ['components', 'callbacks']],
|
|
140
|
+
];
|
|
141
|
+
for (const [entries, childKind, from, to] of sections) {
|
|
142
|
+
if (!isObject(entries)) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const values = {};
|
|
146
|
+
for (const [name, entry] of Object.entries(entries)) {
|
|
147
|
+
setOwn(values, name, await visit(entry, {
|
|
148
|
+
kind: childKind,
|
|
149
|
+
source: [...source, ...from, name],
|
|
150
|
+
output: [...output, ...to, name],
|
|
151
|
+
consumes: sourceConsumes,
|
|
152
|
+
produces: sourceProduces,
|
|
153
|
+
semanticPath: [...to, name],
|
|
154
|
+
}));
|
|
155
|
+
}
|
|
156
|
+
convertedComponents[to[1]] = values;
|
|
157
|
+
}
|
|
158
|
+
if (v2 && isObject(parameters)) {
|
|
159
|
+
for (const [name, parameter] of Object.entries(parameters)) {
|
|
160
|
+
const resolved = await resolveParameter(parameter);
|
|
161
|
+
const body = isObject(resolved) && (resolved.in === 'body' || resolved.in === 'formData');
|
|
162
|
+
const section = body ? 'requestBodies' : 'parameters';
|
|
163
|
+
const entries = isObject(convertedComponents[section]) ? convertedComponents[section] : {};
|
|
164
|
+
convertedComponents[section] = entries;
|
|
165
|
+
setOwn(entries, name, await visit(body ? structuredClone(resolved) : parameter, {
|
|
166
|
+
kind: body ? 'request-body' : 'parameter',
|
|
167
|
+
source: [...source, 'parameters', name],
|
|
168
|
+
output: [...output, 'components', section, name],
|
|
169
|
+
consumes: sourceConsumes,
|
|
170
|
+
produces: sourceProduces,
|
|
171
|
+
semanticPath: ['components', section, name],
|
|
172
|
+
}));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (Object.keys(convertedComponents).length) {
|
|
176
|
+
result.components = { ...(isObject(result.components) ? result.components : {}), ...convertedComponents };
|
|
177
|
+
}
|
|
178
|
+
if (isObject(paths)) {
|
|
179
|
+
const convertedPaths = {};
|
|
180
|
+
result.paths = convertedPaths;
|
|
181
|
+
for (const [name, entry] of Object.entries(paths)) {
|
|
182
|
+
const converted = name.startsWith('x-')
|
|
183
|
+
? structuredClone(entry)
|
|
184
|
+
: await visit(entry, {
|
|
185
|
+
kind: 'path-item',
|
|
186
|
+
source: [...source, 'paths', name],
|
|
187
|
+
output: [...output, 'paths', name],
|
|
188
|
+
consumes: sourceConsumes,
|
|
189
|
+
produces: sourceProduces,
|
|
190
|
+
semanticPath: ['paths', name],
|
|
191
|
+
});
|
|
192
|
+
setOwn(convertedPaths, name, converted);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (isObject(value['x-webhooks'])) {
|
|
196
|
+
result.webhooks = await map(value['x-webhooks'], 'path-item', ['x-webhooks'], ['webhooks']);
|
|
197
|
+
}
|
|
198
|
+
delete result['x-webhooks'];
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
if (kind === 'callback') {
|
|
202
|
+
for (const [expression, pathItem] of Object.entries(value)) {
|
|
203
|
+
if (!expression.startsWith('x-') && expression !== '$ref') {
|
|
204
|
+
setOwn(result, expression, await child(pathItem, 'path-item', [expression]));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
if (kind === 'path-item') {
|
|
210
|
+
for (const method of HTTP_METHODS) {
|
|
211
|
+
if (!isObject(value[method])) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const operation = value[method];
|
|
215
|
+
// Path-level parameters participate in each operation's effective parameter list.
|
|
216
|
+
const inherited = v2 && Array.isArray(value.parameters) ? value.parameters : [];
|
|
217
|
+
const own = Array.isArray(operation.parameters) ? operation.parameters : [];
|
|
218
|
+
const ownIdentities = await Promise.all(own.map(resolveParameter));
|
|
219
|
+
const effective = [];
|
|
220
|
+
for (const [index, parameter] of inherited.entries()) {
|
|
221
|
+
const identity = await resolveParameter(parameter);
|
|
222
|
+
const overridden = ownIdentities.some((candidate) => isObject(identity) &&
|
|
223
|
+
isObject(candidate) &&
|
|
224
|
+
typeof identity.name === 'string' &&
|
|
225
|
+
typeof identity.in === 'string' &&
|
|
226
|
+
candidate.name === identity.name &&
|
|
227
|
+
candidate.in === identity.in);
|
|
228
|
+
if (!overridden) {
|
|
229
|
+
effective.push({ parameter, source: [...source, 'parameters', String(index)] });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
effective.push(...own.map((parameter, index) => ({ parameter, source: [...source, method, 'parameters', String(index)] })));
|
|
233
|
+
result[method] = await visit(inherited.length ? { ...operation, parameters: effective.map((entry) => entry.parameter) } : operation, {
|
|
234
|
+
kind: 'operation',
|
|
235
|
+
source: [...source, method],
|
|
236
|
+
output: [...output, method],
|
|
237
|
+
consumes,
|
|
238
|
+
produces,
|
|
239
|
+
semanticPath: [...semanticPath, method],
|
|
240
|
+
parameterSources: effective.map((entry) => entry.source),
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
if (v2) {
|
|
244
|
+
delete result.parameters;
|
|
245
|
+
}
|
|
246
|
+
else if (Array.isArray(value.parameters)) {
|
|
247
|
+
result.parameters = await Promise.all(value.parameters.map((parameter, index) => child(parameter, 'parameter', ['parameters', String(index)])));
|
|
248
|
+
}
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
if (kind === 'operation') {
|
|
252
|
+
const effectiveConsumes = Array.isArray(value.consumes) ? value.consumes : consumes;
|
|
253
|
+
const effectiveProduces = Array.isArray(value.produces) ? value.produces : produces;
|
|
254
|
+
if (value.consumes || value.produces) {
|
|
255
|
+
contexts.push({ path: output, consumes: effectiveConsumes, produces: effectiveProduces });
|
|
256
|
+
}
|
|
257
|
+
if (isObject(value.callbacks)) {
|
|
258
|
+
result.callbacks = await map(value.callbacks, 'callback', ['callbacks']);
|
|
259
|
+
}
|
|
260
|
+
const parameters = [];
|
|
261
|
+
const forms = [];
|
|
262
|
+
if (Array.isArray(value.parameters)) {
|
|
263
|
+
for (const [index, original] of value.parameters.entries()) {
|
|
264
|
+
const parameter = await resolveParameter(original);
|
|
265
|
+
const parameterSource = parameterSources?.[index] ?? [...source, 'parameters', String(index)];
|
|
266
|
+
if (v2 && isObject(parameter) && parameter.in === 'formData') {
|
|
267
|
+
forms.push({ parameter: structuredClone(parameter), source: parameterSource });
|
|
268
|
+
}
|
|
269
|
+
else if (v2 && isObject(parameter) && parameter.in === 'body') {
|
|
270
|
+
result.requestBody = await visit(parameter, {
|
|
271
|
+
kind: 'request-body',
|
|
272
|
+
source: parameterSource,
|
|
273
|
+
output: [...output, 'requestBody'],
|
|
274
|
+
consumes: effectiveConsumes,
|
|
275
|
+
produces: effectiveProduces,
|
|
276
|
+
semanticPath: [...semanticPath, 'requestBody'],
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
parameters.push(await visit(original, {
|
|
281
|
+
kind: 'parameter',
|
|
282
|
+
source: parameterSource,
|
|
283
|
+
output: [...output, 'parameters', String(parameters.length)],
|
|
284
|
+
consumes: effectiveConsumes,
|
|
285
|
+
produces: effectiveProduces,
|
|
286
|
+
semanticPath: [...semanticPath, 'parameters', String(parameters.length)],
|
|
287
|
+
}));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (parameters.length) {
|
|
291
|
+
result.parameters = parameters;
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
delete result.parameters;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (forms.length) {
|
|
298
|
+
const body = migrateFormDataParameter(forms.map((entry) => entry.parameter), [...effectiveConsumes]);
|
|
299
|
+
for (const form of forms) {
|
|
300
|
+
rememberFormFields(form.parameter, form.source, [...output, 'requestBody'], Object.keys(body.content ?? {}));
|
|
301
|
+
}
|
|
302
|
+
result.requestBody = await visit(body, {
|
|
303
|
+
kind: 'request-body',
|
|
304
|
+
source: [...source, 'parameters'],
|
|
305
|
+
output: [...output, 'requestBody'],
|
|
306
|
+
generated: true,
|
|
307
|
+
consumes: effectiveConsumes,
|
|
308
|
+
produces: effectiveProduces,
|
|
309
|
+
semanticPath: [...semanticPath, 'requestBody'],
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
if (isObject(value.responses)) {
|
|
313
|
+
const convertedResponses = {};
|
|
314
|
+
result.responses = convertedResponses;
|
|
315
|
+
for (const [status, response] of Object.entries(value.responses)) {
|
|
316
|
+
const converted = await visit(response, {
|
|
317
|
+
kind: 'response',
|
|
318
|
+
source: [...source, 'responses', status],
|
|
319
|
+
output: [...output, 'responses', status],
|
|
320
|
+
consumes: effectiveConsumes,
|
|
321
|
+
produces: effectiveProduces,
|
|
322
|
+
semanticPath: [...semanticPath, 'responses', status],
|
|
323
|
+
});
|
|
324
|
+
setOwn(convertedResponses, status, converted);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (isObject(value.requestBody)) {
|
|
328
|
+
result.requestBody = await child(value.requestBody, 'request-body', ['requestBody']);
|
|
329
|
+
}
|
|
330
|
+
delete result.consumes;
|
|
331
|
+
delete result.produces;
|
|
332
|
+
return result;
|
|
333
|
+
}
|
|
334
|
+
if (kind === 'schema') {
|
|
335
|
+
if (v2) {
|
|
336
|
+
migrateSchema(result, false);
|
|
337
|
+
if (result.type === 'file') {
|
|
338
|
+
result.type = 'string';
|
|
339
|
+
result.format = 'binary';
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
// Record where composition members move before visiting descendants, so references
|
|
343
|
+
// into nullable unions still resolve after the null alternative is added.
|
|
344
|
+
const composition = getNullableComposition(result);
|
|
345
|
+
for (const [key, entry] of Object.entries(result)) {
|
|
346
|
+
if (schemaMaps.has(key)) {
|
|
347
|
+
result[key] = await map(entry, 'schema', [key]);
|
|
348
|
+
}
|
|
349
|
+
else if (schemaChildren.has(key)) {
|
|
350
|
+
if (Array.isArray(entry)) {
|
|
351
|
+
result[key] = await Promise.all(entry.map((item, index) => visit(item, {
|
|
352
|
+
kind: 'schema',
|
|
353
|
+
source: [...source, key, String(index)],
|
|
354
|
+
output: composition?.keywords.includes(key)
|
|
355
|
+
? [
|
|
356
|
+
...output,
|
|
357
|
+
'anyOf',
|
|
358
|
+
'0',
|
|
359
|
+
...(key === 'allOf' && composition.unwrapAllOf ? [] : [key, String(index)]),
|
|
360
|
+
]
|
|
361
|
+
: [...output, key, String(index)],
|
|
362
|
+
consumes,
|
|
363
|
+
produces,
|
|
364
|
+
semanticPath: [...semanticPath, key, String(index)],
|
|
365
|
+
generated,
|
|
366
|
+
})));
|
|
367
|
+
}
|
|
368
|
+
else if (isObject(entry)) {
|
|
369
|
+
result[key] = await child(entry, 'schema', [key]);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
const converted = applyChangesToDocument(result, semanticPath);
|
|
374
|
+
return converted;
|
|
375
|
+
}
|
|
376
|
+
if (v2 && kind === 'parameter' && !result.$ref) {
|
|
377
|
+
if (result.in === 'formData' || result.in === 'body') {
|
|
378
|
+
throw new Error('Body and form parameters require an owning operation or request-body context');
|
|
379
|
+
}
|
|
380
|
+
result = transformParameterObject(result);
|
|
381
|
+
for (const key of parameterSchemaFields) {
|
|
382
|
+
if (Object.hasOwn(value, key)) {
|
|
383
|
+
remember([...source, key], [...output, 'schema', key]);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (v2 && kind === 'header' && !result.$ref) {
|
|
388
|
+
result = transformResponseHeader(result);
|
|
389
|
+
for (const key of parameterSchemaFields) {
|
|
390
|
+
if (Object.hasOwn(value, key)) {
|
|
391
|
+
remember([...source, key], [...output, 'schema', key]);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (v2 && kind === 'request-body' && result.in === 'formData') {
|
|
396
|
+
const body = migrateFormDataParameter([result], [...consumes]);
|
|
397
|
+
rememberFormFields(value, source, output, Object.keys(body.content ?? {}));
|
|
398
|
+
result = body;
|
|
399
|
+
}
|
|
400
|
+
if (v2 && kind === 'request-body' && result.in === 'body') {
|
|
401
|
+
result = migrateBodyParameter(result, [...consumes]);
|
|
402
|
+
for (const media of consumes) {
|
|
403
|
+
remember([...source, 'schema'], [...output, 'content', media, 'schema']);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (v2 && kind === 'response' && !result.$ref) {
|
|
407
|
+
const { headers, ...response } = result;
|
|
408
|
+
result = transformResponseObject(response, [...produces]);
|
|
409
|
+
if (headers !== undefined) {
|
|
410
|
+
result.headers = headers;
|
|
411
|
+
}
|
|
412
|
+
for (const media of produces) {
|
|
413
|
+
remember([...source, 'schema'], [...output, 'content', media, 'schema']);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (isObject(result.schema)) {
|
|
417
|
+
const schemaSource = v2 && (kind === 'parameter' || kind === 'header') ? source : [...source, 'schema'];
|
|
418
|
+
result.schema = await visit(result.schema, {
|
|
419
|
+
kind: 'schema',
|
|
420
|
+
source: schemaSource,
|
|
421
|
+
output: [...output, 'schema'],
|
|
422
|
+
consumes,
|
|
423
|
+
produces,
|
|
424
|
+
semanticPath: [...semanticPath, 'schema'],
|
|
425
|
+
generated,
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
if (isObject(result.headers)) {
|
|
429
|
+
result.headers = await map(v2 && kind === 'response' ? value.headers : result.headers, 'header', ['headers']);
|
|
430
|
+
}
|
|
431
|
+
if (isObject(result.content)) {
|
|
432
|
+
for (const [media, entry] of Object.entries(result.content)) {
|
|
433
|
+
if (!isObject(entry)) {
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
if (entry.schema !== undefined) {
|
|
437
|
+
const from = v2 && (kind === 'response' || value.in === 'body') ? ['schema'] : ['content', media, 'schema'];
|
|
438
|
+
entry.schema = await child(entry.schema, 'schema', from, ['content', media, 'schema']);
|
|
439
|
+
}
|
|
440
|
+
if (isObject(entry.encoding)) {
|
|
441
|
+
for (const [name, encoding] of Object.entries(entry.encoding)) {
|
|
442
|
+
if (isObject(encoding) && isObject(encoding.headers)) {
|
|
443
|
+
encoding.headers = await map(encoding.headers, 'header', ['content', media, 'encoding', name, 'headers']);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
applyChangesToDocument(entry, [...semanticPath, 'content', media]);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
// Match the document upgrader's example normalization without traversing literal values.
|
|
451
|
+
if (kind === 'parameter' || kind === 'header') {
|
|
452
|
+
applyChangesToDocument(result, semanticPath);
|
|
453
|
+
}
|
|
454
|
+
return result;
|
|
455
|
+
};
|
|
456
|
+
const semanticPath = options.kind === 'schema' ? ['components', 'schemas', 'fragment'] : [];
|
|
457
|
+
const value = await visit(structuredClone(input), {
|
|
458
|
+
kind: options.kind,
|
|
459
|
+
source: [],
|
|
460
|
+
output: [],
|
|
461
|
+
consumes: options.consumes ?? ['application/json'],
|
|
462
|
+
produces: options.produces ?? ['application/json'],
|
|
463
|
+
semanticPath,
|
|
464
|
+
});
|
|
465
|
+
return { value, locations, contexts };
|
|
466
|
+
};
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"openapi 3.0",
|
|
21
21
|
"openapi 3.1"
|
|
22
22
|
],
|
|
23
|
-
"version": "0.
|
|
23
|
+
"version": "0.5.1",
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": ">=22"
|
|
26
26
|
},
|
|
@@ -71,12 +71,12 @@
|
|
|
71
71
|
},
|
|
72
72
|
"sideEffects": false,
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@scalar/helpers": "0.
|
|
74
|
+
"@scalar/helpers": "0.19.0",
|
|
75
75
|
"@scalar/openapi-types": "0.9.10"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@scalar/types": "0.
|
|
79
|
-
"vite": "8.3.
|
|
78
|
+
"@scalar/types": "0.31.0",
|
|
79
|
+
"vite": "8.3.8"
|
|
80
80
|
},
|
|
81
81
|
"scripts": {
|
|
82
82
|
"build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|