@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/__tests__/bundle.test.js +8 -11
- package/dist/lib/__tests__/pointer.test.js +5 -4
- package/dist/lib/bundle.d.ts +1 -0
- package/dist/lib/bundle.js +336 -80
- package/dist/lib/dereference.js +4 -2
- package/dist/lib/index.d.ts +2 -2
- package/dist/lib/index.js +11 -11
- package/lib/__tests__/bundle.test.ts +9 -15
- package/lib/__tests__/pointer.test.ts +5 -4
- package/lib/__tests__/spec/multiple-refs.json +2 -2
- package/lib/__tests__/spec/path-parameter.json +12 -8
- package/lib/bundle.ts +411 -80
- package/lib/dereference.ts +5 -6
- package/lib/index.ts +18 -18
- package/package.json +1 -1
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 ===
|
|
56
|
+
path: typeof pathOrUrlOrSchema === "string" ? pathOrUrlOrSchema : "",
|
|
57
57
|
schema: undefined,
|
|
58
|
-
type:
|
|
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 =
|
|
68
|
+
resolvedInput.type = "file";
|
|
69
69
|
}
|
|
70
|
-
else if (!resolvedInput.path && pathOrUrlOrSchema && typeof pathOrUrlOrSchema ===
|
|
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 =
|
|
76
|
+
resolvedInput.type = "url";
|
|
77
77
|
}
|
|
78
78
|
else {
|
|
79
79
|
resolvedInput.schema = pathOrUrlOrSchema;
|
|
80
|
-
resolvedInput.type =
|
|
80
|
+
resolvedInput.type = "json";
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
-
if (resolvedInput.type !==
|
|
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) ?
|
|
184
|
+
$ref.pathType = url.isFileSystemPath(path) ? "file" : "http";
|
|
185
185
|
$ref.value = schema;
|
|
186
186
|
}
|
|
187
|
-
else if (type !==
|
|
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 ===
|
|
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 !==
|
|
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
|
-
//
|
|
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
|
|
36
|
-
expect(
|
|
37
|
-
expect(
|
|
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).
|
|
20
|
-
expect(postSchema.type).
|
|
21
|
-
expect(postSchema.properties
|
|
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).
|
|
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
|
-
"
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
}
|