Skip to content

Commit 2e0263a

Browse files
committed
feat(reference): support native fragment - Path Item
Refs #2934
1 parent c5520ef commit 2e0263a

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

packages/apidom-reference/src/dereference/strategies/openapi-3-1/visitor.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ const OpenApi3_1DereferenceVisitor = stampit({
129129
return false;
130130
}
131131

132-
let reference = await this.toReference(this.reference.uri);
133-
reference = await this.toReference(referencingElement.$ref?.toValue());
132+
const reference = await this.toReference(referencingElement.$ref?.toValue());
134133
const { uri: retrievalURI } = reference;
135134
const $refBaseURI = url.resolve(retrievalURI, referencingElement.$ref?.toValue());
136135

@@ -245,7 +244,7 @@ const OpenApi3_1DereferenceVisitor = stampit({
245244
}
246245

247246
const reference = await this.toReference(referencingElement.$ref?.toValue());
248-
const retrievalURI = reference.uri;
247+
const { uri: retrievalURI } = reference;
249248
const $refBaseURI = url.resolve(retrievalURI, referencingElement.$ref?.toValue());
250249

251250
this.indirections.push(referencingElement);

packages/apidom-reference/test/dereference/strategies/openapi-3-1/path-item-object/dereference-apidom.ts

+72-28
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,85 @@ describe('dereference', function () {
1313
context('strategies', function () {
1414
context('openapi-3-1', function () {
1515
context('Path Item Object', function () {
16-
context('given single PathItemElement passed to dereferenceApiDOM', function () {
17-
const fixturePath = path.join(__dirname, 'fixtures', 'external-only', 'root.json');
16+
context(
17+
'given single PathItemElement passed to dereferenceApiDOM with internal references',
18+
function () {
19+
const fixturePath = path.join(__dirname, 'fixtures', 'internal-only', 'root.json');
1820

19-
specify('should dereference', async function () {
20-
const parseResult = await parse(fixturePath, {
21-
parse: { mediaType: mediaTypes.latest('json') },
22-
});
23-
const pathItemElement = evaluate(
24-
compile(['paths', '/path1']),
25-
parseResult.api as OpenApi3_1Element,
26-
);
27-
const dereferenced = await dereferenceApiDOM(pathItemElement, {
28-
parse: { mediaType: mediaTypes.latest('json') },
29-
resolve: { baseURI: fixturePath },
21+
specify('should dereference', async function () {
22+
const parseResult = await parse(fixturePath, {
23+
parse: { mediaType: mediaTypes.latest('json') },
24+
});
25+
const jsonPointer = compile(['paths', '/path1']);
26+
const pathItemElement = evaluate(jsonPointer, parseResult.api as OpenApi3_1Element);
27+
const dereferenced = await dereferenceApiDOM(pathItemElement, {
28+
parse: { mediaType: mediaTypes.latest('json') },
29+
resolve: { baseURI: `${fixturePath}#${jsonPointer}` },
30+
});
31+
32+
assert.isTrue(isPathItemElement(dereferenced));
3033
});
3134

32-
assert.isTrue(isPathItemElement(dereferenced));
33-
});
35+
specify('should dereference and contain metadata about origin', async function () {
36+
const jsonPointer = compile(['paths', '/path1']);
37+
const parseResult = await parse(fixturePath, {
38+
parse: { mediaType: mediaTypes.latest('json') },
39+
});
40+
const pathItemElement = evaluate(jsonPointer, parseResult.api as OpenApi3_1Element);
41+
const dereferenced = await dereferenceApiDOM(pathItemElement, {
42+
parse: { mediaType: mediaTypes.latest('json') },
43+
resolve: { baseURI: `${fixturePath}#${jsonPointer}` },
44+
});
3445

35-
specify('should dereference and contain metadata about origin', async function () {
36-
const parseResult = await parse(fixturePath, {
37-
parse: { mediaType: mediaTypes.latest('json') },
46+
assert.match(
47+
dereferenced.meta.get('ref-origin').toValue(),
48+
/internal-only\/root\.json$/,
49+
);
3850
});
39-
const pathItemElement = evaluate(
40-
compile(['paths', '/path1']),
41-
parseResult.api as OpenApi3_1Element,
42-
);
43-
const dereferenced = await dereferenceApiDOM(pathItemElement, {
44-
parse: { mediaType: mediaTypes.latest('json') },
45-
resolve: { baseURI: fixturePath },
51+
},
52+
);
53+
54+
context(
55+
'given single PathItemElement passed to dereferenceApiDOM with external references',
56+
function () {
57+
const fixturePath = path.join(__dirname, 'fixtures', 'external-only', 'root.json');
58+
59+
specify('should dereference', async function () {
60+
const parseResult = await parse(fixturePath, {
61+
parse: { mediaType: mediaTypes.latest('json') },
62+
});
63+
const pathItemElement = evaluate(
64+
compile(['paths', '/path1']),
65+
parseResult.api as OpenApi3_1Element,
66+
);
67+
const dereferenced = await dereferenceApiDOM(pathItemElement, {
68+
parse: { mediaType: mediaTypes.latest('json') },
69+
resolve: { baseURI: fixturePath },
70+
});
71+
72+
assert.isTrue(isPathItemElement(dereferenced));
4673
});
4774

48-
assert.match(dereferenced.meta.get('ref-origin').toValue(), /external-only\/ex\.json$/);
49-
});
50-
});
75+
specify('should dereference and contain metadata about origin', async function () {
76+
const parseResult = await parse(fixturePath, {
77+
parse: { mediaType: mediaTypes.latest('json') },
78+
});
79+
const pathItemElement = evaluate(
80+
compile(['paths', '/path1']),
81+
parseResult.api as OpenApi3_1Element,
82+
);
83+
const dereferenced = await dereferenceApiDOM(pathItemElement, {
84+
parse: { mediaType: mediaTypes.latest('json') },
85+
resolve: { baseURI: fixturePath },
86+
});
87+
88+
assert.match(
89+
dereferenced.meta.get('ref-origin').toValue(),
90+
/external-only\/ex\.json$/,
91+
);
92+
});
93+
},
94+
);
5195
});
5296
});
5397
});

0 commit comments

Comments
 (0)