function validateFinalSwagger()

in core/routemgmt/common/apigw-utils.js [96:166]


function validateFinalSwagger(swaggerApi) {
  return new Promise(function(resolve, reject) {
    // This returns a map of urls to check for path parameters.
    console.log("validateFinalSwagger: Validating swapper before posting to API GW.")
    var errorMsg;
    var paths = swaggerApi['paths'];

    if (swaggerApi.basePath && isValidRelativePath(swaggerApi.basePath)) {
      errorMsg = "The base path (" + swaggerApi.basePath + ") cannot have parameters. Only the relative path supports path parameters.";
    }
    /*
     * This code will look at each path defined, and look at all the parameters in each path, and validate that each
     * verb (GET,POST, etc) for each path defines parameter objects for each parameter defined in the path. For each of
     * these that contain parameters, it will also check that its target-url ends in $(request.path).
     * #beginPathValidation
     */
    var targetUrlMap = generateTargetUrlMap(swaggerApi['x-ibm-configuration']);
    for (var key in paths) {
      if (errorMsg) { break; }
      var idx = 0;
      if (isValidRelativePath(key)) {
        //Path is valid, lets check that we have parameters defined for each path parameter and that the target-url
        //has $(request.path) at the end.
        var namedParamsInPath = getPathParameters(key);
        //Loop over each verb (GET,POST,etc), each should contain path parameters.
        var parameters = paths[key]['parameters'] ? paths[key]['parameters'] : [];
        for (var httpType in paths[key]) {
          if (httpType == "parameters") {
            continue;
          }
          var xOpenWhisk = paths[key][httpType]['x-openwhisk']
          if (xOpenWhisk && xOpenWhisk['url'] && !xOpenWhisk['url'].endsWith('.http')) {
            errorMsg = "The action must use a response type of '.http' in order to receive the path parameters.";
            break;
          }
          var opId = paths[key][httpType].operationId;
          if (targetUrlMap[opId] && !targetUrlMap[opId].endsWith('.http$(request.path)')) {
            errorMsg = "The target-url for operationId '" + opId;
            errorMsg += "' must end in '$(request.path)' in order for actions to receive the path parameters.";
            break;
          }
          var allParams = parameters.concat(paths[key][httpType].parameters);
          for (var i = 0 ; i < namedParamsInPath.length ; ++i) {
            var found = false;
            for (var j in allParams) {
              if (allParams[j].name == namedParamsInPath[i]) {
                found = true;
                break;
              }
            }
            if (!found) {
              errorMsg = "The parameter '" + namedParamsInPath[i] + "' defined in path '" + key + "' does not match any";
              errorMsg += " of the parameters defined for the path in the swagger file.";
              break;
            }
          }
          if(errorMsg) { break; }
        }
        if(errorMsg) { break; }
      }
    }
    //#endPathValidation
    if (errorMsg) {
      console.error("validateFinalSwagger:" + errorMsg)
      reject(errorMsg);
    } else {
      console.log("validateFinalSwagger: Validation of swagger before posting to API GW was successful.")
      resolve(swaggerApi);
    }
  });
}