@scalar/openapi-upgrader 0.5.0 → 0.5.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.
- package/CHANGELOG.md +10 -0
- package/dist/3.0-to-3.1/upgrade-from-three-to-three-one.d.ts +8 -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 +192 -49
- 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/upgrade-fragment.d.ts.map +1 -1
- package/dist/upgrade-fragment.js +11 -6
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @scalar/openapi-upgrader
|
|
2
2
|
|
|
3
|
+
## 0.5.2
|
|
4
|
+
|
|
5
|
+
No changes in this release.
|
|
6
|
+
|
|
7
|
+
## 0.5.1
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#11105](https://github.com/scalar/scalar/pull/11105): Finish migrating legacy `nullable` schema keywords when upgrading OpenAPI 3.0 descriptions, so downstream request validators can compile the upgraded schemas and enforce the rest of the request contract.
|
|
12
|
+
|
|
3
13
|
## 0.5.0
|
|
4
14
|
|
|
5
15
|
### Minor Changes
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { OpenAPIV3_1 } from '@scalar/openapi-types';
|
|
2
2
|
import type { UnknownObject } from '@scalar/types/utils';
|
|
3
|
+
type NullableComposition = {
|
|
4
|
+
base: unknown;
|
|
5
|
+
keywords: string[];
|
|
6
|
+
unwrapAllOf: boolean;
|
|
7
|
+
};
|
|
8
|
+
/** Plan nullable composition wrapping consistently for migrations and reference locations. */
|
|
9
|
+
export declare const getNullableComposition: (schema: UnknownObject) => NullableComposition | undefined;
|
|
3
10
|
/**
|
|
4
11
|
* Upgrade from OpenAPI 3.0.x to 3.1.1
|
|
5
12
|
*
|
|
@@ -20,4 +27,5 @@ export declare function upgradeFromThreeToThreeOne(originalContent: UnknownObjec
|
|
|
20
27
|
});
|
|
21
28
|
/** Apply one migration step to a node at its semantic OpenAPI location. */
|
|
22
29
|
export declare const applyChangesToDocument: (schema: UnknownObject, path?: string[]) => UnknownObject;
|
|
30
|
+
export {};
|
|
23
31
|
//# sourceMappingURL=upgrade-from-three-to-three-one.d.ts.map
|
|
@@ -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":"
|
|
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":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAoJxD,KAAK,mBAAmB,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,MAAM,EAAE,CAAC;IAAC,WAAW,EAAE,OAAO,CAAA;CAAE,CAAA;AAEtF,8FAA8F;AAC9F,eAAO,MAAM,sBAAsB,GAAI,QAAQ,aAAa,KAAG,mBAAmB,GAAG,SAgBpF,CAAA;AA4BD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,eAAe,EAAE,aAAa;;;;;;;;;;;;GAwCxE;AAED,2EAA2E;AAC3E,eAAO,MAAM,sBAAsB,GAAI,QAAQ,aAAa,EAAE,OAAO,MAAM,EAAE,kBA2G5E,CAAA"}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { escapeJsonPointer } from '@scalar/helpers/json/escape-json-pointer';
|
|
2
|
+
import { parseJsonPointerSegments } from '@scalar/helpers/json/parse-json-pointer-segments';
|
|
3
|
+
import { isObject } from '@scalar/helpers/object/is-object';
|
|
1
4
|
import { isSchemaPath } from '@scalar/helpers/openapi/is-schema-path';
|
|
2
|
-
import { isInsideDataValue, isNamedSchemaMap } from '../helpers/is-inside-data-value.js';
|
|
5
|
+
import { isInsideDataValue, isNamedOpenApiMap, isNamedSchemaMap, isOpenApiObjectName, } from '../helpers/is-inside-data-value.js';
|
|
3
6
|
import { traverse } from '../helpers/traverse.js';
|
|
4
7
|
// Keywords that only describe a schema. An arm made of these and `nullable` constrains nothing else.
|
|
5
8
|
const ANNOTATION_KEYWORDS = new Set([
|
|
@@ -13,21 +16,98 @@ const ANNOTATION_KEYWORDS = new Set([
|
|
|
13
16
|
'readOnly',
|
|
14
17
|
'writeOnly',
|
|
15
18
|
]);
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
/** Keep extension payloads opaque, including extensions outside Schema Objects. */
|
|
20
|
+
const isInsideExtension = (path) => {
|
|
21
|
+
const segments = path?.[0] === 'x-ext' && path[1] !== undefined ? path.slice(2) : path;
|
|
22
|
+
// This legacy root extension is migrated as a Webhooks Object below.
|
|
23
|
+
const objectPath = path?.[0] === 'x-webhooks' ? ['webhooks', ...path.slice(1)] : segments;
|
|
24
|
+
return (!!objectPath &&
|
|
25
|
+
objectPath.some((segment, index) => segment.startsWith('x-') &&
|
|
26
|
+
!isNamedSchemaMap(objectPath.slice(0, index)) &&
|
|
27
|
+
!isOpenApiObjectName(objectPath, index)));
|
|
28
|
+
};
|
|
29
|
+
/** Keep schema keywords separate from author-chosen names. */
|
|
30
|
+
const isSchemaObjectPath = (path) => !!path && isSchemaPath(path) && !isNamedSchemaMap(path) && !isInsideExtension(path);
|
|
31
|
+
/** Interpret referenced fragments at their consuming location and leave unrecognized extension data opaque. */
|
|
32
|
+
const getResourcePath = (path, bundle) => {
|
|
33
|
+
if (path?.[0] !== 'x-ext') {
|
|
34
|
+
return path;
|
|
35
|
+
}
|
|
36
|
+
for (let length = path.length; length >= 1; length--) {
|
|
37
|
+
const context = bundle.fragments.get(JSON.stringify(path.slice(0, length)));
|
|
38
|
+
if (context) {
|
|
39
|
+
return [...context, ...path.slice(length)];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
};
|
|
44
|
+
/** Decode the URI fragment before parsing pointer tokens, without decoding literal percent signs twice. */
|
|
45
|
+
const parseLocalReference = (ref) => {
|
|
46
|
+
if (!ref.startsWith('#')) {
|
|
47
|
+
return undefined;
|
|
25
48
|
}
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
return
|
|
49
|
+
try {
|
|
50
|
+
const pointer = decodeURIComponent(ref.slice(1));
|
|
51
|
+
return pointer.startsWith('/') ? parseJsonPointerSegments(pointer.replaceAll('%', '%25')) : undefined;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// Invalid URI escapes must not make an otherwise unrelated migration fail.
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/** Establish fragment contexts from real references, never from an extension's field names. */
|
|
59
|
+
const getBundleContext = (content) => {
|
|
60
|
+
const preserved = new Set();
|
|
61
|
+
const resources = content['x-ext'];
|
|
62
|
+
if (isObject(resources)) {
|
|
63
|
+
for (const [key, resource] of Object.entries(resources)) {
|
|
64
|
+
if (!isObject(resource)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if ((typeof resource.openapi === 'string' && !resource.openapi.startsWith('3.0')) ||
|
|
68
|
+
typeof resource.swagger === 'string' ||
|
|
69
|
+
typeof resource.$schema === 'string') {
|
|
70
|
+
preserved.add(key);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const fragments = new Map();
|
|
75
|
+
const bundle = { preserved, fragments };
|
|
76
|
+
const references = [];
|
|
77
|
+
traverse(content, (node, path = []) => {
|
|
78
|
+
if (path[0] === 'x-ext' && preserved.has(path[1] ?? '')) {
|
|
79
|
+
return node;
|
|
80
|
+
}
|
|
81
|
+
const target = typeof node.$ref === 'string' ? parseLocalReference(node.$ref) : undefined;
|
|
82
|
+
if (target?.[0] === 'x-ext') {
|
|
83
|
+
references.push({ path, target });
|
|
84
|
+
}
|
|
85
|
+
return node;
|
|
29
86
|
});
|
|
30
|
-
|
|
87
|
+
// A referenced fragment can itself contain references to more fragments.
|
|
88
|
+
while (references.length > 0) {
|
|
89
|
+
const pending = references.splice(0);
|
|
90
|
+
for (const reference of pending) {
|
|
91
|
+
const path = getResourcePath(reference.path, bundle);
|
|
92
|
+
if (path && !isInsideDataValue(path, '3.0') && !isInsideExtension(path)) {
|
|
93
|
+
const key = JSON.stringify(reference.target);
|
|
94
|
+
if (!fragments.has(key)) {
|
|
95
|
+
fragments.set(key, isSchemaObjectPath(path) ? ['schema'] : path);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
references.push(reference);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (references.length === pending.length) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return bundle;
|
|
107
|
+
};
|
|
108
|
+
/** Determine if the node at `path` is one arm of a schema's `oneOf` or `anyOf`. */
|
|
109
|
+
function isUnionArm(path) {
|
|
110
|
+
if (!path || path.length < 2 || !isSchemaObjectPath(path.slice(0, -2))) {
|
|
31
111
|
return false;
|
|
32
112
|
}
|
|
33
113
|
const keyword = path[path.length - 2];
|
|
@@ -40,6 +120,51 @@ function isUnionArm(path) {
|
|
|
40
120
|
function isNullableOnly(schema) {
|
|
41
121
|
return Object.keys(schema).every((key) => key === 'nullable' || ANNOTATION_KEYWORDS.has(key) || key.startsWith('x-'));
|
|
42
122
|
}
|
|
123
|
+
/** Plan nullable composition wrapping consistently for migrations and reference locations. */
|
|
124
|
+
export const getNullableComposition = (schema) => {
|
|
125
|
+
if (schema.nullable !== true || schema.type !== undefined || typeof schema.$ref === 'string') {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const keywords = ['allOf', 'anyOf', 'oneOf'].filter((keyword) => Array.isArray(schema[keyword]));
|
|
129
|
+
if (keywords.length === 0) {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
const { allOf, anyOf, oneOf } = schema;
|
|
133
|
+
const composition = {
|
|
134
|
+
...(allOf === undefined ? {} : { allOf }),
|
|
135
|
+
...(anyOf === undefined ? {} : { anyOf }),
|
|
136
|
+
...(oneOf === undefined ? {} : { oneOf }),
|
|
137
|
+
};
|
|
138
|
+
const unwrapAllOf = Object.keys(composition).length === 1 && Array.isArray(allOf) && allOf.length === 1;
|
|
139
|
+
return { base: unwrapAllOf && Array.isArray(allOf) ? allOf[0] : composition, keywords, unwrapAllOf };
|
|
140
|
+
};
|
|
141
|
+
/** Keep local references pointing at the same subschema when composition members move. */
|
|
142
|
+
const relocateReference = (ref, compositions) => {
|
|
143
|
+
const segments = parseLocalReference(ref);
|
|
144
|
+
if (!segments) {
|
|
145
|
+
return ref;
|
|
146
|
+
}
|
|
147
|
+
const output = [];
|
|
148
|
+
for (let index = 0; index < segments.length; index++) {
|
|
149
|
+
const keyword = segments[index];
|
|
150
|
+
const composition = compositions.get(JSON.stringify(segments.slice(0, index)));
|
|
151
|
+
if (composition?.keywords.includes(keyword)) {
|
|
152
|
+
output.push('anyOf', '0');
|
|
153
|
+
if (keyword === 'allOf' && composition.unwrapAllOf && segments[index + 1] === '0') {
|
|
154
|
+
index++;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
output.push(keyword);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
output.push(keyword);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return output.every((segment, index) => segment === segments[index]) && output.length === segments.length
|
|
165
|
+
? ref
|
|
166
|
+
: `#/${output.map((segment) => encodeURIComponent(escapeJsonPointer(segment))).join('/')}`;
|
|
167
|
+
};
|
|
43
168
|
/**
|
|
44
169
|
* Upgrade from OpenAPI 3.0.x to 3.1.1
|
|
45
170
|
*
|
|
@@ -52,47 +177,65 @@ export function upgradeFromThreeToThreeOne(originalContent) {
|
|
|
52
177
|
return content;
|
|
53
178
|
}
|
|
54
179
|
content.openapi = '3.1.1';
|
|
55
|
-
|
|
56
|
-
|
|
180
|
+
const bundle = getBundleContext(content);
|
|
181
|
+
const isPreservedResource = (path) => path?.[0] === 'x-ext' && bundle.preserved.has(path[1] ?? '');
|
|
182
|
+
const compositions = new Map();
|
|
183
|
+
// Record moves in the original tree before applying them, including nested compositions.
|
|
184
|
+
traverse(content, (node, path = []) => {
|
|
185
|
+
const schemaPath = getResourcePath(path, bundle);
|
|
186
|
+
const composition = getNullableComposition(node);
|
|
187
|
+
if (!isPreservedResource(path) &&
|
|
188
|
+
composition &&
|
|
189
|
+
isSchemaObjectPath(schemaPath) &&
|
|
190
|
+
!isInsideDataValue(schemaPath, '3.0')) {
|
|
191
|
+
compositions.set(JSON.stringify(path), composition);
|
|
192
|
+
}
|
|
193
|
+
return node;
|
|
194
|
+
});
|
|
195
|
+
content = traverse(content, (node, path) => {
|
|
196
|
+
const schemaPath = getResourcePath(path, bundle);
|
|
197
|
+
if (isPreservedResource(path) || !schemaPath) {
|
|
198
|
+
return node;
|
|
199
|
+
}
|
|
200
|
+
if (typeof node.$ref === 'string' && !isInsideDataValue(schemaPath, '3.0') && !isInsideExtension(schemaPath)) {
|
|
201
|
+
node.$ref = relocateReference(node.$ref, compositions);
|
|
202
|
+
}
|
|
203
|
+
return applyChangesToDocument(node, schemaPath);
|
|
204
|
+
});
|
|
57
205
|
return content;
|
|
58
206
|
}
|
|
59
207
|
/** Apply one migration step to a node at its semantic OpenAPI location. */
|
|
60
208
|
export const applyChangesToDocument = (schema, path) => {
|
|
61
|
-
//
|
|
62
|
-
|
|
63
|
-
if (isInsideDataValue(path, '3.0')) {
|
|
209
|
+
// Data values and extension payloads are opaque, even when their keys look like schema keywords.
|
|
210
|
+
if (isInsideDataValue(path, '3.0') || isInsideExtension(path)) {
|
|
64
211
|
return schema;
|
|
65
212
|
}
|
|
66
|
-
//
|
|
67
|
-
if (
|
|
68
|
-
|
|
213
|
+
// Only schema keywords migrate: a property named `nullable` or an extension payload is data.
|
|
214
|
+
if (isSchemaObjectPath(path)) {
|
|
215
|
+
const composition = getNullableComposition(schema);
|
|
216
|
+
if (schema.type !== undefined && schema.nullable === true) {
|
|
217
|
+
schema.type = [schema.type, 'null'];
|
|
218
|
+
}
|
|
219
|
+
else if (schema.nullable === true) {
|
|
220
|
+
// Preserve the legacy nullable-reference spelling used by OpenAPI 3.0 exporters.
|
|
221
|
+
if (typeof schema.$ref === 'string') {
|
|
222
|
+
schema.anyOf = [{ $ref: schema.$ref }, { type: 'null' }];
|
|
223
|
+
delete schema.$ref;
|
|
224
|
+
}
|
|
225
|
+
else if (composition) {
|
|
226
|
+
// Keep sibling constraints and annotations outside the union, as for nullable references.
|
|
227
|
+
schema.anyOf = [composition.base, { type: 'null' }];
|
|
228
|
+
delete schema.allOf;
|
|
229
|
+
delete schema.oneOf;
|
|
230
|
+
}
|
|
231
|
+
else if (isUnionArm(path) && isNullableOnly(schema)) {
|
|
232
|
+
// Some exporters use a nullable-only alternative to mean "or null".
|
|
233
|
+
schema.type = 'null';
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
// `nullable: false` and an untyped nullable without a union add no constraints in 3.1.
|
|
69
237
|
delete schema.nullable;
|
|
70
238
|
}
|
|
71
|
-
// 1b. Handle nullable `$ref` patterns.
|
|
72
|
-
// OpenAPI 3.0 had no way to mark a `$ref` as nullable, so people wrote `nullable: true` next to
|
|
73
|
-
// a `$ref`, or next to an `allOf` wrapping one. `nullable` does not exist in 3.1, so leaving these
|
|
74
|
-
// untouched would silently drop the null option. Rewrite them to the 3.1 null-union form. This only
|
|
75
|
-
// runs when there is no sibling `type` (that case is handled above).
|
|
76
|
-
else if (schema.nullable === true && schema.type === undefined) {
|
|
77
|
-
if (typeof schema.$ref === 'string') {
|
|
78
|
-
const { nullable: _nullable, $ref, ...rest } = schema;
|
|
79
|
-
return { ...rest, anyOf: [{ $ref }, { type: 'null' }] };
|
|
80
|
-
}
|
|
81
|
-
if (Array.isArray(schema.allOf)) {
|
|
82
|
-
const { nullable: _nullable, allOf, ...rest } = schema;
|
|
83
|
-
// A single-member `allOf` unwraps so the union reads `anyOf: [<member>, { type: 'null' }]`.
|
|
84
|
-
const base = allOf.length === 1 ? allOf[0] : { allOf };
|
|
85
|
-
return { ...rest, anyOf: [base, { type: 'null' }] };
|
|
86
|
-
}
|
|
87
|
-
// A union arm that says nothing but `nullable: true` is how some 3.0 exporters spell "or null":
|
|
88
|
-
// `oneOf: [{ nullable: true }, { $ref: X }]`. Left alone, the arm reads as "any value" in 3.1 and
|
|
89
|
-
// swallows the real arm, so it becomes the `{ type: 'null' }` arm it was meant to be.
|
|
90
|
-
if (isUnionArm(path) && isNullableOnly(schema)) {
|
|
91
|
-
delete schema.nullable;
|
|
92
|
-
schema.type = 'null';
|
|
93
|
-
}
|
|
94
|
-
// Otherwise there is nothing for `nullable` to attach to, so leave the schema untouched.
|
|
95
|
-
}
|
|
96
239
|
// 2. Handle exclusiveMinimum and exclusiveMaximum
|
|
97
240
|
if (schema.exclusiveMinimum === true && schema.minimum !== undefined) {
|
|
98
241
|
schema.exclusiveMinimum = schema.minimum;
|
|
@@ -115,13 +258,13 @@ export const applyChangesToDocument = (schema, path) => {
|
|
|
115
258
|
const isInsideExamplesMap = path?.some((segment, index) => {
|
|
116
259
|
if (segment === 'examples' && index > 0) {
|
|
117
260
|
// If the parent node is a named-schema map, 'examples' is a member name, not an examples map
|
|
118
|
-
return !isNamedSchemaMap(path.slice(0, index));
|
|
261
|
+
return !isNamedSchemaMap(path.slice(0, index)) && !isNamedOpenApiMap(path.slice(0, index));
|
|
119
262
|
}
|
|
120
263
|
return false;
|
|
121
264
|
});
|
|
122
265
|
// If the current node is itself a map of named subschemas, a key called 'example' is a
|
|
123
266
|
// member name (property, $defs entry, components/schemas entry) and not the schema keyword.
|
|
124
|
-
if (schema.example !== undefined && !isInsideExamplesMap && !isNamedSchemaMap(path)) {
|
|
267
|
+
if (schema.example !== undefined && !isInsideExamplesMap && !isNamedSchemaMap(path) && !isNamedOpenApiMap(path)) {
|
|
125
268
|
if (isSchemaPath(path)) {
|
|
126
269
|
schema.examples = [schema.example];
|
|
127
270
|
}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** Identify maps whose keys name OpenAPI objects rather than schema/data keywords. */
|
|
2
|
+
export declare const isNamedOpenApiMap: (path: string[] | undefined) => boolean;
|
|
3
|
+
/** Distinguish author-chosen OpenAPI names from keywords and Specification Extensions. */
|
|
4
|
+
export declare const isOpenApiObjectName: (path: readonly string[], index: number) => boolean;
|
|
1
5
|
/**
|
|
2
6
|
* Determine if the node at `path` is a map of named subschemas rather than a schema object.
|
|
3
7
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-inside-data-value.d.ts","sourceRoot":"","sources":["../../src/helpers/is-inside-data-value.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"is-inside-data-value.d.ts","sourceRoot":"","sources":["../../src/helpers/is-inside-data-value.ts"],"names":[],"mappings":"AA2CA,sFAAsF;AACtF,eAAO,MAAM,iBAAiB,GAAI,MAAM,MAAM,EAAE,GAAG,SAAS,KAAG,OAAgD,CAAA;AAE/G,0FAA0F;AAC1F,eAAO,MAAM,mBAAmB,GAAI,MAAM,SAAS,MAAM,EAAE,EAAE,OAAO,MAAM,KAAG,OAgB5E,CAAA;AAED;;;;;;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,CAwCnG"}
|
|
@@ -1,13 +1,64 @@
|
|
|
1
|
+
import { isSchemaPath } from '@scalar/helpers/openapi/is-schema-path';
|
|
1
2
|
// Segments whose value is a map of *named* subschemas. Keys inside these maps are
|
|
2
3
|
// arbitrary names (property names, definition names), never schema keywords.
|
|
3
4
|
const NAMED_SCHEMA_MAP_SEGMENTS = new Set(['properties', 'patternProperties', '$defs', 'definitions']);
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const OPENAPI_NAME_MAPS = new Set([
|
|
6
|
+
'paths',
|
|
7
|
+
'webhooks',
|
|
8
|
+
'responses',
|
|
9
|
+
'content',
|
|
10
|
+
'headers',
|
|
11
|
+
'examples',
|
|
12
|
+
'links',
|
|
13
|
+
'encoding',
|
|
14
|
+
'variables',
|
|
15
|
+
'parameters',
|
|
16
|
+
'requestBodies',
|
|
17
|
+
'securitySchemes',
|
|
18
|
+
'pathItems',
|
|
19
|
+
'scopes',
|
|
20
|
+
'schemas',
|
|
21
|
+
]);
|
|
22
|
+
/** Count author-chosen key levels before entering a Schema Object. */
|
|
23
|
+
const openApiNameLevels = (path) => {
|
|
24
|
+
const segments = path[0] === 'x-ext' && path[1] !== undefined ? path.slice(2) : path;
|
|
25
|
+
let levels = 0;
|
|
26
|
+
for (const [index, segment] of segments.entries()) {
|
|
27
|
+
if (levels > 0) {
|
|
28
|
+
levels--;
|
|
29
|
+
}
|
|
30
|
+
else if (isSchemaPath(segments.slice(0, index))) {
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
else if (segment === 'callbacks') {
|
|
34
|
+
// A callback map has a callback name, followed by a runtime-expression map.
|
|
35
|
+
levels = 2;
|
|
36
|
+
}
|
|
37
|
+
else if (OPENAPI_NAME_MAPS.has(segment) || (index === 0 && segment === 'x-webhooks')) {
|
|
38
|
+
levels = 1;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return levels;
|
|
42
|
+
};
|
|
43
|
+
/** Identify maps whose keys name OpenAPI objects rather than schema/data keywords. */
|
|
44
|
+
export const isNamedOpenApiMap = (path) => !!path && openApiNameLevels(path) > 0;
|
|
45
|
+
/** Distinguish author-chosen OpenAPI names from keywords and Specification Extensions. */
|
|
46
|
+
export const isOpenApiObjectName = (path, index) => {
|
|
47
|
+
const parent = path.slice(0, index);
|
|
48
|
+
const levels = openApiNameLevels(parent);
|
|
49
|
+
if (levels === 0) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if (!path[index]?.startsWith('x-')) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
const resourceParent = parent[0] === 'x-ext' && parent[1] !== undefined ? parent.slice(2) : parent;
|
|
56
|
+
const isPaths = resourceParent.length === 1 && resourceParent[0] === 'paths';
|
|
57
|
+
const isResponses = resourceParent.at(-1) === 'responses' &&
|
|
58
|
+
!(resourceParent.length === 1 || (resourceParent.length === 2 && resourceParent[0] === 'components'));
|
|
59
|
+
const isCallbackExpression = levels === 1 && openApiNameLevels(parent.slice(0, -1)) === 2;
|
|
60
|
+
return !isPaths && !isResponses && !isCallbackExpression;
|
|
61
|
+
};
|
|
11
62
|
/**
|
|
12
63
|
* Determine if the node at `path` is a map of named subschemas rather than a schema object.
|
|
13
64
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upgrade-fragment.d.ts","sourceRoot":"","sources":["../src/upgrade-fragment.ts"],"names":[],"mappings":"AAkBA,mFAAmF;AACnF,MAAM,MAAM,YAAY,GACpB,UAAU,GACV,QAAQ,GACR,WAAW,GACX,QAAQ,GACR,UAAU,GACV,WAAW,GACX,WAAW,GACX,cAAc,GACd,UAAU,CAAA;AAEd;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAAE,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,CAAA;AAEvF,2EAA2E;AAC3E,MAAM,MAAM,sBAAsB,GAAG;IACnC,IAAI,EAAE,KAAK,GAAG,KAAK,CAAA;IACnB,EAAE,EAAE,KAAK,CAAA;IACT,IAAI,EAAE,YAAY,CAAA;IAClB,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC5B,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC5B;;;;OAIG;IACH,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CAClD,CAAA;AAED,yEAAyE;AACzE,MAAM,MAAM,qBAAqB,GAAG;IAClC,KAAK,EAAE,OAAO,CAAA;IACd,gGAAgG;IAChG,SAAS,EAAE,gBAAgB,EAAE,CAAA;IAC7B,0FAA0F;IAC1F,QAAQ,EAAE;QAAE,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;QAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;QAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,EAAE,CAAA;CAClG,CAAA;AAsCD;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAC1B,OAAO,OAAO,EACd,SAAS,sBAAsB,KAC9B,OAAO,CAAC,qBAAqB,
|
|
1
|
+
{"version":3,"file":"upgrade-fragment.d.ts","sourceRoot":"","sources":["../src/upgrade-fragment.ts"],"names":[],"mappings":"AAkBA,mFAAmF;AACnF,MAAM,MAAM,YAAY,GACpB,UAAU,GACV,QAAQ,GACR,WAAW,GACX,QAAQ,GACR,UAAU,GACV,WAAW,GACX,WAAW,GACX,cAAc,GACd,UAAU,CAAA;AAEd;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAAE,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,CAAA;AAEvF,2EAA2E;AAC3E,MAAM,MAAM,sBAAsB,GAAG;IACnC,IAAI,EAAE,KAAK,GAAG,KAAK,CAAA;IACnB,EAAE,EAAE,KAAK,CAAA;IACT,IAAI,EAAE,YAAY,CAAA;IAClB,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC5B,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;IAC5B;;;;OAIG;IACH,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CAClD,CAAA;AAED,yEAAyE;AACzE,MAAM,MAAM,qBAAqB,GAAG;IAClC,KAAK,EAAE,OAAO,CAAA;IACd,gGAAgG;IAChG,SAAS,EAAE,gBAAgB,EAAE,CAAA;IAC7B,0FAA0F;IAC1F,QAAQ,EAAE;QAAE,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;QAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;QAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,EAAE,CAAA;CAClG,CAAA;AAsCD;;;;;GAKG;AACH,eAAO,MAAM,eAAe,GAC1B,OAAO,OAAO,EACd,SAAS,sBAAsB,KAC9B,OAAO,CAAC,qBAAqB,CAmd/B,CAAA"}
|
package/dist/upgrade-fragment.js
CHANGED
|
@@ -2,7 +2,7 @@ import { HTTP_METHODS } from '@scalar/helpers/http/http-methods';
|
|
|
2
2
|
import { isObject } from '@scalar/helpers/object/is-object';
|
|
3
3
|
import { migrateDiscriminatorMappings } from './2.0-to-3.0/migrate-discriminator-mappings.js';
|
|
4
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 } from './3.0-to-3.1/upgrade-from-three-to-three-one.js';
|
|
5
|
+
import { applyChangesToDocument, getNullableComposition } from './3.0-to-3.1/upgrade-from-three-to-three-one.js';
|
|
6
6
|
const schemaMaps = new Set(['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas']);
|
|
7
7
|
const schemaChildren = new Set([
|
|
8
8
|
'items',
|
|
@@ -339,9 +339,9 @@ export const upgradeFragment = async (input, options) => {
|
|
|
339
339
|
result.format = 'binary';
|
|
340
340
|
}
|
|
341
341
|
}
|
|
342
|
-
//
|
|
343
|
-
//
|
|
344
|
-
const
|
|
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
345
|
for (const [key, entry] of Object.entries(result)) {
|
|
346
346
|
if (schemaMaps.has(key)) {
|
|
347
347
|
result[key] = await map(entry, 'schema', [key]);
|
|
@@ -351,8 +351,13 @@ export const upgradeFragment = async (input, options) => {
|
|
|
351
351
|
result[key] = await Promise.all(entry.map((item, index) => visit(item, {
|
|
352
352
|
kind: 'schema',
|
|
353
353
|
source: [...source, key, String(index)],
|
|
354
|
-
output: key
|
|
355
|
-
? [
|
|
354
|
+
output: composition?.keywords.includes(key)
|
|
355
|
+
? [
|
|
356
|
+
...output,
|
|
357
|
+
'anyOf',
|
|
358
|
+
'0',
|
|
359
|
+
...(key === 'allOf' && composition.unwrapAllOf ? [] : [key, String(index)]),
|
|
360
|
+
]
|
|
356
361
|
: [...output, key, String(index)],
|
|
357
362
|
consumes,
|
|
358
363
|
produces,
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"openapi 3.0",
|
|
21
21
|
"openapi 3.1"
|
|
22
22
|
],
|
|
23
|
-
"version": "0.5.
|
|
23
|
+
"version": "0.5.2",
|
|
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.
|
|
74
|
+
"@scalar/helpers": "0.20.0",
|
|
75
75
|
"@scalar/openapi-types": "0.9.10"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@scalar/types": "0.
|
|
78
|
+
"@scalar/types": "0.32.1",
|
|
79
79
|
"vite": "8.3.8"
|
|
80
80
|
},
|
|
81
81
|
"scripts": {
|