Skip to content

Commit

Permalink
feat(reference): skip processing internal refererences in OpenAPI 2.0…
Browse files Browse the repository at this point in the history
… resolve strategy (#3903)

Refs #3451
  • Loading branch information
char0n committed Mar 7, 2024
1 parent 0762e8b commit dc787bf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
Expand Up @@ -138,9 +138,16 @@ const OpenApi2DereferenceVisitor = stampit({
}

const retrievalURI = this.toBaseURI(toValue(referencingElement.$ref));
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
const isExternalReference = !isInternalReference;

// ignore resolving internal Reference Objects
if (!this.options.resolve.internal && isInternalReference) {
// skip traversing this reference element and all it's child elements
return false;
}
// ignore resolving external Reference Objects
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== retrievalURI) {
if (!this.options.resolve.external && isExternalReference) {
// skip traversing this reference element and all it's child elements
return false;
}
Expand Down Expand Up @@ -268,9 +275,16 @@ const OpenApi2DereferenceVisitor = stampit({
}

const retrievalURI = this.toBaseURI(toValue(referencingElement.$ref));
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
const isExternalReference = !isInternalReference;

// ignore resolving internal Path Item Objects
if (!this.options.resolve.internal && isInternalReference) {
// skip traversing this Path Item element but traverse all it's child elements
return undefined;
}
// ignore resolving external Path Item Objects
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== retrievalURI) {
if (!this.options.resolve.external && isExternalReference) {
// skip traversing this Path Item element but traverse all it's child elements
return undefined;
}
Expand Down Expand Up @@ -397,9 +411,16 @@ const OpenApi2DereferenceVisitor = stampit({
}

const retrievalURI = this.toBaseURI(toValue(referencingElement.$ref));
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
const isExternalReference = !isInternalReference;

// ignore resolving internal JSONReference Objects
if (!this.options.resolve.internal && isInternalReference) {
// skip traversing this JSONReference element and all it's child elements
return false;
}
// ignore resolving external JSONReference Objects
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== retrievalURI) {
if (!this.options.resolve.external && isExternalReference) {
// skip traversing this JSONReference element and all it's child elements
return false;
}
Expand Down
Expand Up @@ -16,6 +16,7 @@ import {
import ReferenceSet from '../../../ReferenceSet';
import Reference from '../../../Reference';
import OpenApi2ResolveVisitor from './visitor';
import { merge as mergeOptions } from '../../../options/util';

// @ts-ignore
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];
Expand All @@ -38,7 +39,8 @@ const OpenApi2ResolveStrategy: stampit.Stamp<IResolveStrategy> = stampit(Resolve
async resolve(file: IFile, options: IReferenceOptions) {
const namespace = createNamespace(openapi2Namespace);
const reference = Reference({ uri: file.uri, value: file.parseResult });
const visitor = OpenApi2ResolveVisitor({ reference, namespace, options });
const mergedOptions = mergeOptions(options, { resolve: { internal: false } });
const visitor = OpenApi2ResolveVisitor({ reference, namespace, options: mergedOptions });
const refSet = ReferenceSet();
refSet.add(reference);

Expand Down
@@ -0,0 +1 @@
{}
Expand Up @@ -2,7 +2,7 @@
"swagger": "2.0",
"paths": {
"/path1": {
"$ref": "#/paths/invalid-pointer"
"$ref": "./ex.json#/paths/invalid-pointer"
},
"/path2": {
"get": {}
Expand Down

0 comments on commit dc787bf

Please sign in to comment.