Skip to content

Commit 3cc0791

Browse files
authored
fix(reference): fix internal/external URL determination for AsyncAPI 2.x (#3453)
Refs #3451
1 parent 6dbd998 commit 3cc0791

File tree

4 files changed

+23
-61
lines changed

4 files changed

+23
-61
lines changed

packages/apidom-ns-asyncapi-2/src/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export {
2323
isAsyncApiVersionElement,
2424
isChannelBindingsElement,
2525
isChannelItemElement,
26-
isChannelItemElementExternal,
2726
isChannelsElement,
2827
isComponentsElement,
2928
isContactElement,
@@ -34,7 +33,6 @@ export {
3433
isParameterElement,
3534
isParametersElement,
3635
isReferenceElement,
37-
isReferenceElementExternal,
3836
isSchemaElement,
3937
isBooleanJsonSchemaElement,
4038
isSecurityRequirementElement,

packages/apidom-ns-asyncapi-2/src/predicates.ts

+1-34
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
BooleanElement,
3-
createPredicate,
4-
isBooleanElement,
5-
isStringElement,
6-
toValue,
7-
} from '@swagger-api/apidom-core';
1+
import { BooleanElement, createPredicate, isBooleanElement } from '@swagger-api/apidom-core';
82
import type { ElementPredicate } from '@swagger-api/apidom-core';
93

104
import AsyncApi2Element from './elements/AsyncApi2';
@@ -69,21 +63,6 @@ export const isChannelItemElement = createPredicate(
6963
},
7064
);
7165

72-
export const isChannelItemElementExternal: ElementPredicate<ChannelItemElement> = (
73-
element: unknown,
74-
): element is ChannelItemElement => {
75-
if (!isChannelItemElement(element)) {
76-
return false;
77-
}
78-
if (!isStringElement(element.$ref)) {
79-
return false;
80-
}
81-
82-
const value = toValue(element.$ref);
83-
84-
return typeof value === 'string' && value.length > 0 && !value.startsWith('#');
85-
};
86-
8766
export const isChannelsElement = createPredicate(
8867
({ hasBasicElementProps, isElementType, primitiveEq }) => {
8968
return (element: unknown): element is ChannelsElement =>
@@ -184,18 +163,6 @@ export const isReferenceElement = createPredicate(
184163
},
185164
);
186165

187-
export const isReferenceElementExternal: ElementPredicate<ReferenceElement> = (
188-
element: unknown,
189-
): element is ReferenceElement => {
190-
if (!isReferenceElement(element)) {
191-
return false;
192-
}
193-
194-
const value = toValue(element.$ref);
195-
196-
return typeof value === 'string' && value.length > 0 && !value.startsWith('#');
197-
};
198-
199166
export const isSchemaElement = createPredicate(
200167
({ hasBasicElementProps, isElementType, primitiveEq }) => {
201168
return (element: unknown): element is SchemaElement =>

packages/apidom-reference/src/dereference/strategies/asyncapi-2/visitor.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import { evaluate, uriToPointer } from '@swagger-api/apidom-json-pointer';
1717
import {
1818
ChannelItemElement,
1919
getNodeType,
20-
isChannelItemElementExternal,
21-
isReferenceElementExternal,
2220
isReferenceLikeElement,
2321
isBooleanJsonSchemaElement,
2422
keyMap,
@@ -126,16 +124,16 @@ const AsyncApi2DereferenceVisitor = stampit({
126124
return false;
127125
}
128126

129-
// ignore resolving external Reference Objects
130-
if (!this.options.resolve.external && isReferenceElementExternal(referencingElement)) {
131-
// skip traversing this schema but traverse all it's child schemas
132-
return undefined;
133-
}
134-
135127
const reference = await this.toReference(toValue(referencingElement.$ref));
136128
const { uri: retrievalURI } = reference;
137129
const $refBaseURI = url.resolve(retrievalURI, toValue(referencingElement.$ref));
138130

131+
// ignore resolving external Reference Objects
132+
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== retrievalURI) {
133+
// skip traversing this reference element but traverse all it's child elements
134+
return undefined;
135+
}
136+
139137
this.indirections.push(referencingElement);
140138

141139
const jsonPointer = uriToPointer($refBaseURI);
@@ -266,15 +264,16 @@ const AsyncApi2DereferenceVisitor = stampit({
266264
return false;
267265
}
268266

269-
// ignore resolving external ChannelItem Elements
270-
if (!this.options.resolve.external && isChannelItemElementExternal(referencingElement)) {
271-
return undefined;
272-
}
273-
274267
const reference = await this.toReference(toValue(referencingElement.$ref));
275268
const retrievalURI = reference.uri;
276269
const $refBaseURI = url.resolve(retrievalURI, toValue(referencingElement.$ref));
277270

271+
// ignore resolving external Channel Item Objects
272+
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== retrievalURI) {
273+
// skip traversing this channel item but traverse all it's child elements
274+
return undefined;
275+
}
276+
278277
this.indirections.push(referencingElement);
279278

280279
const jsonPointer = uriToPointer($refBaseURI);

packages/apidom-reference/src/resolve/strategies/asyncapi-2/visitor.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
keyMap,
1313
ReferenceElement,
1414
ChannelItemElement,
15-
isReferenceElementExternal,
16-
isChannelItemElementExternal,
1715
} from '@swagger-api/apidom-ns-asyncapi-2';
1816

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

8482
ReferenceElement(referenceElement: ReferenceElement) {
85-
// ignore resolving external Reference Objects
86-
if (!this.options.resolve.external && isReferenceElementExternal(referenceElement)) {
87-
return false;
88-
}
89-
9083
const uri = toValue(referenceElement.$ref);
9184
const baseURI = this.toBaseURI(uri);
9285

86+
// // ignore resolving external Reference Objects
87+
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== baseURI) {
88+
return false;
89+
}
90+
9391
if (!has(baseURI, this.crawlingMap)) {
9492
this.crawlingMap[baseURI] = this.toReference(uri);
9593
}
@@ -104,14 +102,14 @@ const AsyncApi2ResolveVisitor = stampit({
104102
return undefined;
105103
}
106104

107-
// ignore resolving external Reference Objects
108-
if (!this.options.resolve.external && isChannelItemElementExternal(channelItemElement)) {
109-
return undefined;
110-
}
111-
112105
const uri = toValue(channelItemElement.$ref);
113106
const baseURI = this.toBaseURI(uri);
114107

108+
// ignore resolving external Channel Item Objects
109+
if (!this.options.resolve.external && url.stripHash(this.reference.uri) !== baseURI) {
110+
return undefined;
111+
}
112+
115113
if (!has(baseURI, this.crawlingMap)) {
116114
this.crawlingMap[baseURI] = this.toReference(uri);
117115
}

0 commit comments

Comments
 (0)