Skip to content

Commit

Permalink
fix(reference): fix internal/external URL determination for AsyncAPI …
Browse files Browse the repository at this point in the history
…2.x (#3453)

Refs #3451
  • Loading branch information
char0n authored Nov 24, 2023
1 parent 6dbd998 commit 3cc0791
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 61 deletions.
2 changes: 0 additions & 2 deletions packages/apidom-ns-asyncapi-2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export {
isAsyncApiVersionElement,
isChannelBindingsElement,
isChannelItemElement,
isChannelItemElementExternal,
isChannelsElement,
isComponentsElement,
isContactElement,
Expand All @@ -34,7 +33,6 @@ export {
isParameterElement,
isParametersElement,
isReferenceElement,
isReferenceElementExternal,
isSchemaElement,
isBooleanJsonSchemaElement,
isSecurityRequirementElement,
Expand Down
35 changes: 1 addition & 34 deletions packages/apidom-ns-asyncapi-2/src/predicates.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
BooleanElement,
createPredicate,
isBooleanElement,
isStringElement,
toValue,
} from '@swagger-api/apidom-core';
import { BooleanElement, createPredicate, isBooleanElement } from '@swagger-api/apidom-core';
import type { ElementPredicate } from '@swagger-api/apidom-core';

import AsyncApi2Element from './elements/AsyncApi2';
Expand Down Expand Up @@ -69,21 +63,6 @@ export const isChannelItemElement = createPredicate(
},
);

export const isChannelItemElementExternal: ElementPredicate<ChannelItemElement> = (
element: unknown,
): element is ChannelItemElement => {
if (!isChannelItemElement(element)) {
return false;
}
if (!isStringElement(element.$ref)) {
return false;
}

const value = toValue(element.$ref);

return typeof value === 'string' && value.length > 0 && !value.startsWith('#');
};

export const isChannelsElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is ChannelsElement =>
Expand Down Expand Up @@ -184,18 +163,6 @@ export const isReferenceElement = createPredicate(
},
);

export const isReferenceElementExternal: ElementPredicate<ReferenceElement> = (
element: unknown,
): element is ReferenceElement => {
if (!isReferenceElement(element)) {
return false;
}

const value = toValue(element.$ref);

return typeof value === 'string' && value.length > 0 && !value.startsWith('#');
};

export const isSchemaElement = createPredicate(
({ hasBasicElementProps, isElementType, primitiveEq }) => {
return (element: unknown): element is SchemaElement =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import { evaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
import {
ChannelItemElement,
getNodeType,
isChannelItemElementExternal,
isReferenceElementExternal,
isReferenceLikeElement,
isBooleanJsonSchemaElement,
keyMap,
Expand Down Expand Up @@ -126,16 +124,16 @@ const AsyncApi2DereferenceVisitor = stampit({
return false;
}

// ignore resolving external Reference Objects
if (!this.options.resolve.external && isReferenceElementExternal(referencingElement)) {
// skip traversing this schema but traverse all it's child schemas
return undefined;
}

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

// ignore resolving external Reference Objects
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== retrievalURI) {
// skip traversing this reference element but traverse all it's child elements
return undefined;
}

this.indirections.push(referencingElement);

const jsonPointer = uriToPointer($refBaseURI);
Expand Down Expand Up @@ -266,15 +264,16 @@ const AsyncApi2DereferenceVisitor = stampit({
return false;
}

// ignore resolving external ChannelItem Elements
if (!this.options.resolve.external && isChannelItemElementExternal(referencingElement)) {
return undefined;
}

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

// ignore resolving external Channel Item Objects
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== retrievalURI) {
// skip traversing this channel item but traverse all it's child elements
return undefined;
}

this.indirections.push(referencingElement);

const jsonPointer = uriToPointer($refBaseURI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
keyMap,
ReferenceElement,
ChannelItemElement,
isReferenceElementExternal,
isChannelItemElementExternal,
} from '@swagger-api/apidom-ns-asyncapi-2';

import { Reference as IReference } from '../../../types';
Expand Down Expand Up @@ -82,14 +80,14 @@ const AsyncApi2ResolveVisitor = stampit({
},

ReferenceElement(referenceElement: ReferenceElement) {
// ignore resolving external Reference Objects
if (!this.options.resolve.external && isReferenceElementExternal(referenceElement)) {
return false;
}

const uri = toValue(referenceElement.$ref);
const baseURI = this.toBaseURI(uri);

// // ignore resolving external Reference Objects
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== baseURI) {
return false;
}

if (!has(baseURI, this.crawlingMap)) {
this.crawlingMap[baseURI] = this.toReference(uri);
}
Expand All @@ -104,14 +102,14 @@ const AsyncApi2ResolveVisitor = stampit({
return undefined;
}

// ignore resolving external Reference Objects
if (!this.options.resolve.external && isChannelItemElementExternal(channelItemElement)) {
return undefined;
}

const uri = toValue(channelItemElement.$ref);
const baseURI = this.toBaseURI(uri);

// ignore resolving external Channel Item Objects
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== baseURI) {
return undefined;
}

if (!has(baseURI, this.crawlingMap)) {
this.crawlingMap[baseURI] = this.toReference(uri);
}
Expand Down

0 comments on commit 3cc0791

Please sign in to comment.