Skip to content

Commit

Permalink
feat(reference): support native fragment - OpenAPI 3.0.x
Browse files Browse the repository at this point in the history
Refs #2934
  • Loading branch information
char0n committed Jul 13, 2023
1 parent d7f82ee commit f3cffda
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const OpenApi3_0DereferenceVisitor = stampit({
}

const reference = await this.toReference(referencingElement.$ref?.toValue());
const retrievalURI = reference.uri;
const { uri: retrievalURI } = reference;
const $refBaseURI = url.resolve(retrievalURI, referencingElement.$ref?.toValue());

this.indirections.push(referencingElement);
Expand Down Expand Up @@ -335,9 +335,10 @@ const OpenApi3_0DereferenceVisitor = stampit({
linkElement.operationRef?.meta.set('operation', operationElement);
} else if (isStringElement(linkElement.operationId)) {
const operationId = linkElement.operationId?.toValue();
const reference = await this.toReference(url.unsanitize(this.reference.uri));
operationElement = find(
(e) => isOperationElement(e) && e.operationId.equals(operationId),
this.reference.value.result,
reference.value.result,
);
// OperationElement not found by its operationId
if (isUndefined(operationElement)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,10 @@ const OpenApi3_1DereferenceVisitor = stampit({
linkElement.operationRef?.meta.set('operation', operationElement);
} else if (isStringElement(linkElement.operationId)) {
const operationId = linkElement.operationId?.toValue();
const reference = await this.toReference(url.unsanitize(this.reference.uri));
operationElement = find(
(e) => isOperationElement(e) && e.operationId.equals(operationId),
this.reference.value.result,
reference.value.result,
);
// OperationElement not found by its operationId
if (isUndefined(operationElement)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const OpenApi3_1ResolveVisitor = stampit({
}

// compute baseURI using rules around $id and $ref keywords
const reference = await this.toReference(this.reference.uri);
const reference = await this.toReference(url.unsanitize(this.reference.uri));
const { uri: retrievalURI } = reference;
const $refBaseURI = resolveSchema$refField(retrievalURI, schemaElement) as string;
const $refBaseURIStrippedHash = url.stripHash($refBaseURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,85 @@ describe('dereference', function () {
context('strategies', function () {
context('openapi-3-0', function () {
context('Path Item Object', function () {
context('given single PathItemElement passed to dereferenceApiDOM', function () {
const fixturePath = path.join(__dirname, 'fixtures', 'external-only', 'root.json');
context(
'given single PathItemElement 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 pathItemElement = evaluate(
compile(['paths', '/path1']),
parseResult.api as OpenApi3_0Element,
);
const dereferenced = await dereferenceApiDOM(pathItemElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: fixturePath },
specify('should dereference', async function () {
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const jsonPointer = compile(['paths', '/path1']);
const pathItemElement = evaluate(jsonPointer, parseResult.api as OpenApi3_0Element);
const dereferenced = await dereferenceApiDOM(pathItemElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: `${fixturePath}#${jsonPointer}` },
});

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

assert.isTrue(isPathItemElement(dereferenced));
});
specify('should dereference and contain metadata about origin', async function () {
const jsonPointer = compile(['paths', '/path1']);
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
});
const pathItemElement = evaluate(jsonPointer, parseResult.api as OpenApi3_0Element);
const dereferenced = await dereferenceApiDOM(pathItemElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: `${fixturePath}#${jsonPointer}` },
});

specify('should dereference and contain metadata about origin', async function () {
const parseResult = await parse(fixturePath, {
parse: { mediaType: mediaTypes.latest('json') },
assert.match(
dereferenced.meta.get('ref-origin').toValue(),
/internal-only\/root\.json$/,
);
});
const pathItemElement = evaluate(
compile(['paths', '/path1']),
parseResult.api as OpenApi3_0Element,
);
const dereferenced = await dereferenceApiDOM(pathItemElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: fixturePath },
},
);

context(
'given single PathItemElement 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 pathItemElement = evaluate(
compile(['paths', '/path1']),
parseResult.api as OpenApi3_0Element,
);
const dereferenced = await dereferenceApiDOM(pathItemElement, {
parse: { mediaType: mediaTypes.latest('json') },
resolve: { baseURI: fixturePath },
});

assert.isTrue(isPathItemElement(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(
compile(['paths', '/path1']),
parseResult.api as OpenApi3_0Element,
);
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
Loading

0 comments on commit f3cffda

Please sign in to comment.