Skip to content

Commit

Permalink
User/raych1/cli bug fix (Azure#716)
Browse files Browse the repository at this point in the history
* Fix validation command error

* style fix

* package json fix

* package lock update
  • Loading branch information
raych1 authored Dec 3, 2021
1 parent 06f47f5 commit 77e49b7
Show file tree
Hide file tree
Showing 10 changed files with 17,865 additions and 101 deletions.
6 changes: 5 additions & 1 deletion ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log - oav

## 12/01/2021 2.11.2
## 12/03/2021 2.11.3

- Utils - move get provider related functions to utils because of DI error

## 12/02/2021 2.11.2

- LiveValidator - add more unittest for data-plane spec path
- OperationSearcher - use local variable instead of glob regex
Expand Down
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
validateExamplesInCompositeSpec,
resolveSpec,
resolveCompositeSpec,
validateTrafficAgainstSpec,
} from "./lib/validate";

export { BaseValidationError } from "./lib/util/baseValidationError";
Expand Down Expand Up @@ -103,6 +104,8 @@ export {
SemanticValidator,
} from "./lib/swaggerValidator/semanticValidator";

export { TrafficValidationIssue, TrafficValidator } from "./lib/swaggerValidator/trafficValidator";

export { getAutorestConfig } from "./lib/util/getAutorestConfig";
// Constants
export const Constants = C;
8 changes: 4 additions & 4 deletions lib/liveValidation/liveValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import { RuntimeException } from "../util/validationError";
import { inversifyGetContainer, inversifyGetInstance, TYPES } from "../inversifyUtils";
import { setDefaultOpts } from "../swagger/loader";
import { apiValidationErrors, ApiValidationErrorCode } from "../util/errorDefinitions";
import { kvPairsToObject } from "../util/utils";
import { LiveValidatorLoader, LiveValidatorLoaderOption } from "./liveValidatorLoader";
import {
kvPairsToObject,
getProviderFromPathTemplate,
getProviderFromSpecPath,
OperationSearcher,
} from "./operationSearcher";
} from "../util/utils";
import { LiveValidatorLoader, LiveValidatorLoaderOption } from "./liveValidatorLoader";
import { OperationSearcher } from "./operationSearcher";
import {
LiveRequest,
LiveResponse,
Expand Down
57 changes: 1 addition & 56 deletions lib/liveValidation/operationSearcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
unknownResourceProvider,
} from "../util/constants";
import { getOavErrorMeta } from "../util/errorDefinitions";
import { Writable } from "../util/utils";
import { getProviderFromPathTemplate, Writable } from "../util/utils";
import { LiveValidatorLoggingLevels, LiveValidatorLoggingTypes } from "./liveValidator";
import { ValidationRequest } from "./operationValidator";

Expand Down Expand Up @@ -319,61 +319,6 @@ export class OperationSearcher {
}
}

/**
* Gets provider namespace from the given path. In case of multiple, last one will be returned.
* @param {string} pathStr The path of the operation.
* Example "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/
* providers/{resourceProviderNamespace}/{parentResourcePath}/{resourceType}/
* {resourceName}/providers/Microsoft.Authorization/roleAssignments"
* will return "Microsoft.Authorization".
*
* @returns {string} result - provider namespace from the given path.
*/
export function getProviderFromPathTemplate(pathStr?: string | null): string | undefined {
if (
pathStr === null ||
pathStr === undefined ||
typeof pathStr.valueOf() !== "string" ||
!pathStr.trim().length
) {
throw new Error(
"pathStr is a required parameter of type string and it cannot be an empty string."
);
}

let result;

// Loop over the paths to find the last matched provider namespace
// eslint-disable-next-line no-constant-condition
while (true) {
const pathMatch = providerRegEx.exec(pathStr);
if (pathMatch === null) {
break;
}
result = pathMatch[1];
}

return result;
}
const providerRegEx = new RegExp("/providers/(:?[^{/]+)", "gi");

