Skip to content

Commit 135bf4f

Browse files
authored
fix: replace deprecated resolve function
2 parents 5d7f895 + 58c8b49 commit 135bf4f

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

lib/util/url.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ let urlDecodePatterns = [
2323
];
2424

2525
exports.parse = require("url").parse;
26-
exports.resolve = require("url").resolve;
26+
27+
/**
28+
* Returns resolved target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF.
29+
*
30+
* @return {string}
31+
*/
32+
exports.resolve = function resolve (from, to) {
33+
let resolvedUrl = new URL(to, new URL(from, "resolve://"));
34+
if (resolvedUrl.protocol === "resolve:") {
35+
// `from` is a relative URL.
36+
let { pathname, search, hash } = resolvedUrl;
37+
return pathname + search + hash;
38+
}
39+
return resolvedUrl.toString();
40+
};
2741

2842
/**
2943
* Returns the current working directory (in Node) or the current page URL (in browsers).

test/specs/resolvers/dereferenced.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ module.exports =
88
baz: "hello world"
99
}
1010
},
11+
bar: {
12+
Foo: {
13+
Baz: "hello world"
14+
}
15+
},
1116
name: {
1217
required: [
1318
"first",

test/specs/resolvers/parsed.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module.exports =
66
foo: {
77
$ref: "foo://bar.baz"
88
},
9+
bar: {
10+
$ref: "bar://Foo.Baz"
11+
},
912
pet: {
1013
$ref: "definitions/pet.yaml"
1114
},

test/specs/resolvers/resolvers.spec.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ describe("options.resolve", () => {
3737
foo: {
3838
canRead: /^foo\:\/\//i,
3939
read: { bar: { baz: "hello world" }}
40+
},
41+
bar: {
42+
canRead: /^bar\:\/\//i,
43+
read: { Foo: { Baz: "hello world" }}
4044
}
4145
}
4246
});
47+
4348
expect(schema).to.deep.equal(dereferencedSchema);
4449
});
4550

@@ -53,12 +58,44 @@ describe("options.resolve", () => {
5358
read (_file) {
5459
return { bar: { baz: "hello world" }};
5560
}
61+
},
62+
bar: {
63+
canRead: /^bar\:\/\//i,
64+
read (_file) {
65+
return { Foo: { Baz: "hello world" }};
66+
}
5667
}
5768
}
5869
});
5970
expect(schema).to.deep.equal(dereferencedSchema);
6071
});
6172

73+
it("should return _file url as it's written", async () => {
74+
const schema = await $RefParser
75+
.dereference(path.abs("specs/resolvers/resolvers.yaml"), {
76+
resolve: {
77+
// A custom resolver for "foo://" URLs
78+
foo: {
79+
canRead: /^foo\:\/\//i,
80+
read (_file) {
81+
return { bar: { baz: "hello world" }};
82+
}
83+
},
84+
// A custom resolver with uppercase symbols
85+
bar: {
86+
canRead: /^bar\:\/\//i,
87+
read (_file) {
88+
expect(_file.url).to.be.equal("bar://Foo.Baz");
89+
90+
return { Foo: { Baz: "hello world" }};
91+
}
92+
}
93+
}
94+
});
95+
96+
expect(schema).to.deep.equal(dereferencedSchema);
97+
});
98+
6299
it("should use a custom resolver that calls a callback", async () => {
63100
const schema = await $RefParser
64101
.dereference(path.abs("specs/resolvers/resolvers.yaml"), {
@@ -69,6 +106,12 @@ describe("options.resolve", () => {
69106
read (_file, callback) {
70107
callback(null, { bar: { baz: "hello world" }});
71108
}
109+
},
110+
bar: {
111+
canRead: /^bar\:\/\//i,
112+
read (_file, callback) {
113+
callback(null, { Foo: { Baz: "hello world" }});
114+
}
72115
}
73116
}
74117
});
@@ -86,6 +129,12 @@ describe("options.resolve", () => {
86129
read (_file) {
87130
return Promise.resolve({ bar: { baz: "hello world" }});
88131
}
132+
},
133+
bar: {
134+
canRead: /^bar\:\/\//i,
135+
read (_file) {
136+
return Promise.resolve({ Foo: { Baz: "hello world" }});
137+
}
89138
}
90139
}
91140
});
@@ -109,6 +158,10 @@ describe("options.resolve", () => {
109158
foo: {
110159
canRead: /^foo\:\/\//i,
111160
read: { bar: { baz: "hello world" }}
161+
},
162+
bar: {
163+
canRead: /^bar\:\/\//i,
164+
read: { Foo: { Baz: "hello world" }}
112165
}
113166
}
114167
});
@@ -180,7 +233,7 @@ describe("options.resolve", () => {
180233
message: message => message.startsWith("Could not find resolver for"),
181234
path: [],
182235
source: message => message.endsWith("specs/resolvers/resolvers.yaml"),
183-
},
236+
}
184237
]);
185238
}
186239
});

test/specs/resolvers/resolvers.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ definitions:
2727
$ref: "#/definitions/name/properties/first"
2828
foo:
2929
$ref: foo://bar.baz
30+
bar:
31+
$ref: bar://Foo.Baz
3032
pet:
3133
$ref: definitions/pet.yaml

0 commit comments

Comments
 (0)