Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function validateSwagger(swagger, source) {
let target = 'swagger_2';
if (swagger.openapi) target = 'openapi_3';
return converter.getSpec(swagger, target)
.then(function(spec){
var relativeBase = source.split('/');
relativeBase.pop();
relativeBase = relativeBase.join('/');
// 'relativeBase' is for the released version of sway/json-refs, the latest version uses 'location'
let jsonRefs = {relativeBase: relativeBase, location: source, loaderOptions: {processContent:
function (res, cb) {
cb(undefined, YAML.safeLoad(res.text,{json:true}));
}
}};
if (source.indexOf('azure.com')>=0) {
jsonRefs.includeInvalid = true;
jsonRefs.resolveCirculars = true;
}
else {
jsonRefs.includeInvalid = false;
function writeSpec(source, format, exPatch, command) {
var context = {source};
resolverContext = {
anyDiff: false,
called: false,
source: source,
format: format,
etag: !command.slow
};
return converter.getSpec(source, format)
.then(spec => {
context.spec = spec;
if (resolverContext.called && !resolverContext.anyDiff)
throw Error('Warning: 304 Not modified');
var fixup = util.readYaml(getOriginFixupPath(spec));
jsondiffpatch.patch(spec, fixup);
return convertToSwagger(spec,exPatch,command);
})
.then(swagger => {
context.swagger = swagger;
delete exPatch.info.version; // testing
patchSwagger(swagger, exPatch);
expandPathTemplates(swagger);
async function transformSpec(api: config.API) {
LOGGER.debug("Transforming spec of api '%s' to swagger2 format", api.name)
return Converter.getSpec(api.specification, 'openapi_3')
.then((fromSpec: any) => fromSpec.convertTo('swagger_2'))
}
async function validateSpec(api: config.API, type: string) {
LOGGER.debug("Validating spec of api '%s'", api.name);
return Converter.getSpec(api.specification, type)
.then((fromSpec: any) => {
return fromSpec.validate()
}).then((result: any) => {
if (result.errors) {
throw new Error("Validation error of api '" + api.name + "':" + pretty_yaml.stringify(result.errors));
}
if (result.warnings) {
LOGGER.warn("%s", pretty_yaml.stringify(result.warnings));
}
});
}
function writeSpecFromLead(lead,command) {
var exPatch = _.cloneDeep(lead);
var origin = util.getOrigin(exPatch);
if (Array.isArray(origin))
origin = origin.pop();
var source = origin.url;
var format = converter.getFormatName(origin.format, origin.version);
if (!exPatch.info['x-origin'].length) {
delete exPatch.info['x-origin'];
}
return writeSpec(source, format, exPatch, command);
}
}
function getCacheEntry(url) {
var cacheIndex =_.findIndex(httpCache.cache,function(e){
return e.url === url;
});
if (cacheIndex>=0)
return httpCache.cache[cacheIndex];
else {
var cacheEntry = { url: url };
httpCache.cache.push(cacheEntry);
return cacheEntry;
}
}
converter.ResourceReaders.url = function (url) {
resolverContext.called = true;
var options = {
headers: {
'Accept': 'application/json,*/*',
'Accept-Language': 'en-GB,en;q=0.5'
},
retries : 10
};
var cacheEntry = getCacheEntry(url);
if (cacheEntry && cacheEntry.etag && resolverContext.etag && resolverContext.format !== 'swagger_1') {
options.headers['If-None-Match'] = cacheEntry.etag;
}
return makeRequest('get', url, options)
.then(function(result){
if (result[0].statusCode === 304) {
throw new Error('Warning: 304 Not Modified');
export default function swagger1Loader (config, { onLoadProgress, onNewAPI, onNewOperation, onNewDefinition, onLoadCompleted, onLoadError }) {
const url = config.url.getUrl()
onLoadProgress(`Loading API Swagger 1.0 Spec from ${url}`)
return Converter.convert({
from: 'swagger_1',
to: 'swagger_2',
source: url
}).then(function(converted) {
SwaggerParser.validate(converted.spec).then(function (api) {
let newApi = api
let defaultHost = window.location.origin
newApi = config.interceptor({ friendlyName: config.friendlyName, url: config.url }, api)
swagger2SpecLoader(newApi, config.friendlyName, config.slug, defaultHost, { onLoadProgress, onNewAPI, onNewOperation, onLoadCompleted, onLoadError })
}).catch(function (err) {
onLoadError(err)
})
}).catch(function(err) {
onLoadError(err)
})
}
#!/usr/bin/env node
'use strict';
const assert = require('assert');
const fs = require('fs');
const url = require('url');
const pathLib = require('path');
const utilLib = require('util');
const _ = require('lodash');
const jp = require('json-pointer');
const jsonPath = require('jsonpath');
const converter = require('api-spec-converter');
const converterVersion = require('api-spec-converter/package.json').version;
const parseDomain = require('parse-domain');
const jsonPatch = require('json-merge-patch');
const jiff = require('jiff');
const YAML = require('js-yaml');
const Promise = require('bluebird');
const recurse = require('reftools/lib/recurse.js').recurse;
const makeRequest = require('makeRequest');
const util = require('./util');
const specSources = require('./spec_sources');
const sp = require('./sortParameters.js');
var httpCache;
try {
httpCache = YAML.safeLoad(fs.readFileSync(pathLib.join(__dirname,'../metadata/httpCache.yaml'),'utf8'));
}
catch (ex) {