Skip to content

Commit 44acf6c

Browse files
authored
test(reference): provide new OpenAPI 3.1.0 dereference test cases (#2940)
1 parent ee86f53 commit 44acf6c

File tree

7 files changed

+177
-9
lines changed

7 files changed

+177
-9
lines changed

packages/apidom-reference/src/resolve/strategies/openapi-3-1/util.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { SchemaElement } from '@swagger-api/apidom-ns-openapi-3-1';
44

55
import * as url from '../../../util/url';
66

7-
export const resolveSchema$refField = (retrieveURI: string, schemaElement: SchemaElement) => {
7+
export const resolveSchema$refField = (retrievalURI: string, schemaElement: SchemaElement) => {
88
if (typeof schemaElement.$ref === 'undefined') {
99
return undefined;
1010
}
@@ -15,14 +15,14 @@ export const resolveSchema$refField = (retrieveURI: string, schemaElement: Schem
1515
(acc: string, uri: string): string => {
1616
return url.resolve(acc, url.sanitize(url.stripHash(uri)));
1717
},
18-
retrieveURI,
18+
retrievalURI,
1919
[...inherited$id, schemaElement.$ref.toValue()],
2020
);
2121

2222
return `${$refBaseURI}${hash === '#' ? '' : hash}`;
2323
};
2424

25-
export const resolveSchema$idField = (retrieveURI: string, schemaElement: SchemaElement) => {
25+
export const resolveSchema$idField = (retrievalURI: string, schemaElement: SchemaElement) => {
2626
if (typeof schemaElement.$id === 'undefined') {
2727
return undefined;
2828
}
@@ -33,7 +33,7 @@ export const resolveSchema$idField = (retrieveURI: string, schemaElement: Schema
3333
(acc: string, $id: string): string => {
3434
return url.resolve(acc, url.sanitize(url.stripHash($id)));
3535
},
36-
retrieveURI,
36+
retrievalURI,
3737
[...inherited$id, schemaElement.$id.toValue()],
3838
);
3939
};

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ const OpenApi3_1ResolveVisitor = stampit({
206206
}
207207

208208
// compute baseURI using rules around $id and $ref keywords
209-
const retrieveURI = this.reference.uri;
210-
const $refBaseURI = resolveSchema$refField(retrieveURI, schemaElement) as string;
209+
const retrievalURI = this.reference.uri;
210+
const $refBaseURI = resolveSchema$refField(retrievalURI, schemaElement) as string;
211211
const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
212212
const file = File({ uri: $refBaseURIStrippedHash });
213213
const isUnknownURI = none((r: IResolver) => r.canRead(file), this.options.resolve.resolvers);
@@ -340,8 +340,8 @@ const OpenApi3_1ResolveVisitor = stampit({
340340

341341
async crawlSchemaElement(referencingElement: SchemaElement) {
342342
// compute baseURI using rules around $id and $ref keywords
343-
const retrieveURI = this.reference.uri;
344-
const $refBaseURI = resolveSchema$refField(retrieveURI, referencingElement) as string;
343+
const retrievalURI = this.reference.uri;
344+
const $refBaseURI = resolveSchema$refField(retrievalURI, referencingElement) as string;
345345
const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
346346
const file = File({ uri: $refBaseURIStrippedHash });
347347
const isUnknownURI = none((r: IResolver) => r.canRead(file), this.options.resolve.resolvers);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[
2+
{
3+
"openapi": "3.1.0",
4+
"components": {
5+
"schemas": {
6+
"User": {
7+
"$id": "/components/schemas/user",
8+
"type": "object",
9+
"properties": {
10+
"login": {
11+
"type": "string"
12+
},
13+
"password": {
14+
"type": "string"
15+
},
16+
"profile": {
17+
"$id": "/components/schemas/user-profile",
18+
"$anchor": "user-profile",
19+
"properties": {
20+
"firstName": {
21+
"type": "string"
22+
},
23+
"lastName": {
24+
"type": "string"
25+
}
26+
}
27+
}
28+
}
29+
},
30+
"UserProfile": {
31+
"$id": "/components/schemas/user-profile",
32+
"$anchor": "user-profile",
33+
"properties": {
34+
"firstName": {
35+
"type": "string"
36+
},
37+
"lastName": {
38+
"type": "string"
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"openapi": "3.1.0",
3+
"components": {
4+
"schemas": {
5+
"User": {
6+
"$id": "/components/schemas/user",
7+
"type": "object",
8+
"properties": {
9+
"login": {
10+
"type": "string"
11+
},
12+
"password": {
13+
"type": "string"
14+
},
15+
"profile": {
16+
"$ref": "/components/schemas/user-profile#user-profile"
17+
}
18+
}
19+
},
20+
"UserProfile": {
21+
"$id": "/components/schemas/user-profile",
22+
"$anchor": "user-profile",
23+
"properties": {
24+
"firstName": {
25+
"type": "string"
26+
},
27+
"lastName": {
28+
"type": "string"
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"openapi": "3.1.0",
4+
"components": {
5+
"schemas": {
6+
"User": {
7+
"type": "object"
8+
},
9+
"UserProfile": {
10+
"$id": "/components/schemas/user-profile",
11+
"type": "object",
12+
"properties": {
13+
"user": {
14+
"type": "object"
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
21+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"openapi": "3.1.0",
3+
"components": {
4+
"schemas": {
5+
"User": {
6+
"type": "object"
7+
},
8+
"UserProfile": {
9+
"$id": "/components/schemas/user-profile",
10+
"type": "object",
11+
"properties": {
12+
"user": {
13+
"$ref": "/#/components/schemas/User"
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}

packages/apidom-reference/test/dereference/strategies/openapi-3-1/schema-object/index.ts

+50-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { toValue } from '@swagger-api/apidom-core';
44
import { isSchemaElement, mediaTypes } from '@swagger-api/apidom-ns-openapi-3-1';
55
import { evaluate } from '@swagger-api/apidom-json-pointer';
66

7-
import { dereference } from '../../../../../src';
7+
import { dereference, parse, Reference, ReferenceSet } from '../../../../../src';
88
import {
99
DereferenceError,
1010
MaximumDereferenceDepthError,
@@ -493,6 +493,29 @@ describe('dereference', function () {
493493
});
494494
});
495495

496+
context('given Schema Objects with $ref keyword containing URL path override', function () {
497+
const fixturePath = path.join(rootFixturePath, '$ref-url-path-override');
498+
499+
specify('should dereference', async function () {
500+
const rootFilePath = path.join(fixturePath, 'root.json');
501+
const parseResult = await parse(rootFilePath, {
502+
parse: { mediaType: mediaTypes.latest('json') },
503+
});
504+
const uri = 'https://example.com/';
505+
const reference = Reference({ uri, value: parseResult });
506+
const refSet = ReferenceSet({ refs: [reference] });
507+
508+
const actual = await dereference(uri, {
509+
dereference: { refSet },
510+
parse: { mediaType: mediaTypes.latest('json') },
511+
});
512+
513+
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
514+
515+
assert.deepEqual(toValue(actual), expected);
516+
});
517+
});
518+
496519
context('given Schema Objects with $ref keyword containing resolvable URL', function () {
497520
const fixturePath = path.join(rootFixturePath, '$ref-url-resolvable');
498521

@@ -612,6 +635,32 @@ describe('dereference', function () {
612635
},
613636
);
614637

638+
context(
639+
'given Schema Objects with $anchor keyword after $id pointing to internal schema',
640+
function () {
641+
const fixturePath = path.join(rootFixturePath, '$anchor-internal-no-embedding');
642+
643+
specify('should dereference', async function () {
644+
const rootFilePath = path.join(fixturePath, 'root.json');
645+
const parseResult = await parse(rootFilePath, {
646+
parse: { mediaType: mediaTypes.latest('json') },
647+
});
648+
const uri = 'https://example.com/';
649+
const reference = Reference({ uri, value: parseResult });
650+
const refSet = ReferenceSet({ refs: [reference] });
651+
652+
const actual = await dereference(uri, {
653+
dereference: { refSet },
654+
parse: { mediaType: mediaTypes.latest('json') },
655+
});
656+
657+
const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));
658+
659+
assert.deepEqual(toValue(actual), expected);
660+
});
661+
},
662+
);
663+
615664
context(
616665
'given Schema Objects with $anchor keyword pointing to external schema',
617666
function () {

0 commit comments

Comments
 (0)