public addSpecToCache()

in lib/liveValidation/operationSearcher.ts [51:134]


  public addSpecToCache(spec: SwaggerSpec) {
    traverseSwagger(spec, {
      onOperation: (operation, path, method) => {
        const httpMethod = method.toLowerCase() as LowerHttpMethods;
        const pathObject = path;
        const pathStr = pathObject._pathTemplate;
        let apiVersion = spec.info.version;
        let provider = getProviderFromPathTemplate(pathStr);

        const addOperationToCache = () => {
          let apiVersions = this.cache.get(provider!);
          if (apiVersions === undefined) {
            apiVersions = new Map();
            this.cache.set(provider!, apiVersions);
          }

          let allMethods = apiVersions.get(apiVersion);
          if (allMethods === undefined) {
            allMethods = new Map();
            apiVersions.set(apiVersion, allMethods);
          }

          let operationsForHttpMethod = allMethods.get(httpMethod);
          if (operationsForHttpMethod === undefined) {
            operationsForHttpMethod = [];
            allMethods.set(httpMethod, operationsForHttpMethod);
          }

          operationsForHttpMethod.push(operation);
        };

        this.logging(
          `${apiVersion}, ${operation.operationId}, ${pathStr}, ${httpMethod}`,
          LiveValidatorLoggingLevels.debug
        );

        if (!apiVersion) {
          this.logging(
            `Unable to find apiVersion for path : "${pathObject._pathTemplate}".`,
            LiveValidatorLoggingLevels.error,
            LiveValidatorLoggingTypes.specTrace,
            "Oav.OperationSearcher.addSpecToCache",
            undefined,
            {
              providerNamespace: spec._providerNamespace ?? "unknown",
              apiVersion: spec.info.version,
              specName: spec._filePath,
            }
          );
          apiVersion = unknownApiVersion;
        }
        apiVersion = apiVersion.toLowerCase();

        if (!provider) {
          const title = spec.info.title;

          // Whitelist lookups: Look up knownTitleToResourceProviders
          // Putting the provider namespace onto operation for future use
          if (title && knownTitleToResourceProviders[title]) {
            operation.provider = knownTitleToResourceProviders[title];
          }

          // Put the operation into 'Microsoft.Unknown' RPs
          provider = unknownResourceProvider;
          this.logging(
            `Unable to find provider for path : "${pathObject._pathTemplate}". ` +
              `Bucketizing into provider: "${provider}"`,
            LiveValidatorLoggingLevels.warn,
            LiveValidatorLoggingTypes.specTrace,
            "Oav.OperationSearcher.addSpecToCache",
            undefined,
            {
              providerNamespace: spec._providerNamespace ?? "unknown",
              apiVersion: spec.info.version,
              specName: spec._filePath,
            }
          );
        }
        provider = provider.toLowerCase();

        addOperationToCache();
      },
    });
  }