export interface PathProvider {
provider: string;
type: "resource-manager" | "data-plane";
}

export function getProviderFromSpecPath(specPath: string): PathProvider | undefined {
const managementPlaneProviderInSpecPathRegEx: RegExp = /\/resource-manager\/(.*?)\//gi;
const dataPlaneProviderInSpecPathRegEx: RegExp = /\/data-plane\/(.*?)\//gi;
const manageManagementMatch = managementPlaneProviderInSpecPathRegEx.exec(specPath);
const dataPlaneMatch = dataPlaneProviderInSpecPathRegEx.exec(specPath);
return manageManagementMatch === null
? dataPlaneMatch === null
? undefined
: { provider: dataPlaneMatch[1], type: "data-plane" }
: { provider: manageManagementMatch[1], type: "resource-manager" };
}

/**
* Gets list of matched operations objects for given url.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/swagger/swaggerLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { inject, injectable } from "inversify";
import { TYPES } from "../inversifyUtils";
import { traverseSwagger } from "../transform/traverseSwagger";
import { xmsExamples } from "../util/constants";
import { getProviderFromSpecPath } from "../liveValidation/operationSearcher";
import { getProviderFromSpecPath } from "../util/utils";
import { FileLoader, FileLoaderOption } from "./fileLoader";
import { JsonLoader, JsonLoaderOption } from "./jsonLoader";
import { Loader, setDefaultOpts } from "./loader";
Expand Down
53 changes: 53 additions & 0 deletions lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,23 @@ export function getProvider(pathStr?: string | null): string | undefined {
return result;
}

export interface PathProvider {
provider: string;
type: "resource-manager" | "data-plane";
}

export function getProviderFromSpecPath(specPath: string): PathProvider | undefined {
const managementPlaneProviderInSpecPathRegEx: RegExp = /\/resource-manager\/(.*?)\//gi;
const dataPlaneProviderInSpecPathRegEx: RegExp = /\/data-plane\/(.*?)\//gi;
const manageManagementMatch = managementPlaneProviderInSpecPathRegEx.exec(specPath);
const dataPlaneMatch = dataPlaneProviderInSpecPathRegEx.exec(specPath);
return manageManagementMatch === null
? dataPlaneMatch === null
? undefined
: { provider: dataPlaneMatch[1], type: "data-plane" }
: { provider: manageManagementMatch[1], type: "resource-manager" };
}

export const getValueByJsonPointer = (obj: any, pointer: string | string[]) => {
const refTokens = Array.isArray(pointer) ? pointer : parse(pointer);

Expand Down Expand Up @@ -320,6 +337,42 @@ export function getApiVersionFromSwaggerFile(swaggerFilePath: string): string {
}

const providerRegEx = new RegExp("/providers/(:?[^{/]+)", "gi");
/**
* Gets provider namespace from the given path. In case of multiple, last one will be returned.
* @param {string} pathStr The path of the operation.
* Example "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/
* providers/{resourceProviderNamespace}/{parentResourcePath}/{resourceType}/
* {resourceName}/providers/Microsoft.Authorization/roleAssignments"
* will return "Microsoft.Authorization".
*
* @returns {string} result - provider namespace from the given path.
*/
export function getProviderFromPathTemplate(pathStr?: string | null): string | undefined {
if (
pathStr === null ||
pathStr === undefined ||
typeof pathStr.valueOf() !== "string" ||
!pathStr.trim().length
) {
throw new Error(
"pathStr is a required parameter of type string and it cannot be an empty string."
);
}

let result;

// Loop over the paths to find the last matched provider namespace
// eslint-disable-next-line no-constant-condition
while (true) {
const pathMatch = providerRegEx.exec(pathStr);
if (pathMatch === null) {
break;
}
result = pathMatch[1];
}

return result;
}

/**
* Gets provider resource type from the given path.
Expand Down
Loading

0 comments on commit 77e49b7

Please sign in to comment.