2021-04-08 06:27:49 +03:00
|
|
|
import { JSONPath } from "jsonpath-plus";
|
|
|
|
import { inject, injectable } from "inversify";
|
|
|
|
import _ from "lodash";
|
2022-07-01 15:19:29 +03:00
|
|
|
import { TYPES } from "../inversifyUtils";
|
2021-05-12 06:08:52 +03:00
|
|
|
import { FileLoaderOption, FileLoader } from "../swagger/fileLoader";
|
2022-07-01 15:19:29 +03:00
|
|
|
import { JsonLoader, JsonLoaderOption } from "../swagger/jsonLoader";
|
2021-04-08 06:27:49 +03:00
|
|
|
import { SwaggerLoader, SwaggerLoaderOption } from "../swagger/swaggerLoader";
|
2022-07-22 06:02:28 +03:00
|
|
|
import { Path, SwaggerSpec, Schema, LowerHttpMethods, Operation } from "../swagger/swaggerTypes";
|
2021-04-08 06:27:49 +03:00
|
|
|
import { AjvSchemaValidator } from "../swaggerValidator/ajvSchemaValidator";
|
|
|
|
import { SchemaValidator } from "../swaggerValidator/schemaValidator";
|
|
|
|
import { allOfTransformer } from "../transform/allOfTransformer";
|
|
|
|
import { getTransformContext, TransformContext } from "../transform/context";
|
|
|
|
import { discriminatorTransformer } from "../transform/discriminatorTransformer";
|
|
|
|
import { noAdditionalPropertiesTransformer } from "../transform/noAdditionalPropertiesTransformer";
|
|
|
|
import { referenceFieldsTransformer } from "../transform/referenceFieldsTransformer";
|
|
|
|
import { resolveNestedDefinitionTransformer } from "../transform/resolveNestedDefinitionTransformer";
|
|
|
|
import { xmsPathsTransformer } from "../transform/xmsPathsTransformer";
|
|
|
|
import { applyGlobalTransformers, applySpecTransformers } from "../transform/transformer";
|
2022-07-22 06:02:28 +03:00
|
|
|
import { traverseSwagger, traverseSwaggerAsync } from "../transform/traverseSwagger";
|
2021-04-08 06:27:49 +03:00
|
|
|
import { getProvider } from "../util/utils";
|
2021-09-30 06:27:59 +03:00
|
|
|
import { ScenarioDefinition } from "./apiScenarioTypes";
|
2021-06-01 09:01:44 +03:00
|
|
|
import { SchemaSearcher } from "./schemaSearcher";
|
2021-09-08 09:40:42 +03:00
|
|
|
import { CoverageCalculator, OperationCoverageResult } from "./coverageCalculator";
|
2021-04-08 06:27:49 +03:00
|
|
|
|
|
|
|
export interface SwaggerAnalyzerOption
|
|
|
|
extends FileLoaderOption,
|
|
|
|
JsonLoaderOption,
|
|
|
|
SwaggerLoaderOption {
|
|
|
|
swaggerFilePaths?: string[];
|
|
|
|
noExternalDependencyResourceType?: boolean;
|
|
|
|
filerTopLevelResourceType?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class SwaggerAnalyzer {
|
|
|
|
private swaggerSpecs: SwaggerSpec[];
|
|
|
|
private initialized: boolean = false;
|
|
|
|
private transformContext: TransformContext;
|
|
|
|
private schemaValidator: SchemaValidator;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
|
|
|
|
constructor(
|
|
|
|
@inject(TYPES.opts) private opts: SwaggerAnalyzerOption,
|
|
|
|
public jsonLoader: JsonLoader,
|
2021-05-12 06:08:52 +03:00
|
|
|
private swaggerLoader: SwaggerLoader,
|
|
|
|
private fileLoader: FileLoader
|
2021-04-08 06:27:49 +03:00
|
|
|
) {
|
|
|
|
this.swaggerSpecs = [];
|
|
|
|
this.schemaValidator = new AjvSchemaValidator(this.jsonLoader);
|
|
|
|
this.transformContext = getTransformContext(this.jsonLoader, this.schemaValidator, [
|
|
|
|
xmsPathsTransformer,
|
|
|
|
resolveNestedDefinitionTransformer,
|
|
|
|
referenceFieldsTransformer,
|
|
|
|
|
|
|
|
discriminatorTransformer,
|
|
|
|
allOfTransformer,
|
|
|
|
noAdditionalPropertiesTransformer,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-09-30 06:27:59 +03:00
|
|
|
public calculateOperationCoverage(testDef: ScenarioDefinition): OperationCoverageResult {
|
2021-09-08 09:40:42 +03:00
|
|
|
return CoverageCalculator.calculateOperationCoverage(testDef, this.swaggerSpecs);
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:33:59 +03:00
|
|
|
public calculateOperationCoverageBySpec(
|
|
|
|
testDef: ScenarioDefinition
|
|
|
|
): Map<string, OperationCoverageResult> {
|
|
|
|
return CoverageCalculator.calculateOperationCoverageBySpec(testDef, this.swaggerSpecs);
|
|
|
|
}
|
|
|
|
|
2022-07-22 06:02:28 +03:00
|
|
|
public getOperations(): { [key: string]: string[] } {
|
|
|
|
const operations: { [key: string]: string[] } = {};
|
|
|
|
for (const swaggerSpec of this.swaggerSpecs) {
|
|
|
|
const allOperationIds = new Set<string>();
|
|
|
|
traverseSwagger(swaggerSpec, {
|
|
|
|
onOperation: (operation: Operation, _path: Path, _method: LowerHttpMethods) => {
|
|
|
|
allOperationIds.add(operation.operationId!);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
operations[swaggerSpec._filePath] = Array.from(allOperationIds);
|
|
|
|
}
|
|
|
|
return operations;
|
|
|
|
}
|
|
|
|
|
2021-06-17 06:05:57 +03:00
|
|
|
public async getOperationListPath(): Promise<Path[]> {
|
|
|
|
const ret: Path[] = [];
|
|
|
|
for (const swaggerSpec of this.swaggerSpecs) {
|
|
|
|
await traverseSwaggerAsync(swaggerSpec, {
|
|
|
|
onPath: async (path, pathTemplate) => {
|
|
|
|
const resourceProvider = getProvider(pathTemplate);
|
|
|
|
if (resourceProvider) {
|
2022-07-01 15:19:29 +03:00
|
|
|
if (path._pathTemplate === `/providers/${resourceProvider}/operations`) {
|
2021-06-17 06:05:57 +03:00
|
|
|
ret.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-08 06:27:49 +03:00
|
|
|
public async initialize() {
|
|
|
|
if (this.initialized) {
|
|
|
|
throw new Error("Already initialized");
|
|
|
|
}
|
|
|
|
for (const swaggerFilePath of this.opts.swaggerFilePaths ?? []) {
|
|
|
|
const swaggerSpec = await this.swaggerLoader.load(swaggerFilePath);
|
|
|
|
this.swaggerSpecs.push(swaggerSpec);
|
|
|
|
applySpecTransformers(swaggerSpec, this.transformContext);
|
|
|
|
}
|
|
|
|
applyGlobalTransformers(this.transformContext);
|
2021-06-24 08:27:25 +03:00
|
|
|
this.initialized = true;
|
2021-04-08 06:27:49 +03:00
|
|
|
}
|
2021-05-12 06:08:52 +03:00
|
|
|
|
|
|
|
public async getAllSecretKey(): Promise<string[]> {
|
|
|
|
let ret: string[] = [];
|
|
|
|
const allXmsSecretsPath = '$..[?(@["x-ms-secret"])]~';
|
|
|
|
for (const swaggerPath of this.opts.swaggerFilePaths ?? []) {
|
|
|
|
const swagger = JSON.parse(await this.fileLoader.load(swaggerPath));
|
|
|
|
const allXmsSecretKeys = JSONPath({
|
|
|
|
path: allXmsSecretsPath,
|
|
|
|
json: swagger,
|
|
|
|
});
|
|
|
|
ret = ret.concat(allXmsSecretKeys);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2021-06-24 08:27:25 +03:00
|
|
|
|
|
|
|
public findSchemaByJsonPointer(jsonPointer: string, schema: Schema, body?: any) {
|
|
|
|
return SchemaSearcher.findSchemaByJsonPointer(jsonPointer, schema, this.jsonLoader, body);
|
|
|
|
}
|
2021-04-08 06:27:49 +03:00
|
|
|
}
|