Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let context = clone(options.cache[target]);
let data = options.externalRef = context;
if (fragment) {
data = jptr(data, fragment);
if (data === false) {
data = {}; // case:A(2) where the resolution fails
if (options.fatal) {
let ex = new Error('Cached $ref resolution failed '+target+fragment);
if (options.promise) options.promise.reject(ex)
else throw(ex);
}
}
}
data = resolveAllFragment(data, context, pointer, fragment, target, options);
data = filterData(data, options);
callback(clone(data), target, options);
return Promise.resolve(data);
}
if (options.verbose) console.warn('GET', target, fragment);
if (options.handlers && options.handlers[effectiveProtocol]) {
return options.handlers[effectiveProtocol](base, pointer, fragment, options)
.then(function (data) {
options.externalRef = data;
data = filterData(data, options);
options.cache[target] = data;
callback(data, target, options);
return data;
})
.catch(function(ex){
if (options.verbose) console.warn(ex);
}
}
if (!refs[$ref]) {
refs[$ref] = { resolved: false, paths: [], extras:{}, description: obj[key].description };
}
if (refs[$ref].resolved) {
if (options.rewriteRefs) {
// we've already seen it
let newRef = refs[$ref].resolvedAt;
if (options.verbose>1) console.warn('Rewriting ref', $ref, newRef);
obj[key]['x-miro'] = $ref;
obj[key].$ref = newRef+$extra; // resolutionCase:C (new string)
}
else {
obj[key] = clone(refs[$ref].data); // resolutionCase:D (cloned:yes)
}
}
else {
refs[$ref].paths.push(state.path);
refs[$ref].extras[state.path] = $extra;
}
}
}
}
function fixupRefs(obj, key, state) {
let options = state.payload.options;
if (isRef(obj,key)) {
if (obj[key].startsWith('#/components/')) {
// no-op
}
else if (obj[key] === '#/consumes') {
// people are *so* creative
delete obj[key];
state.parent[state.pkey] = clone(options.openapi.consumes);
}
else if (obj[key] === '#/produces') {
// and by creative, I mean devious
delete obj[key];
state.parent[state.pkey] = clone(options.openapi.produces);
}
else if (obj[key].startsWith('#/definitions/')) {
//only the first part of a schema component name must be sanitised
let keys = obj[key].replace('#/definitions/', '').split('/');
let newKey = componentNames.schemas[decodeURIComponent(keys[0])]; // lookup, resolves a $ref
if (newKey) {
keys[0] = newKey;
}
else {
throwOrWarn('Could not resolve reference '+obj[key],obj,options);
}
let target;
if (effectiveProtocol === 'file:') {
target = path.resolve(base ? base + '/' : '', pointer);
}
else {
target = url.resolve(base ? base + '/' : '', pointer);
}
if (options.cache[target]) {
if (options.verbose) console.warn('CACHED', target, fragment);
/*
resolutionSource:A this is where we have cached the externally-referenced document from a
file, http or custom handler
*/
let context = clone(options.cache[target]);
let data = options.externalRef = context;
if (fragment) {
data = jptr(data, fragment);
if (data === false) {
data = {}; // case:A(2) where the resolution fails
if (options.fatal) {
let ex = new Error('Cached $ref resolution failed '+target+fragment);
if (options.promise) options.promise.reject(ex)
else throw(ex);
}
}
}
data = resolveAllFragment(data, context, pointer, fragment, target, options);
data = filterData(data, options);
callback(clone(data), target, options);
return Promise.resolve(data);
let entry = requestBodyCache[e];
if (entry.refs.length > 1) {
// create a shared requestBody
let suffix = '';
if (!entry.name) {
entry.name = 'requestBody';
// @ts-ignore
suffix = counter++;
}
while (rbNamesGenerated.indexOf(entry.name + suffix) >= 0) {
// @ts-ignore - this can happen if descriptions are not exactly the same (e.g. bitbucket)
suffix = (suffix ? ++suffix : 2);
}
entry.name = entry.name + suffix;
rbNamesGenerated.push(entry.name);
openapi.components.requestBodies[entry.name] = clone(entry.body);
for (let r in entry.refs) {
let ref = {};
ref.$ref = '#/components/requestBodies/' + entry.name;
jptr.jptr(openapi,entry.refs[r],ref);
}
}
}
}
if (openapi.components.responses && Object.keys(openapi.components.responses).length === 0) {
delete openapi.components.responses;
}
if (openapi.components.parameters && Object.keys(openapi.components.parameters).length === 0) {
delete openapi.components.parameters;
}
if (openapi.components.examples && Object.keys(openapi.components.examples).length === 0) {
}
options.context.pop();
}
contextAppend(options, 'responses');
if (options.lint) options.linter('responses',op.responses,'responses',options);
for (let r in op.responses) {
if (!r.startsWith('x-')) {
contextAppend(options, r);
let response = op.responses[r];
checkResponse(response, r, contextServers, openapi, options);
options.context.pop();
}
}
options.context.pop();
let localPathParameters = clone(pathParameters);
let opParameters = {};
if (typeof op.parameters !== 'undefined') {
should(op.parameters).be.an.Array();
contextAppend(options, 'parameters');
for (let p in op.parameters) {
let param = checkParam(op.parameters[p], p, path, contextServers, openapi, options);
if (opParameters[param.in+':'+param.name]) {
should.fail(false,true,'Duplicate operation-level parameter '+param.name);
}
else {
opParameters[param.in+':'+param.name] = param;
delete localPathParameters[param.in+':'+param.name];
}
}
options.context.pop();
let defaults = {};
defaults.title = 'API';
defaults.language_tabs = [{ 'shell': 'Shell' }, { 'http': 'HTTP' }, { 'javascript': 'JavaScript' }, { 'javascript--nodejs': 'Node.JS' }, { 'ruby': 'Ruby' }, { 'python': 'Python' }, { 'java': 'Java' }];
defaults.toc_footers = [];
defaults.includes = [];
defaults.search = true;
defaults.theme = 'darkula';
defaults.headingLevel = 2;
defaults.templateCallback = function(template,stage,data) { return data; };
options = Object.assign({},defaults,options);
let data = {};
if (options.verbose) console.log('starting deref',api.info.title);
if (api.components) {
data.components = clone(api.components);
}
else {
data.components = {};
}
data.api = dereference(api,api,{verbose:options.verbose,$ref:'x-widdershins-oldRef'});
if (options.verbose) console.log('finished deref');
if (data.api.components && data.api.components.schemas && data.api.components.schemas["x-widdershins-oldRef"]) {
delete data.api.components.schemas["x-widdershins-oldRef"];
}
if (typeof templates === 'undefined') {
templates = dot.process({ path: path.join(__dirname, 'templates', 'openapix') });
}
if (options.user_templates) {
templates = Object.assign(templates, dot.process({ path: options.user_templates }));
}
if (example.responses) {
for (let r in example.responses) {
if (example.responses[r].headers) {
for (let h in example.responses[r].headers) {
let value = example.responses[r].headers[h];
for (let rh in op.responses[r].headers) {
if (rh === h) {
let header = op.responses[r].headers[rh];
header.example = value;
}
}
}
}
if (example.responses[r].body) {
openapi.components.examples[se] = { value: clone(example.responses[r].body) };
if (op.responses[r] && op.responses[r].content) {
for (let ct in op.responses[r].content) {
let contentType = op.responses[r].content[ct];
if (!contentType.examples) {
contentType.examples = {};
}
contentType.examples[e] = { $ref: '#/components/examples/'+se };
}
}
}
}
}
}
delete op["x-ms-examples"];
}
processResponse(response, r, op, openapi, options);
}
}
if (op && (op['x-servers']) && (Array.isArray(op['x-servers']))) {
op.servers = op['x-servers'];
delete op['x-servers'];
} else if (op && op.schemes && op.schemes.length) {
for (let scheme of op.schemes) {
if ((!openapi.schemes) || (openapi.schemes.indexOf(scheme) < 0)) {
if (!op.servers) {
op.servers = [];
}
if (Array.isArray(openapi.servers)) {
for (let server of openapi.servers) {
let newServer = clone(server);
let serverUrl = url.parse(newServer.url);
serverUrl.protocol = scheme;
newServer.url = serverUrl.format();
op.servers.push(newServer);
}
}
}
}
}
if (options.debug) {
op["x-s2o-consumes"] = op.consumes || [];
op["x-s2o-produces"] = op.produces || [];
}
if (op) {
delete op.consumes;