Example #1
0
function writeSpec(source, type, exPatch, callback) {
  //FIXME: remove hack and unify API
  if (_.isObject(source)) {
    var spec = source;

    source = util.getOriginUrl(spec);
    callback = type;
    type = getSpecType(spec);

    exPatch = _.cloneDeep(spec);
    delete exPatch.info['x-origin'];
  }

  converter.getSpec(source, type, function (err, spec) {
    assert(!err, err);

    var fixup = util.readYaml(getOriginFixupPath(spec));
    jsondiffpatch.patch(spec, fixup);

    convertToSwagger(spec, function (error, swagger) {
      var result = {
        spec: spec,
        errors: error
      };

      if (error)
        return callback(error, result);

      try {
        patchSwagger(swagger, exPatch);
      }
      catch (e) {
        return callback(e, result);
      }

      expandPathTemplates(swagger);
      replaceSpacesInSchemaNames(swagger);
      extractApiKeysFromParameters(swagger);

      result.swagger = swagger;

      validateAndFix(swagger, function (errors, warnings) {
        result.warnings = warnings;

        if (errors)
          return callback(errors, result);

        if (warnings)
          logYaml(warnings);

        util.saveSwagger(swagger);
        callback(null, result);
      });
    });
  });
}
function writeSpec(source, type, exPatch, callback) {
  converter.getSpec(source, type, function (err, spec) {
    assert(!err, err);

    convertToSwagger(spec, function (error, swagger) {
      var result = {
        spec: spec,
        errors: error
      };

      if (error)
        return callback(error, result);

      try {
        patchSwagger(swagger, exPatch);
      }
      catch (e) {
        callback(e, result);
      }
      result.swagger = swagger;

      function done(errors, warnings) {
        result.warnings = warnings;

        if (errors)
          return callback(errors, result);

        if (warnings)
          logYaml(warnings);

        saveSwagger(swagger);
        callback(null, result);
      }

      function validateAndFix() {
        validateSwagger(swagger, function (errors, warnings) {
          if (!errors)
            return done(errors, warnings);

          if (fixSpec(swagger, errors))
            validateAndFix();
          else
            validateSwagger(swagger, done);
        });
      }

      validateAndFix();
    });
  });
}
Example #3
0
function validateSwagger(swagger) {
  return converter.getSpec(swagger, 'swagger_2')
    .then(spec => spec.validate())
    .then(result => {
      if (result.errors && swagger['x-hasEquivalentPaths']) {
        _.remove(result.errors, function (error) {
          return (error.code === 'EQUIVALENT_PATH');
        });

        if (_.isEmpty(result.errors))
          result.errors = null;
      }

      return result;
    });
}
Example #4
0
function validateSwagger(swagger, callback) {
  //TODO: remove 'getSpec', instead do it when reading file.
  converter.getSpec(swagger, 'swagger_2', function (err, spec) {
    assert(!err, err);
    spec.validate(function (errors, warnings) {
      if (errors && swagger['x-hasEquivalentPaths']) {
        _.remove(errors, function (error) {
          return (error.code === 'EQUIVALENT_PATH');
        });
        if (_.isEmpty(errors))
          errors = null;
      }

      callback(errors, warnings);
    });
  });
}
Example #5
0
function writeSpec(source, type, exPatch) {
  var context = {};

  return converter.getSpec(source, type)
    .then(spec => {
      context.spec = spec;

      var fixup = util.readYaml(getOriginFixupPath(spec));
      jsondiffpatch.patch(spec, fixup);

      return convertToSwagger(spec);
    })
    .then(swagger => {
      context.swagger = swagger;

      patchSwagger(swagger, exPatch);

      expandPathTemplates(swagger);
      replaceSpacesInSchemaNames(swagger);
      extractApiKeysFromParameters(swagger);


      return validateAndFix(swagger)
    })
    .then(validation => {
      context.validation = validation;

      if (validation.errors)
        throw Error(errorToString(null, context));

      if (validation.warnings)
        logYaml(validation.warnings);

      util.saveSwagger(context.swagger);
      return context.swagger;
    })
    .catch(e => {
      e.context = context;
      throw e;
    });
}