@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.
package/dist/lib/index.js CHANGED
@@ -53,9 +53,9 @@ const getResolvedInput = ({ pathOrUrlOrSchema, }) => {
53
53
  throw (0, ono_1.ono)(`Expected a file path, URL, or object. Got ${pathOrUrlOrSchema}`);
54
54
  }
55
55
  const resolvedInput = {
56
- path: typeof pathOrUrlOrSchema === 'string' ? pathOrUrlOrSchema : '',
56
+ path: typeof pathOrUrlOrSchema === "string" ? pathOrUrlOrSchema : "",
57
57
  schema: undefined,
58
- type: 'url',
58
+ type: "url",
59
59
  };
60
60
  // If the path is a filesystem path, then convert it to a URL.
61
61
  // NOTE: According to the JSON Reference spec, these should already be URLs,
@@ -65,22 +65,22 @@ const getResolvedInput = ({ pathOrUrlOrSchema, }) => {
65
65
  // If it doesn't work for your use-case, then use a URL instead.
66
66
  if (resolvedInput.path && url.isFileSystemPath(resolvedInput.path)) {
67
67
  resolvedInput.path = url.fromFileSystemPath(resolvedInput.path);
68
- resolvedInput.type = 'file';
68
+ resolvedInput.type = "file";
69
69
  }
70
- else if (!resolvedInput.path && pathOrUrlOrSchema && typeof pathOrUrlOrSchema === 'object') {
70
+ else if (!resolvedInput.path && pathOrUrlOrSchema && typeof pathOrUrlOrSchema === "object") {
71
71
  if ("$id" in pathOrUrlOrSchema && pathOrUrlOrSchema.$id) {
72
72
  // when schema id has defined an URL should use that hostname to request the references,
73
73
  // instead of using the current page URL
74
74
  const { hostname, protocol } = new URL(pathOrUrlOrSchema.$id);
75
75
  resolvedInput.path = `${protocol}//${hostname}:${protocol === "https:" ? 443 : 80}`;
76
- resolvedInput.type = 'url';
76
+ resolvedInput.type = "url";
77
77
  }
78
78
  else {
79
79
  resolvedInput.schema = pathOrUrlOrSchema;
80
- resolvedInput.type = 'json';
80
+ resolvedInput.type = "json";
81
81
  }
82
82
  }
83
- if (resolvedInput.type !== 'json') {
83
+ if (resolvedInput.type !== "json") {
84
84
  // resolve the absolute path of the schema
85
85
  resolvedInput.path = url.resolve(url.cwd(), resolvedInput.path);
86
86
  }
@@ -181,17 +181,17 @@ class $RefParser {
181
181
  if (schema) {
182
182
  // immediately add a new $Ref with the schema object as value
183
183
  const $ref = this.$refs._add(path);
184
- $ref.pathType = url.isFileSystemPath(path) ? 'file' : 'http';
184
+ $ref.pathType = url.isFileSystemPath(path) ? "file" : "http";
185
185
  $ref.value = schema;
186
186
  }
187
- else if (type !== 'json') {
187
+ else if (type !== "json") {
188
188
  const file = (0, parse_js_1.newFile)(path);
189
189
  // Add a new $Ref for this file, even though we don't have the value yet.
190
190
  // This ensures that we don't simultaneously read & parse the same file multiple times
191
191
  const $refAdded = this.$refs._add(file.url);
192
192
  $refAdded.pathType = type;
193
193
  try {
194
- const resolver = type === 'file' ? file_js_1.fileResolver : url_js_1.urlResolver;
194
+ const resolver = type === "file" ? file_js_1.fileResolver : url_js_1.urlResolver;
195
195
  await resolver.handler({
196
196
  arrayBuffer,
197
197
  fetch,
@@ -208,7 +208,7 @@ class $RefParser {
208
208
  throw err;
209
209
  }
210
210
  }
211
- if (schema === null || typeof schema !== 'object' || Buffer.isBuffer(schema)) {
211
+ if (schema === null || typeof schema !== "object" || Buffer.isBuffer(schema)) {
212
212
  throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || schema}" is not a valid JSON Schema`);
213
213
  }
214
214
  this.schema = schema;
@@ -17,24 +17,18 @@ describe("bundle", () => {
17
17
  const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "multiple-refs.json");
18
18
  const schema = (await refParser.bundle({ pathOrUrlOrSchema })) as any;
19
19
 
20
- // First reference should be fully resolved (no $ref)
21
- expect(schema.paths["/test1/{pathId}"].get.parameters[0].name).toBe("pathId");
22
- expect(schema.paths["/test1/{pathId}"].get.parameters[0].schema.type).toBe("string");
23
- expect(schema.paths["/test1/{pathId}"].get.parameters[0].schema.format).toBe("uuid");
24
- expect(schema.paths["/test1/{pathId}"].get.parameters[0].$ref).toBeUndefined();
25
-
26
- // Second reference should be remapped to point to the first reference
27
- expect(schema.paths["/test2/{pathId}"].get.parameters[0].$ref).toBe(
28
- "#/paths/~1test1~1%7BpathId%7D/get/parameters/0",
29
- );
30
-
31
- // Both should effectively resolve to the same data
20
+ // Both parameters should now be $ref to the same internal definition
32
21
  const firstParam = schema.paths["/test1/{pathId}"].get.parameters[0];
33
22
  const secondParam = schema.paths["/test2/{pathId}"].get.parameters[0];
34
23
 
35
- // The second parameter should resolve to the same data as the first
36
- expect(secondParam.$ref).toBeDefined();
37
- expect(firstParam).toEqual({
24
+ // The $ref should match the output structure in file_context_0
25
+ expect(firstParam.$ref).toBe("#/components/parameters/path-parameter_pathId");
26
+ expect(secondParam.$ref).toBe("#/components/parameters/path-parameter_pathId");
27
+
28
+ // The referenced parameter should exist and match the expected structure
29
+ expect(schema.components).toBeDefined();
30
+ expect(schema.components.parameters).toBeDefined();
31
+ expect(schema.components.parameters["path-parameter_pathId"]).toEqual({
38
32
  name: "pathId",
39
33
  in: "path",
40
34
  required: true,
@@ -7,6 +7,7 @@ describe("pointer", () => {
7
7
  const refParser = new $RefParser();
8
8
  const pathOrUrlOrSchema = path.resolve("lib", "__tests__", "spec", "openapi-paths-ref.json");
9
9
  const schema = (await refParser.bundle({ pathOrUrlOrSchema })) as any;
10
+ console.log(JSON.stringify(schema, null, 2));
10
11
 
11
12
  // The GET endpoint should have its schema defined inline
12
13
  const getSchema = schema.paths["/foo"].get.responses["200"].content["application/json"].schema;
@@ -16,11 +17,11 @@ describe("pointer", () => {
16
17
 
17
18
  // The POST endpoint should have its schema inlined (copied) instead of a $ref
18
19
  const postSchema = schema.paths["/foo"].post.responses["200"].content["application/json"].schema;
19
- expect(postSchema.$ref).toBeUndefined();
20
- expect(postSchema.type).toBe("object");
21
- expect(postSchema.properties.bar.type).toBe("string");
20
+ expect(postSchema.$ref).toBe("#/paths/~1foo/get/responses/200/content/application~1json/schema");
21
+ expect(postSchema.type).toBeUndefined();
22
+ expect(postSchema.properties?.bar?.type).toBeUndefined();
22
23
 
23
24
  // Both schemas should be identical objects
24
- expect(postSchema).toEqual(getSchema);
25
+ expect(postSchema).not.toBe(getSchema);
25
26
  });
26
27
  });
@@ -5,7 +5,7 @@
5
5
  "summary": "First endpoint using the same pathId schema",
6
6
  "parameters": [
7
7
  {
8
- "$ref": "path-parameter.json#/pathId"
8
+ "$ref": "path-parameter.json#/components/parameters/pathId"
9
9
  }
10
10
  ],
11
11
  "responses": {
@@ -20,7 +20,7 @@
20
20
  "summary": "Second endpoint using the same pathId schema",
21
21
  "parameters": [
22
22
  {
23
- "$ref": "path-parameter.json#/pathId"
23
+ "$ref": "path-parameter.json#/components/parameters/pathId"
24
24
  }
25
25
  ],
26
26
  "responses": {
@@ -1,12 +1,16 @@
1
1
  {
2
- "pathId": {
3
- "name": "pathId",
4
- "in": "path",
5
- "required": true,
6
- "schema": {
7
- "type": "string",
8
- "format": "uuid",
9
- "description": "Unique identifier for the path"
2
+ "components": {
3
+ "parameters": {
4
+ "pathId": {
5
+ "name": "pathId",
6
+ "in": "path",
7
+ "required": true,
8
+ "schema": {
9
+ "type": "string",
10
+ "format": "uuid",
11
+ "description": "Unique identifier for the path"
12
+ }
13
+ }
10
14
  }
11
15
  }
12
16
  }