-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathresolvers.spec.ts
255 lines (240 loc) · 7.91 KB
/
resolvers.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { describe, it } from "vitest";
import { expect } from "vitest";
import type { FileInfo } from "../../../lib/index.js";
import $RefParser from "../../../lib/index.js";
import helper from "../../utils/helper.js";
import path from "../../utils/path.js";
import parsedSchema from "./parsed.js";
import dereferencedSchema from "./dereferenced.js";
import { ResolverError, UnmatchedResolverError, JSONParserErrorGroup } from "../../../lib/util/errors.js";
import type { ParserOptions } from "../../../lib/options";
describe("options.resolve", () => {
it('should not resolve external links if "resolve.external" is disabled', async () => {
const schema = await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
resolve: { external: false },
});
expect(schema).to.deep.equal(parsedSchema);
});
it("should throw an error for unrecognized protocols", async () => {
try {
await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"));
helper.shouldNotGetCalled();
} catch (err) {
expect(err).to.be.an.instanceOf(SyntaxError);
expect((err as Error).message).to.equal('Unable to resolve $ref pointer "foo://bar.baz"');
}
});
it("should use a custom resolver with static values", async () => {
const schema = await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
resolve: {
// A custom resolver for "foo://" URLs
foo: {
canRead: /^foo:\/\//i,
read: { bar: { baz: "hello world" } },
},
bar: {
canRead: /^bar:\/\//i,
read: { Foo: { Baz: "hello world" } },
},
},
} as ParserOptions);
expect(schema).to.deep.equal(dereferencedSchema);
});
it("should use a custom resolver that returns a value", async () => {
const schema = await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
resolve: {
// A custom resolver for "foo://" URLs
foo: {
canRead: /^foo:\/\//i,
read(_file: any) {
return { bar: { baz: "hello world" } };
},
},
bar: {
canRead: /^bar:\/\//i,
read(_file: any) {
return { Foo: { Baz: "hello world" } };
},
},
},
});
expect(schema).to.deep.equal(dereferencedSchema);
});
it("should return _file url as it's written", async () => {
const schema = await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
resolve: {
// A custom resolver for "foo://" URLs
foo: {
canRead: /^foo:\/\//i,
read(_file: any) {
return { bar: { baz: "hello world" } };
},
},
// A custom resolver with uppercase symbols
bar: {
canRead: /^bar:\/\//i,
read(_file: any) {
expect(_file.url).to.be.equal("bar://Foo.Baz");
return { Foo: { Baz: "hello world" } };
},
},
},
});
expect(schema).to.deep.equal(dereferencedSchema);
});
it("should use a custom resolver that calls a callback", async () => {
const schema = await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
resolve: {
// A custom resolver for "foo://" URLs
foo: {
canRead: /^foo:\/\//i,
read(_file: any, callback: any) {
callback(null, { bar: { baz: "hello world" } });
},
},
bar: {
canRead: /^bar:\/\//i,
read(_file: any, callback: any) {
callback(null, { Foo: { Baz: "hello world" } });
},
},
},
});
expect(schema).to.deep.equal(dereferencedSchema);
});
if (typeof Promise === "function") {
it("should use a custom resolver that returns a promise", async () => {
const schema = await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
resolve: {
// A custom resolver for "foo://" URLs
foo: {
canRead: /^foo:\/\//i,
read(_file: any) {
return Promise.resolve({ bar: { baz: "hello world" } });
},
},
bar: {
canRead: /^bar:\/\//i,
read(_file: any) {
return Promise.resolve({ Foo: { Baz: "hello world" } });
},
},
},
});
expect(schema).to.deep.equal(dereferencedSchema);
});
}
it("should continue resolving if a custom resolver fails", async () => {
const schema = await $RefParser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
resolve: {
// A custom resolver that always fails
badResolver: {
order: 1,
canRead: true,
read(_file: any) {
throw new Error("BOMB!!!");
},
},
// A custom resolver for "foo://" URLs
foo: {
canRead: /^foo:\/\//i,
read: { bar: { baz: "hello world" } },
},
bar: {
canRead: /^bar:\/\//i,
read: { Foo: { Baz: "hello world" } },
},
},
});
expect(schema).to.deep.equal(dereferencedSchema);
});
it("should normalize errors thrown by resolvers", async () => {
try {
await $RefParser.dereference({ $ref: path.abs("test/specs/resolvers/resolvers.yaml") }, {
resolve: {
// A custom resolver that always fails
file: {
order: 1,
canRead: true,
parse() {
throw new Error("Woops");
},
},
},
} as ParserOptions);
helper.shouldNotGetCalled();
} catch (err) {
expect(err).to.be.instanceof(ResolverError);
expect((err as ResolverError).message).to.contain("Error opening file");
}
});
it("should throw an error if no resolver returned a result", async () => {
try {
await $RefParser.dereference(path.rel("test/specs/resolvers/resolvers.yaml"), {
resolve: {
http: false,
file: {
order: 1,
canRead: true,
read() {
return;
},
},
},
});
helper.shouldNotGetCalled();
} catch (err) {
// would time out otherwise
expect(err).to.be.an.instanceOf(ResolverError);
}
});
it("should throw a grouped error if no resolver can be matched and continueOnError is true", async () => {
const parser = new $RefParser();
try {
await parser.dereference(path.abs("test/specs/resolvers/resolvers.yaml"), {
resolve: {
file: false,
http: false,
},
continueOnError: true,
});
helper.shouldNotGetCalled();
} catch (err) {
expect(err).to.be.instanceof(JSONParserErrorGroup);
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.errors.length).to.equal(1);
// @ts-expect-error TS(2571): Object is of type 'unknown'.
expect(err.errors).to.containSubset([
{
name: UnmatchedResolverError.name,
message: (message: any) => message.startsWith("Could not find resolver for"),
path: [],
source: (message: any) => message.endsWith("specs/resolvers/resolvers.yaml"),
},
]);
}
});
it("should preserver capitalization", async () => {
const parser = new $RefParser();
let parsed: string | undefined;
await parser.dereference(
{
$ref: "custom://Path/Is/Case/Sensitive",
},
{
resolve: {
custom: {
order: 1,
canRead: /^custom:\/\//i,
read(file: FileInfo, callback?: (error: Error | null, data: string | null) => any) {
console.log(file.url);
parsed = file.url;
callback?.(null, "custom://Path/Is/Case/Sensitive");
},
},
},
} as ParserOptions,
);
expect(parsed).to.equal("custom://Path/Is/Case/Sensitive");
});
});