@scalar/openapi-upgrader 0.4.4 → 0.4.6
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 +15 -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 +8 -1
- 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 +57 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @scalar/openapi-upgrader
|
|
2
2
|
|
|
3
|
+
## 0.4.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#10701](https://github.com/scalar/scalar/pull/10701): Preserve descriptions and specification extensions on OAuth2 and Basic security schemes, and preserve extensions on form-data fields when upgrading OpenAPI 2.0 API descriptions.
|
|
8
|
+
|
|
9
|
+
## 0.4.5
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#10613](https://github.com/scalar/scalar/pull/10613): The 3.0 to 3.1 upgrade no longer skips the `default` entry of a Responses Object, which it mistook for the `default` keyword, so schemas in a default response are upgraded too.
|
|
14
|
+
- [#10613](https://github.com/scalar/scalar/pull/10613): Upgrade a `oneOf`/`anyOf` arm that only says `nullable: true` (optionally with annotations, including `default`) to `{ type: 'null' }`, so `oneOf: [{ nullable: true }, { $ref: X }]` becomes "X or null" in OpenAPI 3.1 instead of an arm that accepts any value.
|
|
15
|
+
|
|
16
|
+
Preserve nested extension payloads when upgrading nullable-only union arms.
|
|
17
|
+
|
|
3
18
|
## 0.4.4
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
|
@@ -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;AAgMxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,
|
|
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;AAgMxD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,qBAAqB,EAAE,aAAa,0CAwczE"}
|
|
@@ -476,6 +476,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
476
476
|
}
|
|
477
477
|
for (const [key, securityScheme] of Object.entries(document.securityDefinitions)) {
|
|
478
478
|
if (typeof securityScheme === 'object') {
|
|
479
|
+
const metadata = Object.fromEntries(Object.entries(securityScheme).filter(([key]) => key === 'description' || key.startsWith('x-')));
|
|
479
480
|
if ('type' in securityScheme && securityScheme.type === 'oauth2') {
|
|
480
481
|
const { flow, authorizationUrl, tokenUrl, scopes } = securityScheme;
|
|
481
482
|
// Convert flow values to OpenAPI 3.1.0 format
|
|
@@ -485,6 +486,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
485
486
|
document.components.securitySchemes) {
|
|
486
487
|
Object.assign(document.components.securitySchemes, {
|
|
487
488
|
[key]: {
|
|
489
|
+
...metadata,
|
|
488
490
|
type: 'oauth2',
|
|
489
491
|
flows: {
|
|
490
492
|
[upgradeFlow(flow || 'implicit')]: Object.assign({}, authorizationUrl && { authorizationUrl }, tokenUrl && { tokenUrl }, scopes && { scopes }),
|
|
@@ -500,6 +502,7 @@ export function upgradeFromTwoToThree(originalSpecification) {
|
|
|
500
502
|
document.components.securitySchemes) {
|
|
501
503
|
Object.assign(document.components.securitySchemes, {
|
|
502
504
|
[key]: {
|
|
505
|
+
...metadata,
|
|
503
506
|
type: 'http',
|
|
504
507
|
scheme: 'basic',
|
|
505
508
|
},
|
|
@@ -798,8 +801,12 @@ function migrateFormDataParameter(parameters, consumes = ['multipart/form-data']
|
|
|
798
801
|
if (formContent?.schema && typeof formContent.schema === 'object' && 'properties' in formContent.schema) {
|
|
799
802
|
for (const param of parameters) {
|
|
800
803
|
if (param.name && formContent.schema.properties) {
|
|
804
|
+
const remainingProperties = structuredClone(param);
|
|
805
|
+
const schema = transformItemsObject(remainingProperties);
|
|
801
806
|
formContent.schema.properties[param.name] = {
|
|
802
|
-
...
|
|
807
|
+
...schema,
|
|
808
|
+
// Keep field extensions after consuming recognized schema keywords such as x-nullable.
|
|
809
|
+
...Object.fromEntries(Object.entries(remainingProperties).filter(([key]) => key.startsWith('x-'))),
|
|
803
810
|
// Only carry the description over when it exists, so a parameter without one does not
|
|
804
811
|
// end up with an explicit `description: undefined` on its schema.
|
|
805
812
|
...(param.description !== undefined ? { description: param.description } : {}),
|
|
@@ -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;AAyHxD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,eAAe,EAAE,aAAa;;;;;;;;;;;;GAcxE"}
|
|
@@ -21,6 +21,16 @@ function isNamedSchemaMap(path) {
|
|
|
21
21
|
}
|
|
22
22
|
return NAMED_SCHEMA_MAP_SEGMENTS.has(last);
|
|
23
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
|
+
}
|
|
24
34
|
// Keywords whose value is arbitrary data rather than a schema. The upgrade rules only make
|
|
25
35
|
// sense for schema keywords, so nothing nested under these may be rewritten.
|
|
26
36
|
const DATA_KEYWORDS = new Set(['example', 'default', 'const', 'enum']);
|
|
@@ -38,7 +48,7 @@ function isInsideDataValue(path) {
|
|
|
38
48
|
for (const [index, segment] of path.entries()) {
|
|
39
49
|
// Only treat it as data when the keyword is a real keyword. Inside a named-schema map the
|
|
40
50
|
// same word (e.g. a property literally called `default`) is just a member name.
|
|
41
|
-
if (DATA_KEYWORDS.has(segment) && !isNamedSchemaMap(path.slice(0, index))) {
|
|
51
|
+
if (DATA_KEYWORDS.has(segment) && !isNamedSchemaMap(path.slice(0, index)) && !isDefaultResponse(path, index)) {
|
|
42
52
|
return true;
|
|
43
53
|
}
|
|
44
54
|
// The `value` of an Example Object (`examples/<name>/value`) holds arbitrary data too.
|
|
@@ -50,6 +60,45 @@ function isInsideDataValue(path) {
|
|
|
50
60
|
}
|
|
51
61
|
return false;
|
|
52
62
|
}
|
|
63
|
+
// Keywords that only describe a schema. An arm made of these and `nullable` constrains nothing else.
|
|
64
|
+
const ANNOTATION_KEYWORDS = new Set([
|
|
65
|
+
'title',
|
|
66
|
+
'description',
|
|
67
|
+
'deprecated',
|
|
68
|
+
'default',
|
|
69
|
+
'example',
|
|
70
|
+
'examples',
|
|
71
|
+
'externalDocs',
|
|
72
|
+
'readOnly',
|
|
73
|
+
'writeOnly',
|
|
74
|
+
]);
|
|
75
|
+
/**
|
|
76
|
+
* Determine if the node at `path` is one arm of a `oneOf` or `anyOf`.
|
|
77
|
+
*
|
|
78
|
+
* Extension payloads can contain objects that look like union arms too. Only interpret unions
|
|
79
|
+
* in schema context, keeping extension values opaque and allowing schema names prefixed by `x-`.
|
|
80
|
+
*/
|
|
81
|
+
function isUnionArm(path) {
|
|
82
|
+
if (!path || path.length < 2 || !isSchemaPath(path.slice(0, -2))) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
const isInsideSchemaExtension = path.some((segment, index) => {
|
|
86
|
+
const parent = path.slice(0, index);
|
|
87
|
+
return segment.startsWith('x-') && isSchemaPath(parent) && !isNamedSchemaMap(parent);
|
|
88
|
+
});
|
|
89
|
+
if (isInsideSchemaExtension) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
const keyword = path[path.length - 2];
|
|
93
|
+
const index = path[path.length - 1];
|
|
94
|
+
return (keyword === 'oneOf' || keyword === 'anyOf') && index !== undefined && /^\d+$/.test(index);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Determine if a schema carries `nullable: true` and nothing else but annotations and extensions.
|
|
98
|
+
*/
|
|
99
|
+
function isNullableOnly(schema) {
|
|
100
|
+
return Object.keys(schema).every((key) => key === 'nullable' || ANNOTATION_KEYWORDS.has(key) || key.startsWith('x-'));
|
|
101
|
+
}
|
|
53
102
|
/**
|
|
54
103
|
* Upgrade from OpenAPI 3.0.x to 3.1.1
|
|
55
104
|
*
|
|
@@ -93,6 +142,13 @@ const applyChangesToDocument = (schema, path) => {
|
|
|
93
142
|
const base = allOf.length === 1 ? allOf[0] : { allOf };
|
|
94
143
|
return { ...rest, anyOf: [base, { type: 'null' }] };
|
|
95
144
|
}
|
|
145
|
+
// A union arm that says nothing but `nullable: true` is how some 3.0 exporters spell "or null":
|
|
146
|
+
// `oneOf: [{ nullable: true }, { $ref: X }]`. Left alone, the arm reads as "any value" in 3.1 and
|
|
147
|
+
// swallows the real arm, so it becomes the `{ type: 'null' }` arm it was meant to be.
|
|
148
|
+
if (isUnionArm(path) && isNullableOnly(schema)) {
|
|
149
|
+
delete schema.nullable;
|
|
150
|
+
schema.type = 'null';
|
|
151
|
+
}
|
|
96
152
|
// Otherwise there is nothing for `nullable` to attach to, so leave the schema untouched.
|
|
97
153
|
}
|
|
98
154
|
// 2. Handle exclusiveMinimum and exclusiveMaximum
|
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.6",
|
|
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.16.
|
|
74
|
+
"@scalar/helpers": "0.16.4",
|
|
75
75
|
"@scalar/openapi-types": "0.9.8"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@scalar/types": "0.
|
|
79
|
-
"vite": "8.
|
|
78
|
+
"@scalar/types": "0.25.0",
|
|
79
|
+
"vite": "8.3.4"
|
|
80
80
|
},
|
|
81
81
|
"scripts": {
|
|
82
82
|
"build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|