@scalar/openapi-parser 0.10.2 → 0.10.4
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
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @scalar/openapi-parser
|
|
2
2
|
|
|
3
|
+
## 0.10.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 64df4fc: fix: example arrays are transformed to objects
|
|
8
|
+
|
|
9
|
+
## 0.10.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 8dce84f: Call `onDereference` after dereference process
|
|
14
|
+
|
|
3
15
|
## 0.10.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -6,6 +6,11 @@ export type ResolveReferencesResult = {
|
|
|
6
6
|
schema: OpenAPI.Document;
|
|
7
7
|
};
|
|
8
8
|
export type ResolveReferencesOptions = ThrowOnErrorOption & {
|
|
9
|
+
/**
|
|
10
|
+
* Fired when dereferenced a schema.
|
|
11
|
+
*
|
|
12
|
+
* Note that for object schemas, its properties may not be dereferenced when the hook is called.
|
|
13
|
+
*/
|
|
9
14
|
onDereference?: (data: {
|
|
10
15
|
schema: AnyObject;
|
|
11
16
|
ref: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveReferences.d.ts","sourceRoot":"","sources":["../../src/utils/resolveReferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAGpD,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,UAAU,EACV,eAAe,EACf,kBAAkB,EACnB,MAAM,UAAU,CAAA;AAejB,MAAM,MAAM,uBAAuB,GAAG;IACpC,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,EAAE,WAAW,EAAE,CAAA;IACrB,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,wBAAwB,GAAG,kBAAkB,GAAG;IAC1D,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,MAAM,EAAE,SAAS,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAA;CACnE,CAAA;
|
|
1
|
+
{"version":3,"file":"resolveReferences.d.ts","sourceRoot":"","sources":["../../src/utils/resolveReferences.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAGpD,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,UAAU,EACV,eAAe,EACf,kBAAkB,EACnB,MAAM,UAAU,CAAA;AAejB,MAAM,MAAM,uBAAuB,GAAG;IACpC,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,EAAE,WAAW,EAAE,CAAA;IACrB,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,wBAAwB,GAAG,kBAAkB,GAAG;IAC1D;;;;OAIG;IACH,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,MAAM,EAAE,SAAS,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAA;CACnE,CAAA;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAE/B,KAAK,EAAE,SAAS,GAAG,UAAU,EAE7B,OAAO,CAAC,EAAE,wBAAwB,EAElC,IAAI,CAAC,EAAE,eAAe,EAEtB,MAAM,GAAE,WAAW,EAAO,GACzB,uBAAuB,CAqCzB"}
|
|
@@ -14,66 +14,64 @@ options,
|
|
|
14
14
|
// Fallback to the entrypoint
|
|
15
15
|
file,
|
|
16
16
|
// Errors that occurred during the process
|
|
17
|
-
errors) {
|
|
18
|
-
// Initialize errors
|
|
19
|
-
if (errors === undefined) {
|
|
20
|
-
errors = [];
|
|
21
|
-
}
|
|
17
|
+
errors = []) {
|
|
22
18
|
// Detach from input
|
|
23
19
|
const clonedInput = structuredClone(input);
|
|
24
20
|
// Make it a filesystem, even if it’s just one file
|
|
25
21
|
const filesystem = makeFilesystem(clonedInput);
|
|
26
22
|
// Get the main file
|
|
27
23
|
const entrypoint = getEntrypoint(filesystem);
|
|
24
|
+
const finalInput = file?.specification ?? entrypoint.specification;
|
|
28
25
|
// Recursively resolve all references
|
|
29
|
-
|
|
26
|
+
dereference(finalInput, filesystem, file ?? entrypoint, new WeakSet(), errors, options);
|
|
30
27
|
// Remove duplicats (according to message) from errors
|
|
31
28
|
errors = errors.filter((error, index, self) => index ===
|
|
32
29
|
self.findIndex((t) => t.message === error.message && t.code === error.code));
|
|
33
30
|
// Return the resolved specification
|
|
34
31
|
return {
|
|
35
32
|
valid: errors.length === 0,
|
|
36
|
-
errors
|
|
37
|
-
schema:
|
|
38
|
-
.specification,
|
|
33
|
+
errors,
|
|
34
|
+
schema: finalInput,
|
|
39
35
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Resolves the circular reference to an object and deletes the $ref properties (in-place).
|
|
39
|
+
*/
|
|
40
|
+
function dereference(schema, filesystem, entrypoint,
|
|
41
|
+
// references to resolved object
|
|
42
|
+
resolvedSchemas,
|
|
43
|
+
// error output
|
|
44
|
+
errors, options) {
|
|
45
|
+
if (schema === null || resolvedSchemas.has(schema))
|
|
46
|
+
return;
|
|
47
|
+
resolvedSchemas.add(schema);
|
|
48
|
+
function resolveExternal(externalFile) {
|
|
49
|
+
dereference(externalFile.specification, filesystem, externalFile, resolvedSchemas, errors, options);
|
|
50
|
+
return externalFile;
|
|
51
|
+
}
|
|
52
|
+
while (schema.$ref !== undefined) {
|
|
53
|
+
// Find the referenced content
|
|
54
|
+
const resolved = resolveUri(schema.$ref, options, entrypoint, filesystem, resolveExternal, errors);
|
|
55
|
+
// invalid
|
|
56
|
+
if (typeof resolved !== 'object' || resolved === null)
|
|
57
|
+
break;
|
|
58
|
+
const dereferencedRef = schema.$ref;
|
|
59
|
+
// Get rid of the reference
|
|
60
|
+
delete schema.$ref;
|
|
61
|
+
for (const key of Object.keys(resolved)) {
|
|
62
|
+
if (schema[key] === undefined) {
|
|
63
|
+
schema[key] = resolved[key];
|
|
68
64
|
}
|
|
69
65
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
66
|
+
if (dereferencedRef) {
|
|
67
|
+
options?.onDereference?.({ schema, ref: dereferencedRef });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Iterate over the whole object
|
|
71
|
+
for (const value of Object.values(schema)) {
|
|
72
|
+
if (typeof value === 'object' && value !== null) {
|
|
73
|
+
dereference(value, filesystem, entrypoint, resolvedSchemas, errors, options);
|
|
75
74
|
}
|
|
76
|
-
return result;
|
|
77
75
|
}
|
|
78
76
|
}
|
|
79
77
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"traverse.d.ts","sourceRoot":"","sources":["../../src/utils/traverse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAEzC;;GAEG;AACH,wBAAgB,QAAQ,CACtB,aAAa,EAAE,SAAS,EACxB,SAAS,EAAE,CAAC,aAAa,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,SAAS,EACnE,IAAI,GAAE,MAAM,EAAO,
|
|
1
|
+
{"version":3,"file":"traverse.d.ts","sourceRoot":"","sources":["../../src/utils/traverse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAEzC;;GAEG;AACH,wBAAgB,QAAQ,CACtB,aAAa,EAAE,SAAS,EACxB,SAAS,EAAE,CAAC,aAAa,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,SAAS,EACnE,IAAI,GAAE,MAAM,EAAO,aA0BpB"}
|
package/dist/utils/traverse.js
CHANGED
|
@@ -7,13 +7,15 @@ function traverse(specification, transform, path = []) {
|
|
|
7
7
|
const currentPath = [...path, key];
|
|
8
8
|
if (Array.isArray(value)) {
|
|
9
9
|
result[key] = value.map((item, index) => {
|
|
10
|
-
if (typeof item === 'object' && item !== null) {
|
|
10
|
+
if (typeof item === 'object' && !Array.isArray(item) && item !== null) {
|
|
11
11
|
return traverse(item, transform, [...currentPath, index.toString()]);
|
|
12
12
|
}
|
|
13
13
|
return item;
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
else if (typeof value === 'object' &&
|
|
16
|
+
else if (typeof value === 'object' &&
|
|
17
|
+
!Array.isArray(value) &&
|
|
18
|
+
value !== null) {
|
|
17
19
|
result[key] = traverse(value, transform, currentPath);
|
|
18
20
|
}
|
|
19
21
|
else {
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"parser",
|
|
18
18
|
"typescript"
|
|
19
19
|
],
|
|
20
|
-
"version": "0.10.
|
|
20
|
+
"version": "0.10.4",
|
|
21
21
|
"engines": {
|
|
22
22
|
"node": ">=18"
|
|
23
23
|
},
|
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
"json-to-ast": "^2.1.0",
|
|
65
65
|
"just-diff": "^6.0.2",
|
|
66
66
|
"tinybench": "^2.8.0",
|
|
67
|
-
"@scalar/openapi-types": "0.1.5",
|
|
68
67
|
"@scalar/build-tooling": "0.1.12",
|
|
69
|
-
"@scalar/types": "0.
|
|
68
|
+
"@scalar/openapi-types": "0.1.6",
|
|
69
|
+
"@scalar/types": "0.0.26"
|
|
70
70
|
},
|
|
71
71
|
"scripts": {
|
|
72
72
|
"build": "scalar-build-rollup",
|