Skip to content

Commit

Permalink
refactor(resolver): apply SRP for OpenAPI 3.1 deref strategy
Browse files Browse the repository at this point in the history
SRP = Single Responsibility Principle

Refs #2750
  • Loading branch information
char0n committed Jan 18, 2023
1 parent c7c9804 commit cd5000c
Show file tree
Hide file tree
Showing 4 changed files with 244 additions and 232 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable camelcase */
import { createNamespace, visit } from '@swagger-api/apidom-core';
import { createNamespace, visit, mergeAllVisitors } from '@swagger-api/apidom-core';
import { ReferenceSet, Reference } from '@swagger-api/apidom-reference/configuration/empty';
import OpenApi3_1DereferenceStrategy from '@swagger-api/apidom-reference/dereference/strategies/openapi-3-1';
import openApi3_1Namespace, { getNodeType, keyMap } from '@swagger-api/apidom-ns-openapi-3-1';

import OpenApi3_1SwaggerClientDereferenceVisitor from './visitor.js';
import OpenApi3_1SwaggerClientDereferenceVisitor from './visitors/dereference.js';
import ParameterMacroVisitor from './visitors/parameters.js';
import ModelPropertyMacroVisitor from './visitors/properties.js';

const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')];

Expand All @@ -29,6 +31,7 @@ const OpenApi3_1SwaggerClientDereferenceStrategy = OpenApi3_1DereferenceStrategy
},
methods: {
async dereference(file, options) {
const visitors = [];
const namespace = createNamespace(openApi3_1Namespace);
const refSet = options.dereference.refSet ?? ReferenceSet();
let reference;
Expand All @@ -41,16 +44,39 @@ const OpenApi3_1SwaggerClientDereferenceStrategy = OpenApi3_1DereferenceStrategy
reference = refSet.find((ref) => ref.uri === file.uri);
}

const visitor = OpenApi3_1SwaggerClientDereferenceVisitor({
// create main dereference visitor
const dereferenceVisitor = OpenApi3_1SwaggerClientDereferenceVisitor({
reference,
namespace,
options,
useCircularStructures: this.useCircularStructures,
allowMetaPatches: this.allowMetaPatches,
parameterMacro: this.parameterMacro,
modelPropertyMacro: this.modelPropertyMacro,
});
const dereferencedElement = await visitAsync(refSet.rootRef.value, visitor, {
visitors.push(dereferenceVisitor);

// create parameter macro visitor (if necessary)
if (typeof this.parameterMacro === 'function') {
const parameterMacroVisitor = ParameterMacroVisitor({
parameterMacro: this.parameterMacro,
});
visitors.push(parameterMacroVisitor);
}

// create model property macro visitor (if necessary)
if (typeof this.modelPropertyMacro === 'function') {
const modelPropertyMacroVisitor = ModelPropertyMacroVisitor({
modelPropertyMacro: this.modelPropertyMacro,
});
visitors.push(modelPropertyMacroVisitor);
}

// determine the root visitor
const rootVisitor =
visitors.length === 1
? visitors[0]
: mergeAllVisitors(visitors, { nodeTypeGetter: getNodeType });

const dereferencedElement = await visitAsync(refSet.rootRef.value, rootVisitor, {
keyMap,
nodeTypeGetter: getNodeType,
});
Expand Down
Loading

0 comments on commit cd5000c

Please sign in to comment.