<commit_before>var parser = require('swagger-parser')
    Endpoint = require('../entities/endpoint'),
    Schema = require('../entities/schema'),
    Importer = require('./importer'),
    Project = require('../entities/project')

function Swagger() {
  this.metadata = null
  this.$refs = null
  this.filePath = null
}

Swagger.prototype = new Importer()

function mapSchema(schemaDefinitions) {
  var result = []
  for (var schemaName in schemaDefinitions) {
    var sd = new Schema(schemaName)
    sd.Definition = schemaDefinitions[schemaName]
    result.push(sd)
  }
  return result
}

function mapQueryString(params) {
  var queryString = {type:'object', properties: {}, required: []}
  for (var i in params) {
    var param = params[i]
    if (param.in !== 'query') {
      //skip other type of params
      continue
    }
    queryString.properties[param.name] = {
      type: param.type,
      description: param.description
    }
    if (param.required) {
      queryString.required.push(param.name)
    }
    if (param.type === 'array') {
      queryString.properties[param.name].items = param.items
    }
  }
  return queryString
}

function mapURIParams(params) {
  var pathParams = {}
  for (var i in params) {
    var param = params[i]
    if (param.hasOwnProperty('$ref')) {
      //any unresolved data?!?
      //console.log(i, param)
    }
    if (param.in !== 'path') {
      //skip other type of params
      continue
    }
    pathParams[param.name] = {
      type: param.type,
      description: param.description
    }

    if (param.type === 'array') {
      pathParams[param.name].items = param.items
    }
  }

  return pathParams
}

function mapRequestBody(params, reqType) {
  var data = {mimeType: reqType, body: {properties: {}, required: []}, example: ''}

  for (var i in params) {
    var param = params[i]
    if (param.in !== 'body' && param.in !== 'formData') {
      //skip other type of params
      continue
    }

    if (param.schema) {
      data.body.properties[param.name] = param.schema
      //data.body.properties[param.name].type = 'object'
    }
    else {
      data.body.properties[param.name] = {
        type: param.type,
        description: param.description,
      }
    }
    if (param.required) {
      data.body.required.push(param.name)
    }
  }
  return data
}

function mapResponseBody(responseBody, resType) {
  var data = []
  for (var code in responseBody) {
    var res = {mimeType: resType, body: {}, example: '', codes: []}
    if (responseBody[code].schema) {
      res.body = responseBody[code].schema
    }
    if (responseBody[code].hasOwnProperty('examples')) {
      for(var type in responseBody[code].examples) {
        if (type === resType) {
          res.example = JSON.stringify(responseBody[code].examples[type])
        }
      }
    }
    if (code === 'default') {
      //swagger default usually meant for error responses:
      //https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#responses-object
      code = 400
    }
    res.body = JSON.stringify(res.body)
    res.codes.push(code)
    data.push(res)
  }

  return data
}

function mapRequestHeaders(params) {
  var data = {type: 'object', properties: {}, required: []}

  for (var i in params) {
    var param = params[i]
    if (param.in !== 'header') {
      //skip other type of params
      continue
    }
    data.properties[param.name] = {
      type: param.type
    }
    if (param.required) {
      data.required.push(param.name)
    }
  }
  return data
}

Swagger.prototype.loadFile = function (path) {
  var me = this
  this.filePath = path
  return function(cb) {
     parser.parse(path)
      .then(function(api, metadata) {
        me.data = api
        me.metadata = metadata
        parser.resolve(path)
          .then(function($refs) {
            me.$refs = $refs.values()[me.filePath]
            cb()
          })
          .catch(function(err) {
            cb(err)
          })
      })
      .catch(function(err) {
        cb(err)
      })
  }
}

