in lib/swaggerValidator/trafficValidator.ts [96:171]
public async initialize() {
const specPathStats = fs.statSync(this.specPath);
const trafficPathStats = fs.statSync(this.trafficPath);
let specFileDirectory = "";
let swaggerPathsPattern = "**/*.json";
if (specPathStats.isFile()) {
specFileDirectory = path.dirname(this.specPath);
swaggerPathsPattern = path.basename(this.specPath);
} else if (specPathStats.isDirectory()) {
specFileDirectory = this.specPath;
}
if (trafficPathStats.isFile()) {
this.trafficFiles.push(this.trafficPath);
} else if (trafficPathStats.isDirectory()) {
const searchPattern = path.join(this.trafficPath, "**/*.json");
const matchedPaths = glob.sync(searchPattern, {
nodir: true,
});
for (const filePath of matchedPaths) {
this.trafficFiles.push(filePath);
}
}
const liveValidationOptions = {
checkUnderFileRoot: false,
loadValidatorInBackground: false,
directory: specFileDirectory,
swaggerPathsPattern: [swaggerPathsPattern],
excludedSwaggerPathsPattern: DefaultConfig.ExcludedExamplesAndCommonFiles,
git: {
shouldClone: false,
},
};
this.liveValidator = new LiveValidator(liveValidationOptions);
await this.liveValidator.initialize();
const container = inversifyGetContainer();
this.loader = inversifyGetInstance(LiveValidatorLoader, {
container,
fileRoot: liveValidationOptions.directory,
...liveValidationOptions,
loadSuppression: Object.keys(apiValidationErrors),
});
const swaggerPaths = this.liveValidator.swaggerList;
while (swaggerPaths.length > 0) {
const swaggerPath = swaggerPaths.shift()!;
let spec;
try {
spec = await this.loader.load(pathResolve(swaggerPath));
} catch (e) {
console.log(
`Exception when loading spec, ErrorMessage: ${(e as any)?.message}; ErrorStack: ${
(e as any)?.stack
}.`
);
}
if (spec !== undefined) {
// Get Swagger - operation mapper.
if (this.operationSpecMapper.get(swaggerPath) === undefined) {
this.operationSpecMapper.set(swaggerPath, []);
}
traverseSwagger(spec, {
onOperation: (operation) => {
if (
operation.operationId !== undefined &&
!this.operationSpecMapper.get(swaggerPath)?.includes(operation.operationId)
) {
this.operationSpecMapper.get(swaggerPath)!.push(operation.operationId);
}
},
});
}
}
}