<commit_before>var Endpoint = require('../entities/endpoint'),
    Exporter = require('./exporter'),
    SwaggerParser = require('swagger-parser'),
    jsonHelper = require('../utils/json.js'),
    SwaggerDefinition = require('../entities/swagger/definition'),
    _ = require('lodash'),
    url = require('url');

function Swagger() {
  this.metadata = null;
}

function mapResponseTypes(slResponses) {
  var result = [];
  for(var i in slResponses) {
    var res = slResponses[i];
    if (res.mimeType) {
      result.push(res.mimeType);
    }
  }
  return _.uniq(result);
}


function validateParameters(parameters) {
  parameters = jsonHelper.orderByKeys(parameters, ['$ref', 'name', 'in', 'description', 'required', 'schema']);
  var validTypes = ['string', 'number', 'integer', 'boolean', 'array', 'file'], defaultType = 'string';
  for(var i in parameters) {
    if (parameters[i].in != 'body') {
      if (Array.isArray(parameters[i].type)) {
        if(parameters[i].type.length > 0) {
          parameters[i].type = parameters[i].type[0];
        } else {
          parameters[i].type = defaultType;
        }
      }

      if(validTypes.indexOf(parameters[i].type) < 0) {
        //type not valid
        parameters[i].type = defaultType;
      }
    }
  }
  return parameters;
}

function constructSwaggerMethod(endpoint, parameters, responses, env) {
  var resultSwaggerMethod = {
    description: endpoint.Description,
    operationId: endpoint.Name,
    consumes: [endpoint.Body.mimeType || env.DefaultRequestType],
    produces: mapResponseTypes(endpoint.Responses),
    parameters: parameters,
    responses: responses,
    summary: endpoint.Summary
  };
  if (resultSwaggerMethod.operationId.length === 0) {
    //don't keep empty operationId in exported definition
    delete resultSwaggerMethod.operationId;
  }
  return resultSwaggerMethod;
}

Swagger.prototype = new Exporter();

Swagger.prototype._mapEndpointSecurity = function(securedByTypes, securityDefinitions) {
  var security = [];
  for (var type in securedByTypes) {
    var scheme = securityDefinitions[type];
    if (!scheme) {
      //definition error
    }
    switch (type) {
      case 'apiKey':
      case 'basic':
        var result = {};
        if (scheme.header) {
          result[scheme.header.name] = [];
        }
        if (scheme.query) {
          result[scheme.query.name] = [];
        }
        security.push(result);
        break;
      case 'oauth2':
        var result = {};
        result[scheme.name] = securedByTypes[type];
        security.push(result);
        break;
    }
  }
  return security;
};

