@scalar/openapi-parser 0.9.0 → 0.10.0
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 +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;AAOD;;GAEG;AACH,wBAAgB,iBAAiB,CAE/B,KAAK,EAAE,SAAS,GAAG,UAAU,EAE7B,OAAO,CAAC,EAAE,wBAAwB,EAElC,IAAI,CAAC,EAAE,eAAe,EAEtB,MAAM,CAAC,EAAE,WAAW,EAAE,GACrB,uBAAuB,
|
|
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;AAOD;;GAEG;AACH,wBAAgB,iBAAiB,CAE/B,KAAK,EAAE,SAAS,GAAG,UAAU,EAE7B,OAAO,CAAC,EAAE,wBAAwB,EAElC,IAAI,CAAC,EAAE,eAAe,EAEtB,MAAM,CAAC,EAAE,WAAW,EAAE,GACrB,uBAAuB,CAqGzB"}
|
|
@@ -26,10 +26,7 @@ errors) {
|
|
|
26
26
|
// Get the main file
|
|
27
27
|
const entrypoint = getEntrypoint(filesystem);
|
|
28
28
|
// Recursively resolve all references
|
|
29
|
-
resolve(file?.specification ?? entrypoint.specification, filesystem, file ?? entrypoint
|
|
30
|
-
// If we replace references with content, that includes a reference, we can’t deal with that right-away.
|
|
31
|
-
// That’s why we need a second run.
|
|
32
|
-
resolve(file?.specification ?? entrypoint.specification, filesystem, file ?? entrypoint, options?.onDereference);
|
|
29
|
+
resolve(file?.specification ?? entrypoint.specification, filesystem, file ?? entrypoint);
|
|
33
30
|
// Remove duplicats (according to message) from errors
|
|
34
31
|
errors = errors.filter((error, index, self) => index ===
|
|
35
32
|
self.findIndex((t) => t.message === error.message && t.code === error.code));
|
|
@@ -43,49 +40,46 @@ errors) {
|
|
|
43
40
|
/**
|
|
44
41
|
* Resolves the circular reference to an object and deletes the $ref properties.
|
|
45
42
|
*/
|
|
46
|
-
function resolve(schema, resolveFilesystem, resolveFile,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
43
|
+
function resolve(schema, resolveFilesystem, resolveFile,
|
|
44
|
+
// references to resolved object
|
|
45
|
+
resolved = new WeakSet()) {
|
|
46
|
+
let result = { errors: [] };
|
|
47
|
+
if (schema === null || resolved.has(schema))
|
|
48
|
+
return result;
|
|
49
|
+
resolved.add(schema);
|
|
50
|
+
function resolveExternal(externalFile) {
|
|
51
|
+
resolve(externalFile.specification, resolveFilesystem, externalFile, resolved);
|
|
52
|
+
return externalFile;
|
|
53
|
+
}
|
|
54
|
+
// Ignore parts without a reference
|
|
55
|
+
while (schema.$ref !== undefined) {
|
|
56
|
+
// Find the referenced content
|
|
57
|
+
const target = resolveUri(schema.$ref, options, resolveFile, resolveFilesystem, resolveExternal, errors);
|
|
58
|
+
// invalid
|
|
59
|
+
if (typeof target !== 'object' || target === null)
|
|
60
|
+
break;
|
|
61
|
+
options?.onDereference?.({ schema, ref: schema.$ref });
|
|
62
|
+
// Get rid of the reference
|
|
63
|
+
delete schema.$ref;
|
|
64
|
+
for (const key of Object.keys(target)) {
|
|
65
|
+
if (schema[key] === undefined) {
|
|
66
|
+
schema[key] = target[key];
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
}
|
|
70
|
+
// Iterate over the whole object
|
|
71
|
+
for (const value of Object.values(schema)) {
|
|
72
|
+
if (typeof value === 'object' && value !== null) {
|
|
73
|
+
result = resolve(value, resolveFilesystem, resolveFile, resolved);
|
|
70
74
|
}
|
|
71
|
-
}
|
|
72
|
-
return
|
|
73
|
-
errors: result?.errors ?? [],
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
// TODO: Is there a better way? :D
|
|
78
|
-
function isCircular(schema) {
|
|
79
|
-
try {
|
|
80
|
-
JSON.stringify(schema);
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
catch (error) {
|
|
84
|
-
return true;
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
85
77
|
}
|
|
86
78
|
}
|
|
87
79
|
/**
|
|
88
80
|
* Resolves a URI to a part of the specification
|
|
81
|
+
*
|
|
82
|
+
* The output is not necessarily dereferenced
|
|
89
83
|
*/
|
|
90
84
|
function resolveUri(
|
|
91
85
|
// 'foobar.json#/foo/bar'
|
|
@@ -93,7 +87,9 @@ uri, options,
|
|
|
93
87
|
// { filename: './foobar.json '}
|
|
94
88
|
file,
|
|
95
89
|
// [ { filename: './foobar.json '} ]
|
|
96
|
-
filesystem,
|
|
90
|
+
filesystem,
|
|
91
|
+
// a function to resolve references in external file
|
|
92
|
+
resolve, errors) {
|
|
97
93
|
// Ignore invalid URIs
|
|
98
94
|
if (typeof uri !== 'string') {
|
|
99
95
|
if (options?.throwOnError) {
|
|
@@ -124,13 +120,13 @@ filesystem, errors) {
|
|
|
124
120
|
});
|
|
125
121
|
return;
|
|
126
122
|
}
|
|
127
|
-
const result = resolveReferences(filesystem, options, externalReference, errors);
|
|
128
123
|
// $ref: 'other-file.yaml'
|
|
129
124
|
if (path === undefined) {
|
|
130
|
-
return
|
|
125
|
+
return externalReference.specification;
|
|
131
126
|
}
|
|
132
127
|
// $ref: 'other-file.yaml#/foo/bar'
|
|
133
|
-
|
|
128
|
+
// resolve refs first before accessing properties directly
|
|
129
|
+
return resolveUri(`#${path}`, options, resolve(externalReference), filesystem, resolve, errors);
|
|
134
130
|
}
|
|
135
131
|
// Pointers
|
|
136
132
|
const segments = getSegmentsFromPath(path);
|