@hey-api/json-schema-ref-parser 1.0.8 → 1.1.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.
@@ -1,4 +1,5 @@
1
1
  import $Ref from "./ref.js";
2
+ import cloneDeep from "lodash/cloneDeep";
2
3
  import Pointer from "./pointer.js";
3
4
  import { ono } from "@jsdevtools/ono";
4
5
  import * as url from "./util/url.js";
@@ -17,10 +18,7 @@ export default dereference;
17
18
  * @param parser
18
19
  * @param options
19
20
  */
20
- function dereference(
21
- parser: $RefParser,
22
- options: ParserOptions,
23
- ) {
21
+ function dereference(parser: $RefParser, options: ParserOptions) {
24
22
  const start = Date.now();
25
23
  // console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
26
24
  const dereferenced = crawl<JSONSchema>(
@@ -200,11 +198,12 @@ function dereference$Ref<S extends object = JSONSchema>(
200
198
  }
201
199
  return {
202
200
  circular: cache.circular,
203
- value: Object.assign({}, cache.value, extraKeys),
201
+ value: Object.assign({}, cloneDeep(cache.value), extraKeys),
204
202
  };
205
203
  }
206
204
 
207
- return cache;
205
+ // Return a deep-cloned value so each occurrence is an independent copy
206
+ return { circular: cache.circular, value: cloneDeep(cache.value) };
208
207
  }
209
208
 
210
209
  const pointer = $refs._resolve($refPath, path, options);
package/lib/index.ts CHANGED
@@ -14,7 +14,7 @@ import { fileResolver } from "./resolvers/file.js";
14
14
  interface ResolvedInput {
15
15
  path: string;
16
16
  schema: string | JSONSchema | Buffer | Awaited<JSONSchema> | undefined;
17
- type: 'file' | 'json' | 'url';
17
+ type: "file" | "json" | "url";
18
18
  }
19
19
 
20
20
  export const getResolvedInput = ({
@@ -27,10 +27,10 @@ export const getResolvedInput = ({
27
27
  }
28
28
 
29
29
  const resolvedInput: ResolvedInput = {
30
- path: typeof pathOrUrlOrSchema === 'string' ? pathOrUrlOrSchema : '',
30
+ path: typeof pathOrUrlOrSchema === "string" ? pathOrUrlOrSchema : "",
31
31
  schema: undefined,
32
- type: 'url',
33
- }
32
+ type: "url",
33
+ };
34
34
 
35
35
  // If the path is a filesystem path, then convert it to a URL.
36
36
  // NOTE: According to the JSON Reference spec, these should already be URLs,
@@ -40,27 +40,27 @@ export const getResolvedInput = ({
40
40
  // If it doesn't work for your use-case, then use a URL instead.
41
41
  if (resolvedInput.path && url.isFileSystemPath(resolvedInput.path)) {
42
42
  resolvedInput.path = url.fromFileSystemPath(resolvedInput.path);
43
- resolvedInput.type = 'file';
44
- } else if (!resolvedInput.path && pathOrUrlOrSchema && typeof pathOrUrlOrSchema === 'object') {
43
+ resolvedInput.type = "file";
44
+ } else if (!resolvedInput.path && pathOrUrlOrSchema && typeof pathOrUrlOrSchema === "object") {
45
45
  if ("$id" in pathOrUrlOrSchema && pathOrUrlOrSchema.$id) {
46
46
  // when schema id has defined an URL should use that hostname to request the references,
47
47
  // instead of using the current page URL
48
48
  const { hostname, protocol } = new URL(pathOrUrlOrSchema.$id as string);
49
49
  resolvedInput.path = `${protocol}//${hostname}:${protocol === "https:" ? 443 : 80}`;
50
- resolvedInput.type = 'url';
50
+ resolvedInput.type = "url";
51
51
  } else {
52
52
  resolvedInput.schema = pathOrUrlOrSchema;
53
- resolvedInput.type = 'json';
53
+ resolvedInput.type = "json";
54
54
  }
55
55
  }
56
56
 
57
- if (resolvedInput.type !== 'json') {
57
+ if (resolvedInput.type !== "json") {
58
58
  // resolve the absolute path of the schema
59
59
  resolvedInput.path = url.resolve(url.cwd(), resolvedInput.path);
60
60
  }
61
61
 
62
62
  return resolvedInput;
63
- }
63
+ };
64
64
 
65
65
  /**
66
66
  * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
@@ -74,7 +74,7 @@ export class $RefParser {
74
74
  * @readonly
75
75
  */
76
76
  $refs = new $Refs<JSONSchema>();
77
- public options = getJsonSchemaRefParserDefaultOptions()
77
+ public options = getJsonSchemaRefParserDefaultOptions();
78
78
  /**
79
79
  * The parsed (and possibly dereferenced) JSON schema object
80
80
  *
@@ -185,17 +185,17 @@ export class $RefParser {
185
185
  if (schema) {
186
186
  // immediately add a new $Ref with the schema object as value
187
187
  const $ref = this.$refs._add(path);
188
- $ref.pathType = url.isFileSystemPath(path) ? 'file' : 'http';
188
+ $ref.pathType = url.isFileSystemPath(path) ? "file" : "http";
189
189
  $ref.value = schema;
190
- } else if (type !== 'json') {
191
- const file = newFile(path)
190
+ } else if (type !== "json") {
191
+ const file = newFile(path);
192
192
 
193
193
  // Add a new $Ref for this file, even though we don't have the value yet.
194
194
  // This ensures that we don't simultaneously read & parse the same file multiple times
195
195
  const $refAdded = this.$refs._add(file.url);
196
196
  $refAdded.pathType = type;
197
197
  try {
198
- const resolver = type === 'file' ? fileResolver : urlResolver;
198
+ const resolver = type === "file" ? fileResolver : urlResolver;
199
199
  await resolver.handler({
200
200
  arrayBuffer,
201
201
  fetch,
@@ -208,12 +208,12 @@ export class $RefParser {
208
208
  if (isHandledError(err)) {
209
209
  $refAdded.value = err;
210
210
  }
211
-
211
+
212
212
  throw err;
213
213
  }
214
214
  }
215
215
 
216
- if (schema === null || typeof schema !== 'object' || Buffer.isBuffer(schema)) {
216
+ if (schema === null || typeof schema !== "object" || Buffer.isBuffer(schema)) {
217
217
  throw ono.syntax(`"${this.$refs._root$Ref.path || schema}" is not a valid JSON Schema`);
218
218
  }
219
219
 
@@ -225,5 +225,5 @@ export class $RefParser {
225
225
  }
226
226
  }
227
227
 
228
- export { sendRequest } from './resolvers/url.js'
228
+ export { sendRequest } from "./resolvers/url.js";
229
229
  export type { JSONSchema } from "./types/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hey-api/json-schema-ref-parser",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "Parse, Resolve, and Dereference JSON Schema $ref pointers",
5
5
  "homepage": "https://heyapi.dev/",
6
6
  "repository": {