Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
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);
}
obj[key] = '#/components/schemas/' + keys.join('/');
}
else if (obj[key].startsWith('#/parameters/')) {
// for extensions like Apigee's x-templates
obj[key] = '#/components/parameters/' + common.sanitise(obj[key].replace('#/parameters/', ''));
}
else if (obj[key].startsWith('#/responses/')) {
// for extensions like Apigee's x-templates
obj[key] = '#/components/responses/' + common.sanitise(obj[key].replace('#/responses/', ''));
}
else if (obj[key].startsWith('#')) {
// fixes up direct $refs or those created by resolvers
let target = clone(jptr.jptr(options.openapi,obj[key]));
if (target === false) throwOrWarn('direct $ref not found '+obj[key],obj,options)
else if (options.refmap[obj[key]]) {
obj[key] = options.refmap[obj[key]];
}
else {
// we use a heuristic to determine what kind of thing is being referenced
let oldRef = obj[key];
oldRef = oldRef.replace('/properties/headers/','');
function main(openapi, options) {
let requestBodyCache = {};
componentNames = { schemas: {} };
if (openapi.security) processSecurity(openapi.security);
for (let s in openapi.components.securitySchemes) {
let sname = common.sanitise(s);
if (s != sname) {
if (openapi.components.securitySchemes[sname]) {
throwError('Duplicate sanitised securityScheme name ' + sname, options);
}
openapi.components.securitySchemes[sname] = openapi.components.securitySchemes[s];
delete openapi.components.securitySchemes[s];
}
processSecurityScheme(openapi.components.securitySchemes[sname], options);
}
for (let s in openapi.components.schemas) {
let sname = common.sanitiseAll(s);
let suffix = '';
if (s != sname) {
while (openapi.components.schemas[sname + suffix]) {
// @ts-ignore
suffix = (suffix ? ++suffix : 2);
}
openapi.components.schemas[sname + suffix] = openapi.components.schemas[s];
delete openapi.components.schemas[s];
}
componentNames.schemas[s] = sname + suffix;
fixUpSchema(openapi.components.schemas[sname+suffix],options)
}
// fix all $refs to their new locations (and potentially new names)
options.refmap = {};
recurse(openapi, { payload: { options: options } }, fixupRefs);
dedupeRefs(openapi,options);
for (let p in openapi.components.parameters) {
let sname = common.sanitise(p);
if (p != sname) {
if (openapi.components.parameters[sname]) {
throwError('Duplicate sanitised parameter name ' + sname, options);
}
openapi.components.parameters[sname] = openapi.components.parameters[p];
delete openapi.components.parameters[p];
}
let param = openapi.components.parameters[sname];
processParameter(param, null, null, null, sname, openapi, options);
}
for (let r in openapi.components.responses) {
let sname = common.sanitise(r);
if (r != sname) {
if (openapi.components.responses[sname]) {
throwError('Duplicate sanitised response name ' + sname, options);
|| ((response.description === '') && options.patch)) {
if (options.patch) {
if ((typeof response === 'object') && (!Array.isArray(response))) {
response.description = (statusCodes[response] || '');
}
}
else {
throwError('(Patchable) response.description is mandatory', options);
}
}
if (typeof response.schema !== 'undefined') {
fixUpSchema(response.schema,options);
if (response.schema.$ref && (typeof response.schema.$ref === 'string') && response.schema.$ref.startsWith('#/responses/')) {
response.schema.$ref = '#/components/responses/' + common.sanitise(decodeURIComponent(response.schema.$ref.replace('#/responses/', '')));
}
if (op && op.produces && (typeof op.produces === 'string')) {
if (options.patch) {
op.produces = [op.produces];
}
else {
return throwError('(Patchable) operation.produces must be an array', options);
}
}
if (openapi.produces && !Array.isArray(openapi.produces)) delete openapi.produces;
let produces = ((op ? op.produces : null) || (openapi.produces || [])).filter(common.uniqueOnly);
if (!produces.length) produces.push('*/*'); // TODO verify default
response.content = {};
function fixParamRef(param, options) {
if (param.$ref.indexOf('#/parameters/') >= 0) {
let refComponents = param.$ref.split('#/parameters/');
param.$ref = refComponents[0] + '#/components/parameters/' + common.sanitise(refComponents[1]);
}
if (param.$ref.indexOf('#/definitions/') >= 0) {
throwOrWarn('Definition used as parameter', param, options);
}
}
function getMiroComponentName(ref) {
if (ref.indexOf('#')>=0) {
ref = ref.split('#')[1].split('/').pop();
}
else {
ref = ref.split('/').pop().split('.')[0];
}
return encodeURIComponent(common.sanitise(ref));
}
for (let p in openapi.components.parameters) {
let sname = common.sanitise(p);
if (p != sname) {
if (openapi.components.parameters[sname]) {
throwError('Duplicate sanitised parameter name ' + sname, options);
}
openapi.components.parameters[sname] = openapi.components.parameters[p];
delete openapi.components.parameters[p];
}
let param = openapi.components.parameters[sname];
processParameter(param, null, null, null, sname, openapi, options);
}
for (let r in openapi.components.responses) {
let sname = common.sanitise(r);
if (r != sname) {
if (openapi.components.responses[sname]) {
throwError('Duplicate sanitised response name ' + sname, options);
}
openapi.components.responses[sname] = openapi.components.responses[r];
delete openapi.components.responses[r];
}
let response = openapi.components.responses[sname];
processResponse(response, sname, null, openapi, options);
if (response.headers) {
for (let h in response.headers) {
if (h.toLowerCase() === 'status code') {
if (options.patch) {
delete response.headers[h];
}
else {
function processResponse(response, name, op, openapi, options) {
if (!response) return false;
if (response.$ref && (typeof response.$ref === 'string')) {
if (response.$ref.indexOf('#/definitions/') >= 0) {
//response.$ref = '#/components/schemas/'+common.sanitise(response.$ref.replace('#/definitions/',''));
throwOrWarn('definition used as response: ' + response.$ref, response, options);
}
else {
if (response.$ref.startsWith('#/responses/')) {
response.$ref = '#/components/responses/' + common.sanitise(decodeURIComponent(response.$ref.replace('#/responses/', '')));
}
}
}
else {
if ((typeof response.description === 'undefined') || (response.description === null)
|| ((response.description === '') && options.patch)) {
if (options.patch) {
if ((typeof response === 'object') && (!Array.isArray(response))) {
response.description = (statusCodes[response] || '');
}
}
else {
throwError('(Patchable) response.description is mandatory', options);
}
}
if (typeof response.schema !== 'undefined') {
}
contentType.examples[e] = { $ref: '#/components/examples/'+se };
}
}
}
}
}
}
delete op["x-ms-examples"];
}
if (op.parameters && op.parameters.length === 0) delete op.parameters;
if (op.requestBody) {
let effectiveOperationId = op.operationId ? common.sanitiseAll(op.operationId) : common.sanitiseAll(method + p).toCamelCase();
let rbName = common.sanitise(op.requestBody['x-s2o-name'] || effectiveOperationId || '');
delete op.requestBody['x-s2o-name'];
let rbStr = JSON.stringify(op.requestBody);
let rbHash = common.hash(rbStr);
if (!requestBodyCache[rbHash]) {
let entry = {};
entry.name = rbName;
entry.body = op.requestBody;
entry.refs = [];
requestBodyCache[rbHash] = entry;
}
let ptr = '#/'+containerName+'/'+encodeURIComponent(jptr.jpescape(p))+'/'+method+'/requestBody';
requestBodyCache[rbHash].refs.push(ptr);
}
}
}