@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,5 +1,15 @@
1
1
  # @scalar/openapi-parser
2
2
 
3
+ ## 0.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - fbef0c3: fix(openapi-parser): improve performance
8
+
9
+ ### Patch Changes
10
+
11
+ - fbef0c3: fix: doesn’t validate files with external references
12
+
3
13
  ## 0.9.0
4
14
 
5
15
  ### Minor Changes
@@ -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,CAoGzB"}
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, options?.onDereference);
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, onResolve) {
47
- let result;
48
- // Iterate over the whole objecct
49
- Object.entries(schema ?? {}).forEach(([_, value]) => {
50
- // Ignore parts without a reference
51
- if (schema.$ref !== undefined) {
52
- // Find the referenced content
53
- const target = resolveUri(schema.$ref, options, resolveFile, resolveFilesystem, errors);
54
- if (target === undefined) {
55
- return undefined;
56
- }
57
- onResolve?.({ schema, ref: schema.$ref });
58
- // Get rid of the reference
59
- delete schema.$ref;
60
- if (typeof target === 'object') {
61
- Object.keys(target).forEach((key) => {
62
- if (schema[key] === undefined) {
63
- schema[key] = target[key];
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
- if (typeof value === 'object' && !isCircular(value)) {
69
- result = resolve(value, resolveFilesystem, resolveFile, onResolve);
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, errors) {
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 result.schema;
125
+ return externalReference.specification;
131
126
  }
132
127
  // $ref: 'other-file.yaml#/foo/bar'
133
- return resolveUri(`#${path}`, options, externalReference, filesystem, errors);
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);
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "parser",
18
18
  "typescript"
19
19
  ],
20
- "version": "0.9.0",
20
+ "version": "0.10.0",
21
21
  "engines": {
22
22
  "node": ">=18"
23
23
  },