Skip to content

Commit

Permalink
test(reference): provide new OpenAPI 3.1.0 dereference test cases (#2940
Browse files Browse the repository at this point in the history
)
  • Loading branch information
char0n authored Jul 13, 2023
1 parent ee86f53 commit 44acf6c
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SchemaElement } from '@swagger-api/apidom-ns-openapi-3-1';

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

export const resolveSchema$refField = (retrieveURI: string, schemaElement: SchemaElement) => {
export const resolveSchema$refField = (retrievalURI: string, schemaElement: SchemaElement) => {
if (typeof schemaElement.$ref === 'undefined') {
return undefined;
}
Expand All @@ -15,14 +15,14 @@ export const resolveSchema$refField = (retrieveURI: string, schemaElement: Schem
(acc: string, uri: string): string => {
return url.resolve(acc, url.sanitize(url.stripHash(uri)));
},
retrieveURI,
retrievalURI,
[...inherited$id, schemaElement.$ref.toValue()],
);

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

export const resolveSchema$idField = (retrieveURI: string, schemaElement: SchemaElement) => {
export const resolveSchema$idField = (retrievalURI: string, schemaElement: SchemaElement) => {
if (typeof schemaElement.$id === 'undefined') {
return undefined;
}
Expand All @@ -33,7 +33,7 @@ export const resolveSchema$idField = (retrieveURI: string, schemaElement: Schema
(acc: string, $id: string): string => {
return url.resolve(acc, url.sanitize(url.stripHash($id)));
},
retrieveURI,
retrievalURI,
[...inherited$id, schemaElement.$id.toValue()],
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ const OpenApi3_1ResolveVisitor = stampit({
}

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

async crawlSchemaElement(referencingElement: SchemaElement) {
// compute baseURI using rules around $id and $ref keywords
const retrieveURI = this.reference.uri;
const $refBaseURI = resolveSchema$refField(retrieveURI, referencingElement) as string;
const retrievalURI = this.reference.uri;
const $refBaseURI = resolveSchema$refField(retrievalURI, referencingElement) as string;
const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
const file = File({ uri: $refBaseURIStrippedHash });
const isUnknownURI = none((r: IResolver) => r.canRead(file), this.options.resolve.resolvers);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"$id": "/components/schemas/user",
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$id": "/components/schemas/user-profile",
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
},
"UserProfile": {
"$id": "/components/schemas/user-profile",
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"$id": "/components/schemas/user",
"type": "object",
"properties": {
"login": {
"type": "string"
},
"password": {
"type": "string"
},
"profile": {
"$ref": "/components/schemas/user-profile#user-profile"
}
}
},
"UserProfile": {
"$id": "/components/schemas/user-profile",
"$anchor": "user-profile",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object"
},
"UserProfile": {
"$id": "/components/schemas/user-profile",
"type": "object",
"properties": {
"user": {
"type": "object"
}
}
}
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"openapi": "3.1.0",
"components": {
"schemas": {
"User": {
"type": "object"
},
"UserProfile": {
"$id": "/components/schemas/user-profile",
"type": "object",
"properties": {
"user": {
"$ref": "/#/components/schemas/User"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { toValue } from '@swagger-api/apidom-core';
import { isSchemaElement, mediaTypes } from '@swagger-api/apidom-ns-openapi-3-1';
import { evaluate } from '@swagger-api/apidom-json-pointer';

import { dereference } from '../../../../../src';
import { dereference, parse, Reference, ReferenceSet } from '../../../../../src';
import {
DereferenceError,
MaximumDereferenceDepthError,
Expand Down Expand Up @@ -493,6 +493,29 @@ describe('dereference', function () {
});
});

context('given Schema Objects with $ref keyword containing URL path override', function () {
const fixturePath = path.join(rootFixturePath, '$ref-url-path-override');

specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const parseResult = await parse(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const uri = 'https://example.com/';
const reference = Reference({ uri, value: parseResult });
const refSet = ReferenceSet({ refs: [reference] });

const actual = await dereference(uri, {
dereference: { refSet },
parse: { mediaType: mediaTypes.latest('json') },
});

const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));

assert.deepEqual(toValue(actual), expected);
});
});

context('given Schema Objects with $ref keyword containing resolvable URL', function () {
const fixturePath = path.join(rootFixturePath, '$ref-url-resolvable');

Expand Down Expand Up @@ -612,6 +635,32 @@ describe('dereference', function () {
},
);

context(
'given Schema Objects with $anchor keyword after $id pointing to internal schema',
function () {
const fixturePath = path.join(rootFixturePath, '$anchor-internal-no-embedding');

specify('should dereference', async function () {
const rootFilePath = path.join(fixturePath, 'root.json');
const parseResult = await parse(rootFilePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const uri = 'https://example.com/';
const reference = Reference({ uri, value: parseResult });
const refSet = ReferenceSet({ refs: [reference] });

const actual = await dereference(uri, {
dereference: { refSet },
parse: { mediaType: mediaTypes.latest('json') },
});

const expected = loadJsonFile(path.join(fixturePath, 'dereferenced.json'));

assert.deepEqual(toValue(actual), expected);
});
},
);

context(
'given Schema Objects with $anchor keyword pointing to external schema',
function () {
Expand Down

0 comments on commit 44acf6c

Please sign in to comment.