@scalar/openapi-upgrader 0.4.6 → 0.4.8
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 +20 -0
- package/dist/2.0-to-3.0/migrate-discriminator-mappings.d.ts +3 -0
- package/dist/2.0-to-3.0/migrate-discriminator-mappings.d.ts.map +1 -0
- package/dist/2.0-to-3.0/migrate-discriminator-mappings.js +72 -0
- 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 +37 -17
- 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 +2 -61
- package/dist/helpers/is-inside-data-value.d.ts +17 -0
- package/dist/helpers/is-inside-data-value.d.ts.map +1 -0
- package/dist/helpers/is-inside-data-value.js +82 -0
- package/dist/helpers/traverse.d.ts.map +1 -1
- package/dist/helpers/traverse.js +2 -1
- package/package.json +4 -4
- package/dist/upgrade.bench.d.ts +0 -2
- package/dist/upgrade.bench.d.ts.map +0 -1
- package/dist/upgrade.bench.js +0 -47
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @scalar/openapi-upgrader
|
|
2
2
|
|
|
3
|
+
## 0.4.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#10736](https://github.com/scalar/scalar/pull/10736): Exclude benchmark fixtures from published build output.
|
|
8
|
+
- [#10925](https://github.com/scalar/scalar/pull/10925): Keep `type: file` inside Swagger 2.0 example data when upgrading to OpenAPI 3.0. Continue converting file schemas in reusable responses and parameters named like data keywords. Preserve example and default data on schemas and reusable parameters whose names match schema-map keywords. Continue upgrading response headers whose names match data keywords. Limit Swagger-specific data guards to the 2.0 upgrade so OpenAPI 3.0 request bodies and callbacks named `x-example` still upgrade. Preserve example payloads on reusable objects named `headers`.
|
|
9
|
+
- [#10948](https://github.com/scalar/scalar/pull/10948): Convert Swagger 2.0 discriminator strings to Discriminator Objects so upgrades preserve the discriminating property name.
|
|
10
|
+
|
|
11
|
+
Preserve x-ms-discriminator-value and x-discriminator-value aliases from allOf subtypes in discriminator mappings, including inherited subtypes, without replacing authored mappings.
|
|
12
|
+
|
|
13
|
+
- [#10949](https://github.com/scalar/scalar/pull/10949): Show response header examples, including media type examples and OpenAPI 3.2 cookie examples, with explicit examples taking precedence over schema examples. Preserve this precedence through composed schemas and fall back to media type examples when header examples have no usable values.
|
|
14
|
+
|
|
15
|
+
Keep external example URLs visible and preserve line breaks in displayed string examples. Across all schemas, array-level examples take precedence over item examples.
|
|
16
|
+
|
|
17
|
+
Preserve `x-example` and `x-examples` on OpenAPI 2.0 response headers during upgrades.
|
|
18
|
+
|
|
19
|
+
- [#10920](https://github.com/scalar/scalar/pull/10920): Prefer operation-level `produces` over the global `produces` when upgrading Swagger 2.0 response content.
|
|
20
|
+
|
|
21
|
+
## 0.4.7
|
|
22
|
+
|
|
3
23
|
## 0.4.6
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate-discriminator-mappings.d.ts","sourceRoot":"","sources":["../../src/2.0-to-3.0/migrate-discriminator-mappings.ts"],"names":[],"mappings":"AAIA,mGAAmG;AACnG,eAAO,MAAM,4BAA4B,GAAI,aAAa,OAAO,KAAG,IAwEnE,CAAA"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { escapeJsonPointer } from '@scalar/helpers/json/escape-json-pointer';
|
|
2
|
+
import { unescapeJsonPointerSegment } from '@scalar/helpers/json/unescape-json-pointer-segment';
|
|
3
|
+
import { isObjectLike } from '@scalar/helpers/object/is-object';
|
|
4
|
+
/** Preserve vendor wire values when OpenAPI 2.0 inheritance becomes an OpenAPI 3 discriminator. */
|
|
5
|
+
export const migrateDiscriminatorMappings = (definitions) => {
|
|
6
|
+
if (!isObjectLike(definitions)) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const mappings = new Map();
|
|
10
|
+
for (const [name, schema] of Object.entries(definitions)) {
|
|
11
|
+
if (!isObjectLike(schema)) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
const aliases = [schema['x-ms-discriminator-value'], schema['x-discriminator-value']].filter((value) => typeof value === 'string');
|
|
15
|
+
if (aliases.length === 0) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const reference = `#/components/schemas/${encodeURIComponent(escapeJsonPointer(name))}`;
|
|
19
|
+
const pending = Array.isArray(schema.allOf) ? [...schema.allOf] : [];
|
|
20
|
+
const seen = new Set([schema]);
|
|
21
|
+
while (pending.length > 0) {
|
|
22
|
+
const member = pending.pop();
|
|
23
|
+
if (!isObjectLike(member) || seen.has(member)) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
seen.add(member);
|
|
27
|
+
if (Array.isArray(member.allOf)) {
|
|
28
|
+
pending.push(...member.allOf);
|
|
29
|
+
}
|
|
30
|
+
if (typeof member.$ref !== 'string' || !member.$ref.startsWith('#/definitions/')) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
// Invalid escaping or unresolved/external references cannot identify a local parent.
|
|
34
|
+
let parentName;
|
|
35
|
+
try {
|
|
36
|
+
const segment = decodeURIComponent(member.$ref.slice('#/definitions/'.length));
|
|
37
|
+
if (segment.includes('/')) {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
parentName = unescapeJsonPointerSegment(segment);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const parent = Object.hasOwn(definitions, parentName) ? definitions[parentName] : undefined;
|
|
46
|
+
if (!isObjectLike(parent) || seen.has(parent)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
pending.push(parent);
|
|
50
|
+
if (!isObjectLike(parent.discriminator) || typeof parent.discriminator.propertyName !== 'string') {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const mapping = mappings.get(parent.discriminator) ?? new Map();
|
|
54
|
+
for (const alias of aliases) {
|
|
55
|
+
// Conflicting wire values cannot select a subtype reliably; keep them unmapped.
|
|
56
|
+
mapping.set(alias, mapping.has(alias) && mapping.get(alias) !== reference ? undefined : reference);
|
|
57
|
+
}
|
|
58
|
+
mappings.set(parent.discriminator, mapping);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const [discriminator, aliases] of mappings) {
|
|
62
|
+
const derived = Object.fromEntries([...aliases].filter(([, reference]) => reference !== undefined));
|
|
63
|
+
if (Object.keys(derived).length === 0) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (discriminator.mapping !== undefined && !isObjectLike(discriminator.mapping)) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
// Authored mappings take precedence, including special keys such as __proto__.
|
|
70
|
+
discriminator.mapping = { ...derived, ...discriminator.mapping };
|
|
71
|
+
}
|
|
72
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upgrade-from-two-to-three.d.ts","sourceRoot":"","sources":["../../src/2.0-to-3.0/upgrade-from-two-to-three.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAa,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"upgrade-from-two-to-three.d.ts","sourceRoot":"","sources":["../../src/2.0-to-3.0/upgrade-from-two-to-three.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAa,SAAS,EAAE,MAAM,uBAAuB,CAAA;AACjE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAmMxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CAyczE"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { isObjectLike } from '@scalar/helpers/object/is-object';
|
|
2
|
+
import { isInsideDataValue } from '../helpers/is-inside-data-value.js';
|
|
2
3
|
import { traverse } from '../helpers/traverse.js';
|
|
4
|
+
import { migrateDiscriminatorMappings } from './migrate-discriminator-mappings.js';
|
|
3
5
|
const DEFAULT_MEDIA_TYPE = 'application/json';
|
|
4
6
|
/** Extracts and removes x-example and x-examples extensions from an object */
|
|
5
7
|
function extractXExampleExtensions(obj) {
|
|
@@ -189,8 +191,9 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
189
191
|
// Schemas
|
|
190
192
|
if (document.definitions) {
|
|
191
193
|
for (const schema of Object.values(document.definitions)) {
|
|
192
|
-
|
|
194
|
+
migrateSchema(schema);
|
|
193
195
|
}
|
|
196
|
+
migrateDiscriminatorMappings(document.definitions);
|
|
194
197
|
document.components = Object.assign({}, document.components, {
|
|
195
198
|
schemas: document.definitions,
|
|
196
199
|
});
|
|
@@ -205,8 +208,8 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
205
208
|
});
|
|
206
209
|
}
|
|
207
210
|
// Transform file type to string with binary format
|
|
208
|
-
document = traverse(document, (schema) => {
|
|
209
|
-
if (schema.type === 'file') {
|
|
211
|
+
document = traverse(document, (schema, path) => {
|
|
212
|
+
if (schema.type === 'file' && !isInsideDataValue(path, '2.0')) {
|
|
210
213
|
schema.type = 'string';
|
|
211
214
|
schema.format = 'binary';
|
|
212
215
|
}
|
|
@@ -291,7 +294,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
291
294
|
const produces = document.produces ?? [DEFAULT_MEDIA_TYPE];
|
|
292
295
|
// Transform schema to content
|
|
293
296
|
if (responseObj.schema) {
|
|
294
|
-
|
|
297
|
+
migrateSchema(responseObj.schema);
|
|
295
298
|
if (typeof responseObj.content !== 'object') {
|
|
296
299
|
responseObj.content = {};
|
|
297
300
|
}
|
|
@@ -407,8 +410,8 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
407
410
|
}, {});
|
|
408
411
|
}
|
|
409
412
|
if (responseItem.schema) {
|
|
410
|
-
|
|
411
|
-
const produces =
|
|
413
|
+
migrateSchema(responseItem.schema);
|
|
414
|
+
const produces = operationItem.produces ?? document.produces ?? [DEFAULT_MEDIA_TYPE];
|
|
412
415
|
if (typeof responseItem.content !== 'object') {
|
|
413
416
|
responseItem.content = {};
|
|
414
417
|
}
|
|
@@ -426,11 +429,11 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
426
429
|
if (typeof responseItem.content !== 'object') {
|
|
427
430
|
responseItem.content = {};
|
|
428
431
|
}
|
|
429
|
-
const produces =
|
|
432
|
+
const produces = operationItem.produces ?? document.produces ?? [DEFAULT_MEDIA_TYPE];
|
|
430
433
|
const defaultMediaType = produces[0] ?? DEFAULT_MEDIA_TYPE;
|
|
431
434
|
for (const [key, exampleValue] of Object.entries(responseItem.examples)) {
|
|
432
435
|
// Media-type keys (e.g. application/json) go to content[key]; named example keys go to content[defaultMediaType].examples[key].
|
|
433
|
-
if (isMediaTypeKey(key,
|
|
436
|
+
if (isMediaTypeKey(key, produces)) {
|
|
434
437
|
if (typeof responseItem.content[key] !== 'object') {
|
|
435
438
|
responseItem.content[key] = {};
|
|
436
439
|
}
|
|
@@ -528,13 +531,24 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
528
531
|
return document;
|
|
529
532
|
}
|
|
530
533
|
/**
|
|
531
|
-
* Swagger
|
|
532
|
-
* Visit only
|
|
534
|
+
* Converts Swagger discriminator and nullability fields to their OpenAPI 3.0 forms.
|
|
535
|
+
* Visit only OpenAPI 2.0 subschema keywords so example/default values and custom extensions remain user data.
|
|
533
536
|
*/
|
|
534
|
-
const
|
|
537
|
+
const migrateSchema = (schema) => {
|
|
535
538
|
if (!isObjectLike(schema)) {
|
|
536
539
|
return;
|
|
537
540
|
}
|
|
541
|
+
if (typeof schema.discriminator === 'string') {
|
|
542
|
+
const propertyName = schema.discriminator;
|
|
543
|
+
schema.discriminator = { propertyName };
|
|
544
|
+
// OpenAPI 2.0 requires the discriminating property, including an empty property name.
|
|
545
|
+
if (schema.required === undefined) {
|
|
546
|
+
schema.required = [propertyName];
|
|
547
|
+
}
|
|
548
|
+
else if (Array.isArray(schema.required) && !schema.required.includes(propertyName)) {
|
|
549
|
+
schema.required.push(propertyName);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
538
552
|
if (typeof schema['x-nullable'] === 'boolean') {
|
|
539
553
|
// False is already the default in OpenAPI 3.0 and needs no keyword in later versions.
|
|
540
554
|
if (schema['x-nullable']) {
|
|
@@ -544,14 +558,14 @@ const migrateSchemaNullability = (schema) => {
|
|
|
544
558
|
}
|
|
545
559
|
if (isObjectLike(schema.properties)) {
|
|
546
560
|
for (const property of Object.values(schema.properties)) {
|
|
547
|
-
|
|
561
|
+
migrateSchema(property);
|
|
548
562
|
}
|
|
549
563
|
}
|
|
550
|
-
|
|
551
|
-
|
|
564
|
+
migrateSchema(schema.items);
|
|
565
|
+
migrateSchema(schema.additionalProperties);
|
|
552
566
|
if (Array.isArray(schema.allOf)) {
|
|
553
567
|
for (const member of schema.allOf) {
|
|
554
|
-
|
|
568
|
+
migrateSchema(member);
|
|
555
569
|
}
|
|
556
570
|
}
|
|
557
571
|
};
|
|
@@ -582,7 +596,7 @@ function transformItemsObject(obj) {
|
|
|
582
596
|
}
|
|
583
597
|
return acc;
|
|
584
598
|
}, {});
|
|
585
|
-
|
|
599
|
+
migrateSchema(schema);
|
|
586
600
|
return schema;
|
|
587
601
|
}
|
|
588
602
|
function getParameterLocation(location) {
|
|
@@ -651,9 +665,15 @@ function transformResponseHeader(header) {
|
|
|
651
665
|
};
|
|
652
666
|
}
|
|
653
667
|
const schema = transformItemsObject(header);
|
|
668
|
+
const { xExample, xExamples } = extractXExampleExtensions(header);
|
|
669
|
+
const examples = isNonEmptyObject(xExamples)
|
|
670
|
+
? Object.fromEntries(Object.entries(xExamples).map(([name, value]) => [name, wrapAsExampleObject(value)]))
|
|
671
|
+
: undefined;
|
|
654
672
|
return {
|
|
655
673
|
...header,
|
|
656
674
|
schema,
|
|
675
|
+
...(examples ? { examples } : {}),
|
|
676
|
+
...(!examples && xExample !== undefined ? { example: xExample } : {}),
|
|
657
677
|
};
|
|
658
678
|
}
|
|
659
679
|
const querySerialization = {
|
|
@@ -725,7 +745,7 @@ function migrateBodyParameter(bodyParameter, consumes) {
|
|
|
725
745
|
delete bodyParameter.name;
|
|
726
746
|
delete bodyParameter.in;
|
|
727
747
|
const { schema, ...requestBody } = bodyParameter;
|
|
728
|
-
|
|
748
|
+
migrateSchema(schema);
|
|
729
749
|
const requestBodyObject = {
|
|
730
750
|
content: {},
|
|
731
751
|
...requestBody,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upgrade-from-three-to-three-one.d.ts","sourceRoot":"","sources":["../../src/3.0-to-3.1/upgrade-from-three-to-three-one.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"upgrade-from-three-to-three-one.d.ts","sourceRoot":"","sources":["../../src/3.0-to-3.1/upgrade-from-three-to-three-one.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAmDxD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,eAAe,EAAE,aAAa;;;;;;;;;;;;GAcxE"}
|
|
@@ -1,65 +1,6 @@
|
|
|
1
1
|
import { isSchemaPath } from '@scalar/helpers/openapi/is-schema-path';
|
|
2
|
+
import { isInsideDataValue, isNamedSchemaMap } from '../helpers/is-inside-data-value.js';
|
|
2
3
|
import { traverse } from '../helpers/traverse.js';
|
|
3
|
-
// Segments whose value is a map of *named* subschemas. Keys inside these maps are
|
|
4
|
-
// arbitrary names (property names, definition names), never schema keywords.
|
|
5
|
-
const NAMED_SCHEMA_MAP_SEGMENTS = new Set(['properties', 'patternProperties', '$defs', 'definitions']);
|
|
6
|
-
/**
|
|
7
|
-
* Determine if the node at `path` is a map of named subschemas rather than a schema object.
|
|
8
|
-
*
|
|
9
|
-
* This matters because the 3.0 to 3.1 upgrade rewrites the `example` keyword into `examples`.
|
|
10
|
-
* A key called `example` (or `examples`) inside one of these maps is a member *name*, not the
|
|
11
|
-
* schema keyword, so it must be left untouched.
|
|
12
|
-
*/
|
|
13
|
-
function isNamedSchemaMap(path) {
|
|
14
|
-
const last = path?.[path.length - 1];
|
|
15
|
-
if (last === undefined) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
// OpenAPI's named-schema map lives at components/schemas
|
|
19
|
-
if (last === 'schemas' && path?.[path.length - 2] === 'components') {
|
|
20
|
-
return true;
|
|
21
|
-
}
|
|
22
|
-
return NAMED_SCHEMA_MAP_SEGMENTS.has(last);
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Determine if `path[index]` is the `default` entry of a Responses Object.
|
|
26
|
-
*
|
|
27
|
-
* That `default` is a response, not the `default` keyword, so everything under it (its schemas
|
|
28
|
-
* included) still has to be upgraded. A schema property literally called `responses` does not
|
|
29
|
-
* count: there `default` is the keyword again.
|
|
30
|
-
*/
|
|
31
|
-
function isDefaultResponse(path, index) {
|
|
32
|
-
return path[index] === 'default' && path[index - 1] === 'responses' && !isNamedSchemaMap(path.slice(0, index - 1));
|
|
33
|
-
}
|
|
34
|
-
// Keywords whose value is arbitrary data rather than a schema. The upgrade rules only make
|
|
35
|
-
// sense for schema keywords, so nothing nested under these may be rewritten.
|
|
36
|
-
const DATA_KEYWORDS = new Set(['example', 'default', 'const', 'enum']);
|
|
37
|
-
/**
|
|
38
|
-
* Determine if the node at `path` lives inside a data value rather than a schema.
|
|
39
|
-
*
|
|
40
|
-
* The traversal visits every object node, but a value held by keywords like `example` or
|
|
41
|
-
* `default` is user data, not a schema. Applying the schema transforms to it would mangle it
|
|
42
|
-
* (turning `nullable: true` into a type array, renaming a nested `x-webhooks`, and so on).
|
|
43
|
-
*/
|
|
44
|
-
function isInsideDataValue(path) {
|
|
45
|
-
if (!path) {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
for (const [index, segment] of path.entries()) {
|
|
49
|
-
// Only treat it as data when the keyword is a real keyword. Inside a named-schema map the
|
|
50
|
-
// same word (e.g. a property literally called `default`) is just a member name.
|
|
51
|
-
if (DATA_KEYWORDS.has(segment) && !isNamedSchemaMap(path.slice(0, index)) && !isDefaultResponse(path, index)) {
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
// The `value` of an Example Object (`examples/<name>/value`) holds arbitrary data too.
|
|
55
|
-
// Require a real examples map two segments up so a property named `value` inside a schema
|
|
56
|
-
// named `examples` is not mistaken for it.
|
|
57
|
-
if (segment === 'value' && path[index - 2] === 'examples' && !isNamedSchemaMap(path.slice(0, index - 2))) {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
4
|
// Keywords that only describe a schema. An arm made of these and `nullable` constrains nothing else.
|
|
64
5
|
const ANNOTATION_KEYWORDS = new Set([
|
|
65
6
|
'title',
|
|
@@ -118,7 +59,7 @@ export function upgradeFromThreeToThreeOne(originalContent) {
|
|
|
118
59
|
const applyChangesToDocument = (schema, path) => {
|
|
119
60
|
// 0. Bail out inside data values (example, default, const, enum). These hold user data, not a
|
|
120
61
|
// schema, so none of the schema transforms below apply and running them would corrupt the data.
|
|
121
|
-
if (isInsideDataValue(path)) {
|
|
62
|
+
if (isInsideDataValue(path, '3.0')) {
|
|
122
63
|
return schema;
|
|
123
64
|
}
|
|
124
65
|
// 1. Handle nullable types
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determine if the node at `path` is a map of named subschemas rather than a schema object.
|
|
3
|
+
*
|
|
4
|
+
* This matters because the 3.0 to 3.1 upgrade rewrites the `example` keyword into `examples`.
|
|
5
|
+
* A key called `example` (or `examples`) inside one of these maps is a member *name*, not the
|
|
6
|
+
* schema keyword, so it must be left untouched.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isNamedSchemaMap(path: string[] | undefined): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Determine if the node at `path` lives inside a data value rather than a schema.
|
|
11
|
+
*
|
|
12
|
+
* The traversal visits every object node, but a value held by keywords like `example` or
|
|
13
|
+
* `default` is user data, not a schema. Applying the schema transforms to it would mangle it
|
|
14
|
+
* (turning `nullable: true` into a type array, renaming a nested `x-webhooks`, and so on).
|
|
15
|
+
*/
|
|
16
|
+
export declare function isInsideDataValue(path: string[] | undefined, sourceVersion: '2.0' | '3.0'): boolean;
|
|
17
|
+
//# sourceMappingURL=is-inside-data-value.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-inside-data-value.d.ts","sourceRoot":"","sources":["../../src/helpers/is-inside-data-value.ts"],"names":[],"mappings":"AAaA;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,SAAS,GAAG,OAAO,CAcpE;AAiBD;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE,aAAa,EAAE,KAAK,GAAG,KAAK,GAAG,OAAO,CAuCnG"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// Segments whose value is a map of *named* subschemas. Keys inside these maps are
|
|
2
|
+
// arbitrary names (property names, definition names), never schema keywords.
|
|
3
|
+
const NAMED_SCHEMA_MAP_SEGMENTS = new Set(['properties', 'patternProperties', '$defs', 'definitions']);
|
|
4
|
+
/** Identify response, parameter, and header names so their spelling cannot turn them into schema keywords. */
|
|
5
|
+
const isOpenApiObjectName = (path, index) => (index === 1 && (path[0] === 'responses' || path[0] === 'parameters')) ||
|
|
6
|
+
(index === 2 && path[0] === 'components' && (path[1] === 'responses' || path[1] === 'parameters')) ||
|
|
7
|
+
// A reusable object or header named `headers` is not itself a header map.
|
|
8
|
+
(path[index - 1] === 'headers' &&
|
|
9
|
+
!isOpenApiObjectName(path, index - 1) &&
|
|
10
|
+
!isNamedSchemaMap(path.slice(0, index - 1)));
|
|
11
|
+
/**
|
|
12
|
+
* Determine if the node at `path` is a map of named subschemas rather than a schema object.
|
|
13
|
+
*
|
|
14
|
+
* This matters because the 3.0 to 3.1 upgrade rewrites the `example` keyword into `examples`.
|
|
15
|
+
* A key called `example` (or `examples`) inside one of these maps is a member *name*, not the
|
|
16
|
+
* schema keyword, so it must be left untouched.
|
|
17
|
+
*/
|
|
18
|
+
export function isNamedSchemaMap(path) {
|
|
19
|
+
const last = path?.[path.length - 1];
|
|
20
|
+
if (!path || last === undefined || isOpenApiObjectName(path, path.length - 1)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
// OpenAPI's named-schema map lives at components/schemas
|
|
24
|
+
if (last === 'schemas' && path?.[path.length - 2] === 'components') {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
// A member of a named-schema map is a schema, even when its name is another map keyword.
|
|
28
|
+
return NAMED_SCHEMA_MAP_SEGMENTS.has(last) && !isNamedSchemaMap(path?.slice(0, -1));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Determine if `path[index]` is the `default` entry of a Responses Object.
|
|
32
|
+
*
|
|
33
|
+
* That `default` is a response, not the `default` keyword, so everything under it (its schemas
|
|
34
|
+
* included) still has to be upgraded. A schema property literally called `responses` does not
|
|
35
|
+
* count: there `default` is the keyword again.
|
|
36
|
+
*/
|
|
37
|
+
function isDefaultResponse(path, index) {
|
|
38
|
+
return path[index] === 'default' && path[index - 1] === 'responses' && !isNamedSchemaMap(path.slice(0, index - 1));
|
|
39
|
+
}
|
|
40
|
+
// Keywords whose value is arbitrary data rather than a schema. The upgrade rules only make
|
|
41
|
+
// sense for schema keywords, so nothing nested under these may be rewritten.
|
|
42
|
+
const DATA_KEYWORDS = new Set(['example', 'default', 'const', 'enum']);
|
|
43
|
+
/**
|
|
44
|
+
* Determine if the node at `path` lives inside a data value rather than a schema.
|
|
45
|
+
*
|
|
46
|
+
* The traversal visits every object node, but a value held by keywords like `example` or
|
|
47
|
+
* `default` is user data, not a schema. Applying the schema transforms to it would mangle it
|
|
48
|
+
* (turning `nullable: true` into a type array, renaming a nested `x-webhooks`, and so on).
|
|
49
|
+
*/
|
|
50
|
+
export function isInsideDataValue(path, sourceVersion) {
|
|
51
|
+
if (!path) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
for (const [index, segment] of path.entries()) {
|
|
55
|
+
// Response, parameter, and header names are arbitrary, just like schema names.
|
|
56
|
+
// Match their document locations so nested data keywords still protect payloads.
|
|
57
|
+
if (isOpenApiObjectName(path, index)) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
// Only treat it as data when the keyword is a real keyword. Inside a named-schema map the
|
|
61
|
+
// same word (e.g. a property literally called `default`) is just a member name.
|
|
62
|
+
// Swagger extensions must not classify OpenAPI 3.0 component or callback names as data.
|
|
63
|
+
const isDataKeyword = DATA_KEYWORDS.has(segment) || (sourceVersion === '2.0' && segment === 'x-example');
|
|
64
|
+
if (isDataKeyword && !isNamedSchemaMap(path.slice(0, index)) && !isDefaultResponse(path, index)) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
// Swagger 2.0 response examples (`responses/<code>/examples/<mime>`) map media types straight to data.
|
|
68
|
+
if (sourceVersion === '2.0' &&
|
|
69
|
+
segment === 'examples' &&
|
|
70
|
+
path[index - 2] === 'responses' &&
|
|
71
|
+
!isNamedSchemaMap(path.slice(0, index - 2))) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
// The `value` of an Example Object (`examples/<name>/value`) holds arbitrary data too.
|
|
75
|
+
// Require a real examples map two segments up so a property named `value` inside a schema
|
|
76
|
+
// named `examples` is not mistaken for it.
|
|
77
|
+
if (segment === 'value' && path[index - 2] === 'examples' && !isNamedSchemaMap(path.slice(0, index - 2))) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"traverse.d.ts","sourceRoot":"","sources":["../../src/helpers/traverse.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAExD;;GAEG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,aAAa,EACtB,SAAS,EAAE,CAAC,OAAO,EAAE,aAAa,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,aAAa,EACrE,IAAI,GAAE,MAAM,EAAO,
|
|
1
|
+
{"version":3,"file":"traverse.d.ts","sourceRoot":"","sources":["../../src/helpers/traverse.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAExD;;GAEG;AACH,wBAAgB,QAAQ,CACtB,OAAO,EAAE,aAAa,EACtB,SAAS,EAAE,CAAC,OAAO,EAAE,aAAa,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,aAAa,EACrE,IAAI,GAAE,MAAM,EAAO,iBA8BpB"}
|
package/dist/helpers/traverse.js
CHANGED
|
@@ -3,7 +3,8 @@ import { isObjectLike } from '@scalar/helpers/object/is-object';
|
|
|
3
3
|
* Recursively traverses the content and applies the transform function to each node.
|
|
4
4
|
*/
|
|
5
5
|
export function traverse(content, transform, path = []) {
|
|
6
|
-
|
|
6
|
+
// Define input keys as own properties before assignment, including literal __proto__ mapping keys.
|
|
7
|
+
const result = { ...content };
|
|
7
8
|
for (const [key, value] of Object.entries(content)) {
|
|
8
9
|
const currentPath = [...path, key];
|
|
9
10
|
if (Array.isArray(value)) {
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"openapi 3.0",
|
|
21
21
|
"openapi 3.1"
|
|
22
22
|
],
|
|
23
|
-
"version": "0.4.
|
|
23
|
+
"version": "0.4.8",
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": ">=22"
|
|
26
26
|
},
|
|
@@ -71,11 +71,11 @@
|
|
|
71
71
|
},
|
|
72
72
|
"sideEffects": false,
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@scalar/helpers": "0.
|
|
75
|
-
"@scalar/openapi-types": "0.9.
|
|
74
|
+
"@scalar/helpers": "0.17.1",
|
|
75
|
+
"@scalar/openapi-types": "0.9.9"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@scalar/types": "0.
|
|
78
|
+
"@scalar/types": "0.28.0",
|
|
79
79
|
"vite": "8.3.4"
|
|
80
80
|
},
|
|
81
81
|
"scripts": {
|
package/dist/upgrade.bench.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"upgrade.bench.d.ts","sourceRoot":"","sources":["../src/upgrade.bench.ts"],"names":[],"mappings":""}
|
package/dist/upgrade.bench.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { PETSTORE_URL_2_0, STRIPE_URL_3_0 } from '@scalar/helpers/url/oas-document-fixtures';
|
|
2
|
-
import { bench, describe, expect } from 'vitest';
|
|
3
|
-
import { upgradeFromTwoToThree } from './2.0-to-3.0/upgrade-from-two-to-three.js';
|
|
4
|
-
// import { upgrade as upgradeOld } from './slow/upgrade'
|
|
5
|
-
// import { upgradeFromTwoToThree as upgradeFromTwoToThreeOld } from './slow/upgradeFromTwoToThree'
|
|
6
|
-
import { upgrade } from './upgrade.js';
|
|
7
|
-
// Setup the test data
|
|
8
|
-
const STRIPE = await fetch(STRIPE_URL_3_0).then((r) => r.json());
|
|
9
|
-
const PETSTORE = await fetch(PETSTORE_URL_2_0).then((r) => r.json());
|
|
10
|
-
describe('upgrade', () => {
|
|
11
|
-
describe('Petstore: Swagger 2.0 to OpenAPI 3.0', () => {
|
|
12
|
-
bench('new', () => {
|
|
13
|
-
expect(PETSTORE.swagger).toBe('2.0');
|
|
14
|
-
const result = upgradeFromTwoToThree({ ...PETSTORE });
|
|
15
|
-
expect(result.openapi).toBe('3.0.4');
|
|
16
|
-
});
|
|
17
|
-
// bench('old', () => {
|
|
18
|
-
// expect(PETSTORE.swagger).toBe('2.0')
|
|
19
|
-
// const result = upgradeFromTwoToThreeOld({ ...PETSTORE })
|
|
20
|
-
// expect(result.openapi).toBe('3.0.4')
|
|
21
|
-
// })
|
|
22
|
-
});
|
|
23
|
-
describe('Stripe: OpenAPI 3.0 to 3.1', () => {
|
|
24
|
-
bench('new', () => {
|
|
25
|
-
expect(STRIPE.openapi).toBe('3.0.0');
|
|
26
|
-
const document = upgrade(STRIPE, '3.1');
|
|
27
|
-
expect(document?.openapi).toBe('3.1.1');
|
|
28
|
-
});
|
|
29
|
-
// bench('old', () => {
|
|
30
|
-
// expect(STRIPE.openapi).toBe('3.0.0')
|
|
31
|
-
// const document = upgradeOld(STRIPE)
|
|
32
|
-
// expect(document?.openapi).toBe('3.1.1')
|
|
33
|
-
// })
|
|
34
|
-
});
|
|
35
|
-
describe('Petstore: Swagger 2.0 to OpenAPI 3.1', () => {
|
|
36
|
-
bench('new', () => {
|
|
37
|
-
expect(PETSTORE.swagger).toBe('2.0');
|
|
38
|
-
const document = upgrade(PETSTORE, '3.1');
|
|
39
|
-
expect(document?.openapi).toBe('3.1.1');
|
|
40
|
-
});
|
|
41
|
-
// bench('old', () => {
|
|
42
|
-
// expect(PETSTORE.swagger).toBe('2.0')
|
|
43
|
-
// const document = upgradeOld(PETSTORE)
|
|
44
|
-
// expect(document?.openapi).toBe('3.1.1')
|
|
45
|
-
// })
|
|
46
|
-
});
|
|
47
|
-
});
|