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

feat(reference): skip processing internal refererences in apidom resolve strategy #3902

Merged
merged 1 commit into from
Mar 7, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ const ApiDOMDereferenceVisitor = stampit({
const refURI = toValue(refElement);
const refNormalizedURI = refURI.includes('#') ? refURI : `#${refURI}`;
const retrievalURI = this.toBaseURI(refNormalizedURI);
const isExternal = url.stripHash(this.reference.uri) !== retrievalURI;
const isInternalReference = url.stripHash(this.reference.uri) === retrievalURI;
const isExternalReference = !isInternalReference;

// ignore resolving external Ref Objects
if (!this.options.resolve.external && isExternal) {
// ignore resolving internal RefElements
if (!this.options.resolve.internal && isInternalReference) {
// skip traversing this ref element
return false;
}
// ignore resolving external RefElements
if (!this.options.resolve.external && isExternalReference) {
// skip traversing this ref element
return false;
}
Expand All @@ -116,7 +122,7 @@ const ApiDOMDereferenceVisitor = stampit({
throw new ApiDOMError('RefElement cannot reference another RefElement');
}

if (isExternal) {
if (isExternalReference) {
// dive deep into the fragment
const visitor = ApiDOMDereferenceVisitor({ reference, options: this.options });
referencedElement = await visitAsync(referencedElement, visitor);
Expand Down
5 changes: 5 additions & 0 deletions packages/apidom-reference/src/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ const defaultOptions: IReferenceOptions = {
* These options are available in resolver strategy `canResolve` and `resolve` methods.
*/
strategyOpts: {},
/**
* Determines whether internal references will be resolved.
* Internal references will simply be ignored.
*/
internal: true,
/**
* Determines whether external references will be resolved.
* If this option is disabled, then none of above resolvers will be called.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import ReferenceSet from '../../../ReferenceSet';
import Reference from '../../../Reference';
import ApiDOMResolveVisitor from './visitor';
import { merge as mergeOptions } from '../../../options/util';

// @ts-ignore
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];
Expand All @@ -30,7 +31,8 @@ const ApiDOMResolveStrategy: stampit.Stamp<IResolveStrategy> = stampit(ResolveSt
? cloneDeep(file.parseResult)
: file.parseResult;
const reference = Reference({ uri: file.uri, value: referenceValue });
const visitor = ApiDOMResolveVisitor({ reference, options });
const mergedOptions = mergeOptions(options, { resolve: { internal: false } });
const visitor = ApiDOMResolveVisitor({ reference, options: mergedOptions });
const refSet = ReferenceSet();
refSet.add(reference);

Expand Down
1 change: 1 addition & 0 deletions packages/apidom-reference/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export interface ReferenceResolveOptions {
resolverOpts: Record<string, any>;
strategies: Array<ResolveStrategy>;
strategyOpts: Record<string, any>;
internal: boolean;
external: boolean;
maxDepth: number;
}
Expand Down