Swagger.prototype.map = function() {

  var defaultReqContentType = this.data.consumes[0], defaultResContentType = this.data.produces[0]
  this.project = new Project(this.data.info.title)
  this.project.Description = this.data.info.description || ''

  this.project.Environment.BasePath = this.data.basePath || ''
  this.project.Environment.Host = this.data.host || 'localhost'


  for (var path in this.data.paths) {
    var methods = this.data.paths[path]
    var pathParams = {}
    if (methods.parameters) {
      pathParams = mapURIParams(methods.parameters)
    }<commit_msg>- validation for consume/produce field in swagger importer.<commit_after>var parser = require('swagger-parser')
    Endpoint = require('../entities/endpoint'),
    Schema = require('../entities/schema'),
    Importer = require('./importer'),
    Project = require('../entities/project')

function Swagger() {
  this.metadata = null
  this.$refs = null
  this.filePath = null
}

Swagger.prototype = new Importer()

function mapSchema(schemaDefinitions) {
  var result = []
  for (var schemaName in schemaDefinitions) {
    var sd = new Schema(schemaName)
    sd.Definition = schemaDefinitions[schemaName]
    result.push(sd)
  }
  return result
}

function mapQueryString(params) {
  var queryString = {type:'object', properties: {}, required: []}
  for (var i in params) {
    var param = params[i]
    if (param.in !== 'query') {
      //skip other type of params
      continue
    }
    queryString.properties[param.name] = {
      type: param.type,
      description: param.description
    }
    if (param.required) {
      queryString.required.push(param.name)
    }
    if (param.type === 'array') {
      queryString.properties[param.name].items = param.items
    }
  }
  return queryString
}

function mapURIParams(params) {
  var pathParams = {}
  for (var i in params) {
    var param = params[i]
    if (param.hasOwnProperty('$ref')) {
      //any unresolved data?!?
      //console.log(i, param)
    }
    if (param.in !== 'path') {
      //skip other type of params
      continue
    }
    pathParams[param.name] = {
      type: param.type,
      description: param.description
    }

    if (param.type === 'array') {
      pathParams[param.name].items = param.items
    }
  }

  return pathParams
}

function mapRequestBody(params, reqType) {
  var data = {mimeType: reqType, body: {properties: {}, required: []}, example: ''}

  for (var i in params) {
    var param = params[i]
    if (param.in !== 'body' && param.in !== 'formData') {
      //skip other type of params
      continue
    }

    if (param.schema) {
      data.body.properties[param.name] = param.schema
      //data.body.properties[param.name].type = 'object'
    }
    else {
      data.body.properties[param.name] = {
        type: param.type,
        description: param.description,
      }
    }
    if (param.required) {
      data.body.required.push(param.name)
    }
  }
  return data
}

function mapResponseBody(responseBody, resType) {
  var data = []
  for (var code in responseBody) {
    var res = {mimeType: resType, body: {}, example: '', codes: []}
    if (responseBody[code].schema) {
      res.body = responseBody[code].schema
    }
    if (responseBody[code].hasOwnProperty('examples')) {
      for(var type in responseBody[code].examples) {
        if (type === resType) {
          res.example = JSON.stringify(responseBody[code].examples[type])
        }
      }
    }
    if (code === 'default') {
      //swagger default usually meant for error responses:
      //https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#responses-object
      code = 400
    }
    res.body = JSON.stringify(res.body)
    res.codes.push(code)
    data.push(res)
  }

  return data
}

function mapRequestHeaders(params) {
  var data = {type: 'object', properties: {}, required: []}

  for (var i in params) {
    var param = params[i]
    if (param.in !== 'header') {
      //skip other type of params
      continue
    }
    data.properties[param.name] = {
      type: param.type
    }
    if (param.required) {
      data.required.push(param.name)
    }
  }
  return data
}

Swagger.prototype.loadFile = function (path) {
  var me = this
  this.filePath = path
  return function(cb) {
     parser.parse(path)
      .then(function(api, metadata) {
        me.data = api
        me.metadata = metadata
        parser.resolve(path)
          .then(function($refs) {
            me.$refs = $refs.values()[me.filePath]
            cb()
          })
          .catch(function(err) {
            cb(err)
          })
      })
      .catch(function(err) {
        cb(err)
      })
  }
}

Swagger.prototype.map = function() {

  var defaultReqContentType = 'application/json', defaultResContentType = 'application/json'
  if (this.data.consumes && this.data.consumes.length > 0) {
    defaultReqContentType = this.data.consumes[0]
  }
  if (this.data.produces && this.data.produces.length > 0) {
    defaultResContentType = this.data.produces[0]
  }
  this.project = new Project(this.data.info.title)
  this.project.Description = this.data.info.description || ''

  this.project.Environment.BasePath = this.data.basePath || ''
  this.project.Environment.Host = this.data.host || 'localhost'


  for (var path in this.data.paths) {
    var methods = this.data.paths[path]
    var pathParams = {}
    if (methods.parameters) {
      pathParams = mapURIParams(methods.parameters)
    }
{'fraction_non_alphanumeric': 0.14148756847871893, 'fraction_numerical': 0.001685630004214075, 'mean_word_length': 2.5329363602530703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2065794', 'n_tokens_mistral': 3013, 'n_tokens_neox': 2972, 'n_words': 633}