Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(reference): support native fragment deref/resolve - Schema Object #2941

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ const OpenApi3_1DereferenceVisitor = stampit({
}

// compute baseURI using rules around $id and $ref keywords
let { reference } = this;
let reference = await this.toReference(url.unsanitize(this.reference.uri));
let { uri: retrievalURI } = reference;
const $refBaseURI = resolveSchema$refField(retrievalURI, referencingElement) as string;
const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const OpenApi3_1ResolveVisitor = stampit({
return undefined;
},

SchemaElement(schemaElement: SchemaElement) {
async SchemaElement(schemaElement: SchemaElement) {
/**
* Skip traversal for already visited schemas and all their child schemas.
* visit function detects cycles in path automatically.
Expand All @@ -206,13 +206,14 @@ const OpenApi3_1ResolveVisitor = stampit({
}

// compute baseURI using rules around $id and $ref keywords
const retrievalURI = this.reference.uri;
const reference = await this.toReference(this.reference.uri);
const { uri: retrievalURI } = reference;
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);
const isURL = !isUnknownURI;
const isExternal = !isUnknownURI && this.reference.uri !== $refBaseURIStrippedHash;
const isExternal = !isUnknownURI && retrievalURI !== $refBaseURIStrippedHash;

// ignore resolving external Reference Objects
if (!this.options.resolve.external && isExternal) {
Expand All @@ -225,7 +226,7 @@ const OpenApi3_1ResolveVisitor = stampit({
if (!has($refBaseURIStrippedHash, this.crawlingMap)) {
try {
if (isUnknownURI || isURL) {
this.crawlingMap[$refBaseURIStrippedHash] = this.reference;
this.crawlingMap[$refBaseURIStrippedHash] = reference;
} else {
this.crawlingMap[$refBaseURIStrippedHash] = this.toReference(
url.unsanitize($refBaseURI),
Expand Down Expand Up @@ -340,7 +341,8 @@ const OpenApi3_1ResolveVisitor = stampit({

async crawlSchemaElement(referencingElement: SchemaElement) {
// compute baseURI using rules around $id and $ref keywords
const retrievalURI = this.reference.uri;
let reference = await this.toReference(url.unsanitize(this.reference.uri));
const { uri: retrievalURI } = reference;
const $refBaseURI = resolveSchema$refField(retrievalURI, referencingElement) as string;
const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
const file = File({ uri: $refBaseURIStrippedHash });
Expand All @@ -350,13 +352,11 @@ const OpenApi3_1ResolveVisitor = stampit({
this.indirections.push(referencingElement);

// determining reference, proper evaluation and selection mechanism
let reference: IReference;
let referencedElement;

try {
if (isUnknownURI || isURL) {
// we're dealing with canonical URI or URL with possible fragment
reference = this.reference;
const selector = $refBaseURI;
referencedElement = uriEvaluate(
selector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,105 @@ import { assert } from 'chai';
import { mediaTypes, isSchemaElement, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
import { evaluate } from '@swagger-api/apidom-json-pointer';

import { parse, dereferenceApiDOM } from '../../../../../src';
import { parse, dereferenceApiDOM, Reference, ReferenceSet } from '../../../../../src';

describe('dereference', function () {
context('strategies', function () {
context('openapi-3-1', function () {
context('Schema Object', function () {
context('given single SchemaElement passed to dereferenceApiDOM', function () {
const fixturePath = path.join(__dirname, 'fixtures', 'external-only', 'root.json');
context(
'given single SchemaElement passed to dereferenceApiDOM with internal references',
function () {
const fixturePath = path.join(__dirname, 'fixtures', 'internal-only', 'root.json');

specify('should dereference', async function () {
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const schemaElement = evaluate(
'/components/schemas/User/properties/profile',
parseResult.api as OpenApi3_1Element,
);
const dereferenced = await dereferenceApiDOM(schemaElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: fixturePath },
specify('should dereference', async function () {
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const schemaElement = evaluate(
'/components/schemas/User/properties/profile',
parseResult.api as OpenApi3_1Element,
);
const reference = Reference({ uri: fixturePath, parseResult });
const refSet = ReferenceSet({ refs: [reference] });
// @ts-ignore
refSet.rootRef = null;

const dereferenced = await dereferenceApiDOM(schemaElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: `${fixturePath}#/components/schemas/User/properties/profile` },
});

assert.isTrue(isSchemaElement(dereferenced));
});

assert.isTrue(isSchemaElement(dereferenced));
});
specify('should dereference and contain metadata about origin', async function () {
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const schemaElement = evaluate(
'/components/schemas/User/properties/profile',
parseResult.api as OpenApi3_1Element,
);
const reference = Reference({ uri: fixturePath, parseResult });
const refSet = ReferenceSet({ refs: [reference] });
// @ts-ignore
refSet.rootRef = null;

specify('should dereference and contain metadata about origin', async function () {
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
const dereferenced = await dereferenceApiDOM(schemaElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: `${fixturePath}#/components/schemas/User/properties/profile` },
});

assert.match(
dereferenced.meta.get('ref-origin').toValue(),
/internal-only\/root\.json$/,
);
});
const pathItemElement = evaluate(
'/components/schemas/User/properties/profile',
parseResult.api as OpenApi3_1Element,
);
const dereferenced = await dereferenceApiDOM(pathItemElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: fixturePath },
},
);

context(
'given single SchemaElement passed to dereferenceApiDOM with external references',
function () {
const fixturePath = path.join(__dirname, 'fixtures', 'external-only', 'root.json');

specify('should dereference', async function () {
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const schemaElement = evaluate(
'/components/schemas/User/properties/profile',
parseResult.api as OpenApi3_1Element,
);
const dereferenced = await dereferenceApiDOM(schemaElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: fixturePath },
});

assert.isTrue(isSchemaElement(dereferenced));
});

assert.match(dereferenced.meta.get('ref-origin').toValue(), /external-only\/ex\.json$/);
});
});
specify('should dereference and contain metadata about origin', async function () {
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const pathItemElement = evaluate(
'/components/schemas/User/properties/profile',
parseResult.api as OpenApi3_1Element,
);
const dereferenced = await dereferenceApiDOM(pathItemElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: fixturePath },
});

assert.match(
dereferenced.meta.get('ref-origin').toValue(),
/external-only\/ex\.json$/,
);
});
},
);
});
});
});
Expand Down