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

fix(reference): fix internal/external URL determination for AsyncAPI 2.x #3453

Merged
merged 1 commit into from
Nov 24, 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
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