Swagger.prototype._mapSecurityDefinitions = function(securityDefinitions) {
  var result = {};
  for(var type in securityDefinitions) {
    var sd = securityDefinitions[type];
    switch(type) {
      case 'apiKey':
        if (sd.hasOwnProperty('header')) {
          result[sd.header.name] = {
            name: sd.header.name,
            type: type,
            in: 'header'
          };
        }
        if (sd.hasOwnProperty('query')) {
          result[sd.query.name] = {
            name: sd.query.name,
            type: type,
            in: 'query'
          };
        }
        break;
      case 'oauth2':

        result[sd.name] = {
          authorizationUrl: sd.authorizationUrl || '',
          tokenUrl: sd.tokenUrl || '',
          type: type
        };

        var slScopes = sd.scopes, swaggerScopes = {};
        for (var i in slScopes) {
          var scope = slScopes[i];
          swaggerScopes[scope.name] = scope.value;
        }
        if (Object.keys(swaggerScopes).length > 0) {
          result[sd.name]['scopes'] = swaggerScopes;
        }

        if (sd.flow) {
          result[sd.name]['flow'] = sd.flow;
        }
        //TODO
        break;
      case 'basic':
        result[type] = {
          type: type
        };
        break;<commit_msg>- security definitions export update(more accordance with spec)<commit_after>var Endpoint = require('../entities/endpoint'),
    Exporter = require('./exporter'),
    SwaggerParser = require('swagger-parser'),
    jsonHelper = require('../utils/json.js'),
    SwaggerDefinition = require('../entities/swagger/definition'),
    _ = require('lodash'),
    url = require('url');

function Swagger() {
  this.metadata = null;
}

function mapResponseTypes(slResponses) {
  var result = [];
  for(var i in slResponses) {
    var res = slResponses[i];
    if (res.mimeType) {
      result.push(res.mimeType);
    }
  }
  return _.uniq(result);
}


function validateParameters(parameters) {
  parameters = jsonHelper.orderByKeys(parameters, ['$ref', 'name', 'in', 'description', 'required', 'schema']);
  var validTypes = ['string', 'number', 'integer', 'boolean', 'array', 'file'], defaultType = 'string';
  for(var i in parameters) {
    if (parameters[i].in != 'body') {
      if (Array.isArray(parameters[i].type)) {
        if(parameters[i].type.length > 0) {
          parameters[i].type = parameters[i].type[0];
        } else {
          parameters[i].type = defaultType;
        }
      }

      if(validTypes.indexOf(parameters[i].type) < 0) {
        //type not valid
        parameters[i].type = defaultType;
      }
    }
  }
  return parameters;
}

function constructSwaggerMethod(endpoint, parameters, responses, env) {
  var resultSwaggerMethod = {
    description: endpoint.Description,
    operationId: endpoint.Name,
    consumes: [endpoint.Body.mimeType || env.DefaultRequestType],
    produces: mapResponseTypes(endpoint.Responses),
    parameters: parameters,
    responses: responses,
    summary: endpoint.Summary
  };
  if (resultSwaggerMethod.operationId.length === 0) {
    //don't keep empty operationId in exported definition
    delete resultSwaggerMethod.operationId;
  }
  return resultSwaggerMethod;
}

Swagger.prototype = new Exporter();

Swagger.prototype._mapEndpointSecurity = function(securedByTypes, securityDefinitions) {
  var security = [];
  for (var type in securedByTypes) {
    var scheme = securityDefinitions[type];
    if (!scheme) {
      //definition error
    }
    switch (type) {
      case 'apiKey':
      case 'basic':
        var result = {};
        if (scheme.header) {
          result[scheme.header.name] = [];
        }
        if (scheme.query) {
          result[scheme.query.name] = [];
        }
        security.push(result);
        break;
      case 'oauth2':
        var result = {};
        result[scheme.name] = securedByTypes[type];
        security.push(result);
        break;
    }
  }
  return security;
};

Swagger.prototype._mapSecurityDefinitions = function(securityDefinitions) {
  var result = {};
  for(var type in securityDefinitions) {
    var sd = securityDefinitions[type];
    switch(type) {
      case 'apiKey':
        if (sd.hasOwnProperty('header')) {
          result[sd.header.name] = {
            name: sd.header.name,
            type: type,
            in: 'header'
          };
        }
        if (sd.hasOwnProperty('query')) {
          result[sd.query.name] = {
            name: sd.query.name,
            type: type,
            in: 'query'
          };
        }
        break;
      case 'oauth2':

        var slScopes = sd.scopes, swaggerScopes = {};
        for (var i in slScopes) {
          var scope = slScopes[i];
          swaggerScopes[scope.name] = scope.value;
        }

        result[sd.name] = {
          type: type,
          flow: sd.flow,
          scopes: swaggerScopes
        };

        if (['implicit', 'accessCode'].indexOf(sd.flow) >= 0) {
          result[sd.name]['authorizationUrl'] = sd.authorizationUrl;
        }

        if (['password', 'application', 'accessCode'].indexOf(sd.flow) >= 0) {
          result[sd.name]['tokenUrl'] = sd.tokenUrl;
        }
        break;
      case 'basic':
        result[type] = {
          type: type
        };
        break;
{'fraction_non_alphanumeric': 0.1395466632898569, 'fraction_numerical': 0.0018994554894263644, 'mean_word_length': 2.328276443320691, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '738313', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2327, 'n_words': 477}