-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
370 lines (306 loc) · 15.4 KB
/
index.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import path from 'node:path';
import { assert } from 'chai';
import { toValue } from '@swagger-api/apidom-core';
import { isSchemaElement, mediaTypes } from '@swagger-api/apidom-ns-openapi-2';
import { evaluate } from '@swagger-api/apidom-json-pointer';
import { loadJsonFile } from '../../../../helpers';
import { parse, dereference, Reference, ReferenceSet } from '../../../../../src';
import DereferenceError from '../../../../../src/errors/DereferenceError';
import MaximumDereferenceDepthError from '../../../../../src/errors/MaximumDereferenceDepthError';
import MaximumResolveDepthError from '../../../../../src/errors/MaximumResolveDepthError';
const rootFixturePath = path.join(__dirname, 'fixtures');
describe('dereference', function () {
context('strategies', function () {
context('openapi-2', function () {
context('JSONReference Object', function () {
context('given JSONReference Objects pointing internally and externally', function () {
const fixturePath = path.join(rootFixturePath, 'internal-external');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
specify('should apply semantics to external fragment', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const dereferenced = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const fragment = evaluate('/0/definitions/schema2', dereferenced);
assert.isTrue(isSchemaElement(fragment));
});
specify(
'should annotate transcluded element with additional metadata',
async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const dereferenced = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const fragment = evaluate('/0/definitions/schema1', dereferenced);
assert.strictEqual(
toValue(fragment.meta.get('ref-fields').get('$ref')),
'#/definitions/schema3',
);
},
);
});
context('given JSONReference Objects pointing internally only', function () {
const fixturePath = path.join(rootFixturePath, 'internal-only');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
});
context('given JSONReference Objects pointing externally only', function () {
const fixturePath = path.join(rootFixturePath, 'external-only');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
});
context('given JSONReference Objects pointing to external cycles', function () {
const fixturePath = path.join(rootFixturePath, 'external-cycle');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const dereferenced = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const parent = evaluate('/0/definitions/schema/properties', dereferenced);
const cyclicParent = evaluate(
'/0/definitions/schema/properties/parent/properties',
dereferenced,
);
assert.strictEqual(parent, cyclicParent);
});
});
context('given JSONReference Objects pointing to external indirections', function () {
const fixturePath = path.join(rootFixturePath, 'external-indirections');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
specify('should apply semantics to eventual external fragment', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const dereferenced = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const fragment = evaluate('/0/definitions/schema', dereferenced);
assert.isTrue(isSchemaElement(fragment));
});
});
context('given JSONReference Objects with additional ignored fields', function () {
const fixturePath = path.join(rootFixturePath, 'additional-ignored-fields');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
});
context('given JSONReference Objects with external resolution disabled', function () {
const fixturePath = path.join(rootFixturePath, 'ignore-external');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { external: false },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
});
context('given JSONReference Objects with direct circular internal reference', function () {
const fixturePath = path.join(rootFixturePath, 'direct-internal-circular');
specify('should throw error', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
try {
await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, DereferenceError);
}
});
});
context(
'given JSONReference Objects with indirect circular internal reference',
function () {
const fixturePath = path.join(rootFixturePath, 'indirect-internal-circular');
specify('should throw error', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
try {
await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, DereferenceError);
}
});
},
);
context('given JSONReference Objects with direct circular external reference', function () {
const fixturePath = path.join(rootFixturePath, 'direct-external-circular');
specify('should throw error', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
try {
await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, DereferenceError);
}
});
});
context(
'given JSONReference Objects with indirect circular external reference',
function () {
const fixturePath = path.join(rootFixturePath, 'indirect-external-circular');
specify('should throw error', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
try {
await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, DereferenceError);
}
});
},
);
context('given JSONReference Objects with unresolvable reference', function () {
const fixturePath = path.join(rootFixturePath, 'unresolvable-reference');
specify('should throw error', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
try {
await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, DereferenceError);
}
});
});
context('given JSONReference Objects with invalid JSON Pointer', function () {
const fixturePath = path.join(rootFixturePath, 'invalid-pointer');
specify('should throw error', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
try {
await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
} catch (e) {
assert.instanceOf(e, DereferenceError);
}
});
});
context('given JSONReference Objects with arbitrary circular references', function () {
const fixturePath = path.join(rootFixturePath, 'ignore-arbitrary-$refs');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
});
context('given JSONReference Objects with external circular dependency', function () {
const fixturePath = path.join(rootFixturePath, 'external-circular-dependency');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
});
context('given JSONReference Objects and maxDepth of dereference', function () {
const fixturePath = path.join(rootFixturePath, 'max-depth');
specify('should throw error', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
try {
await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
dereference: { maxDepth: 2 },
});
assert.fail('should throw MaximumDereferenceDepthError');
} catch (error: any) {
assert.instanceOf(error, DereferenceError);
assert.instanceOf(error.cause.cause, MaximumDereferenceDepthError);
assert.match(error.cause.cause.message, /fixtures\/max-depth\/ex2.json"$/);
}
});
});
context('given JSONReference Objects and maxDepth of resolution', function () {
const fixturePath = path.join(rootFixturePath, 'max-depth');
specify('should throw error', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
try {
await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { maxDepth: 2 },
});
assert.fail('should throw MaximumResolveDepthError');
} catch (error: any) {
assert.instanceOf(error, DereferenceError);
assert.instanceOf(error.cause.cause, MaximumResolveDepthError);
assert.match(error.cause.cause.message, /fixtures\/max-depth\/ex2.json"$/);
}
});
});
context('given refSet is provided as an option', function () {
specify('should dereference without external resolution', async function () {
const fixturePath = path.join(__dirname, 'fixtures', 'refset-as-option');
const rootURI = path.join(fixturePath, 'root.json');
const rootParseResult = await parse(rootURI, { mediaType: mediaTypes.latest('json') });
const rootRef = Reference({ uri: rootURI, value: rootParseResult });
const ex1URI = path.join(fixturePath, 'ex1.json');
const ex1ParseResult = await parse(ex1URI, { mediaType: 'application/json' });
const ex1Ref = Reference({ uri: ex1URI, value: ex1ParseResult });
const ex2URI = path.join(fixturePath, 'ex2.json');
const ex2ParseResult = await parse(ex2URI, { mediaType: 'application/json' });
const ex2Ref = Reference({ uri: ex2URI, value: ex2ParseResult });
const ex3URI = path.join(fixturePath, 'ex3.json');
const ex3ParseResult = await parse(ex3URI, { mediaType: 'application/json' });
const ex3Ref = Reference({ uri: ex3URI, value: ex3ParseResult });
const refSet = ReferenceSet({ refs: [rootRef, ex1Ref, ex2Ref, ex3Ref] });
const actual = await dereference(rootURI, {
dereference: { refSet },
resolve: { resolvers: [] },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
});
context('given path with invalid URL characters - spaces', function () {
const fixturePath = path.join(rootFixturePath, 'path-encoding', 'path with spaces');
specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const actual = await dereference(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
assert.deepEqual(toValue(actual), expected);
});
});
});
});
});
});