updated version check, multi support for 2.0

This commit is contained in:
Tony Tam
2014-10-16 02:02:56 -07:00
parent e52f83b501
commit 559221de56
7 changed files with 468 additions and 353 deletions

View File

@@ -1,5 +1,61 @@
// swagger-client.js // swagger-client.js
// version 2.1.0 // version 2.1.0-alpha.1
/**
* Array Model
**/
var ArrayModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
this.type;
this.ref;
var requiredFields = definition.enum || [];
var items = definition.items;
if(items) {
var type = items.type;
if(items.type) {
this.type = typeFromJsonSchema(type.type, type.format);
}
else {
this.ref = items['$ref'];
}
}
}
ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
var result;
modelsToIgnore = (modelsToIgnore||{})
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].createJSONSample();
}
return [ result ];
};
ArrayModel.prototype.getSampleValue = function(modelsToIgnore) {
var result;
modelsToIgnore = (modelsToIgnore || {})
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].getSampleValue(modelsToIgnore);
}
return [ result ];
}
ArrayModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
if(this.ref) {
return models[simpleRef(this.ref)].getMockSignature();
}
};
/** /**
* SwaggerAuthorizations applys the correct authorization to an operation being executed * SwaggerAuthorizations applys the correct authorization to an operation being executed
@@ -177,7 +233,58 @@ Object.keys = Object.keys || (function () {
return result; return result;
}; };
})(); })();
var SwaggerClient = function(url, options) { /**
* PrimitiveModel
**/
var PrimitiveModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
this.type;
var requiredFields = definition.enum || [];
this.type = typeFromJsonSchema(definition.type, definition.format);
}
PrimitiveModel.prototype.createJSONSample = function(modelsToIgnore) {
var result = this.type;
return result;
};
PrimitiveModel.prototype.getSampleValue = function() {
var result = this.type;
return null;
}
PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
var i;
for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i];
propertiesStr.push(prop.toString());
}
var strong = '<span class="strong">';
var stronger = '<span class="stronger">';
var strongClose = '</span>';
var classOpen = strong + this.name + ' {' + strongClose;
var classClose = strong + '}' + strongClose;
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
if (!modelsToIgnore)
modelsToIgnore = {};
modelsToIgnore[this.name] = this;
var i;
for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i];
var ref = prop['$ref'];
var model = models[ref];
if (model && typeof modelsToIgnore[ref] === 'undefined') {
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
}
}
return returnVal;
};var SwaggerClient = function(url, options) {
this.isBuilt = false; this.isBuilt = false;
this.url = null; this.url = null;
this.debug = false; this.debug = false;
@@ -237,7 +344,7 @@ SwaggerClient.prototype.build = function() {
var responseObj = resp.obj || JSON.parse(resp.data); var responseObj = resp.obj || JSON.parse(resp.data);
self.swaggerVersion = responseObj.swaggerVersion; self.swaggerVersion = responseObj.swaggerVersion;
if(responseObj.swagger && responseObj.swagger === 2.0) { if(responseObj.swagger && parseInt(responseObj.swagger) === 2) {
self.swaggerVersion = responseObj.swagger; self.swaggerVersion = responseObj.swagger;
self.buildFromSpec(responseObj); self.buildFromSpec(responseObj);
self.isValid = true; self.isValid = true;
@@ -251,9 +358,7 @@ SwaggerClient.prototype.build = function() {
}; };
if(this.spec) { if(this.spec) {
var self = this; var self = this;
setTimeout(function() { setTimeout(function() { self.buildFromSpec(self.spec); }, 10);
self.buildFromSpec(self.spec);
}, 10);
} }
else { else {
var e = (typeof window !== 'undefined' ? window : exports); var e = (typeof window !== 'undefined' ? window : exports);
@@ -265,8 +370,8 @@ SwaggerClient.prototype.build = function() {
}; };
SwaggerClient.prototype.buildFromSpec = function(response) { SwaggerClient.prototype.buildFromSpec = function(response) {
if(this.isBuilt) if(this.isBuilt) return this;
return this;
this.info = response.info || {}; this.info = response.info || {};
this.title = response.title || ''; this.title = response.title || '';
this.host = response.host || ''; this.host = response.host || '';
@@ -333,7 +438,7 @@ SwaggerClient.prototype.buildFromSpec = function(response) {
// legacy UI feature // legacy UI feature
var j; var j;
var api = null; var api;
for(j = 0; j < this.apisArray.length; j++) { for(j = 0; j < this.apisArray.length; j++) {
if(this.apisArray[j].tag === tag) { if(this.apisArray[j].tag === tag) {
api = this.apisArray[j]; api = this.apisArray[j];
@@ -425,10 +530,30 @@ var Operation = function(parent, operationId, httpMethod, path, args, definition
var i; var i;
for(i = 0; i < this.parameters.length; i++) { for(i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i]; var param = this.parameters[i];
type = this.getType(param); if(param.type === 'array') {
param.isList = true;
param.signature = this.getSignature(type, models); param.allowMultiple = true;
param.sampleJSON = this.getSampleJSON(type, models); }
var innerType = this.getType(param);
if(innerType.toString().toLowerCase() === 'boolean') {
param.allowableValues = {};
param.isList = true;
param.enum = ["true", "false"];
}
if(typeof param.enum !== 'undefined') {
var id;
param.allowableValues = {};
param.allowableValues.values = [];
param.allowableValues.descriptiveValues = [];
for(id = 0; id < param.enum.length; id++) {
var value = param.enum[id];
var isDefault = (value === param.default) ? true : false;
param.allowableValues.values.push(value);
param.allowableValues.descriptiveValues.push({value : value, isDefault: isDefault});
}
}
param.signature = this.getSignature(innerType, models);
param.sampleJSON = this.getSampleJSON(innerType, models);
param.responseClassSignature = param.signature; param.responseClassSignature = param.signature;
} }
@@ -577,14 +702,16 @@ Operation.prototype.getSignature = function(type, models) {
if (isPrimitive) { if (isPrimitive) {
return type; return type;
} else { } else {
if (listType != null) { if (listType != null)
return models[type].getMockSignature(); return models[type].getMockSignature();
} else { else
return models[type].getMockSignature(); return models[type].getMockSignature();
}
} }
}; };
/**
* gets sample response for a single operation
**/
Operation.prototype.getSampleJSON = function(type, models) { Operation.prototype.getSampleJSON = function(type, models) {
var isPrimitive, listType, sampleJson; var isPrimitive, listType, sampleJson;
@@ -613,11 +740,16 @@ Operation.prototype.getSampleJSON = function(type, models) {
} }
}; };
// legacy support /**
* legacy binding
**/
Operation.prototype["do"] = function(args, opts, callback, error, parent) { Operation.prototype["do"] = function(args, opts, callback, error, parent) {
return this.execute(args, opts, callback, error, parent); return this.execute(args, opts, callback, error, parent);
} }
/**
* executes an operation
**/
Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) { Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
var args = (arg1||{}); var args = (arg1||{});
var opts = {}, success, error; var opts = {}, success, error;
@@ -640,8 +772,9 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
var requiredParams = []; var requiredParams = [];
var missingParams = []; var missingParams = [];
// check required params // check required params, track the ones that are missing
for(var i = 0; i < this.parameters.length; i++) { var i;
for(i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i]; var param = this.parameters[i];
if(param.required === true) { if(param.required === true) {
requiredParams.push(param.name); requiredParams.push(param.name);
@@ -688,10 +821,25 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
} }
else if (param.in === 'header') else if (param.in === 'header')
headers[param.name] = args[param.name]; headers[param.name] = args[param.name];
else if (param.in === 'form') else if (param.in === 'formData')
formParams[param.name] = args[param.name]; formParams[param.name] = args[param.name];
} }
} }
// handle form params
if(headers['Content-Type'] === 'application/x-www-form-urlencoded') {
var encoded = "";
var key;
for(key in formParams) {
value = formParams[key];
if(typeof value !== 'undefined'){
if(encoded !== "")
encoded += "&";
encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
}
// todo append?
args.body = encoded;
}
var scheme = this.schemes[0]; var scheme = this.schemes[0];
var url = scheme + '://' + this.host + this.basePath + requestUrl + querystring; var url = scheme + '://' + this.host + this.basePath + requestUrl + querystring;
@@ -729,7 +877,7 @@ Operation.prototype.setContentTypes = function(args, opts) {
var i; var i;
for(i = 0; i < allDefinedParams.length; i++) { for(i = 0; i < allDefinedParams.length; i++) {
var param = allDefinedParams[i]; var param = allDefinedParams[i];
if(param.in === 'form') if(param.in === 'formData')
definedFormParams.push(param); definedFormParams.push(param);
else if(param.in === 'file') else if(param.in === 'file')
definedFileParams.push(param); definedFileParams.push(param);
@@ -788,19 +936,45 @@ Operation.prototype.setContentTypes = function(args, opts) {
Operation.prototype.encodeCollection = function(type, name, value) { Operation.prototype.encodeCollection = function(type, name, value) {
var encoded = ''; var encoded = '';
var i; var i;
if(type === 'jaxrs') { if(type === 'default' || type === 'multi') {
for(i = 0; i < value.length; i++) { for(i = 0; i < value.length; i++) {
if(i > 0) encoded += '&' if(i > 0) encoded += '&'
encoded += this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]); encoded += this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
} }
} }
else {
var separator = '';
if(type === 'csv')
separator = ',';
else if(type === 'ssv')
separator = '%20';
else if(type === 'tsv')
separator = '\\t';
else if(type === 'pipes')
separator = '|';
if(separator !== '') {
for(i = 0; i < value.length; i++) {
if(i == 0)
encoded = this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
else
encoded += separator + this.encodeQueryParam(value[i]);
}
}
}
// TODO: support the different encoding schemes here
return encoded; return encoded;
} }
/**
* TODO this encoding needs to be changed
**/
Operation.prototype.encodeQueryParam = function(arg) { Operation.prototype.encodeQueryParam = function(arg) {
return escape(arg); return escape(arg);
} }
/**
* TODO revisit, might not want to leave '/'
**/
Operation.prototype.encodePathParam = function(pathParam) { Operation.prototype.encodePathParam = function(pathParam) {
var encParts, part, parts, _i, _len; var encParts, part, parts, _i, _len;
pathParam = pathParam.toString(); pathParam = pathParam.toString();
@@ -817,127 +991,6 @@ Operation.prototype.encodePathParam = function(pathParam) {
} }
}; };
Operation.prototype.encodePathParam = function(pathParam) {
var encParts, part, parts, _i, _len;
pathParam = pathParam.toString();
if (pathParam.indexOf('/') === -1) {
return encodeURIComponent(pathParam);
} else {
parts = pathParam.split('/');
encParts = [];
for (_i = 0, _len = parts.length; _i < _len; _i++) {
part = parts[_i];
encParts.push(encodeURIComponent(part));
}
return encParts.join('/');
}
};
var ArrayModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
this.type;
this.ref;
var requiredFields = definition.enum || [];
var items = definition.items;
if(items) {
var type = items.type;
if(items.type) {
this.type = typeFromJsonSchema(type.type, type.format);
}
else {
this.ref = items['$ref'];
}
}
}
ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
var result;
var modelsToIgnore = (modelsToIgnore||[])
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].createJSONSample();
}
return [ result ];
};
ArrayModel.prototype.getSampleValue = function() {
var result;
var modelsToIgnore = (modelsToIgnore||[])
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].getSampleValue();
}
return [ result ];
}
ArrayModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
if(this.ref) {
return models[simpleRef(this.ref)].getMockSignature();
}
};
var PrimitiveModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
this.type;
var requiredFields = definition.enum || [];
this.type = typeFromJsonSchema(definition.type, definition.format);
}
PrimitiveModel.prototype.createJSONSample = function(modelsToIgnore) {
var result = this.type;
return result;
};
PrimitiveModel.prototype.getSampleValue = function() {
var result = this.type;
return null;
}
PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
var i;
for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i];
propertiesStr.push(prop.toString());
}
var strong = '<span class="strong">';
var stronger = '<span class="stronger">';
var strongClose = '</span>';
var classOpen = strong + this.name + ' {' + strongClose;
var classClose = strong + '}' + strongClose;
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
if (!modelsToIgnore)
modelsToIgnore = [];
modelsToIgnore.push(this.name);
var i;
for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i];
var ref = prop['$ref'];
var model = models[ref];
if (model && modelsToIgnore.indexOf(ref) === -1) {
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
}
}
return returnVal;
};
var Model = function(name, definition) { var Model = function(name, definition) {
this.name = name; this.name = name;
this.definition = definition || {}; this.definition = definition || {};
@@ -959,22 +1012,23 @@ var Model = function(name, definition) {
Model.prototype.createJSONSample = function(modelsToIgnore) { Model.prototype.createJSONSample = function(modelsToIgnore) {
var result = {}; var result = {};
var modelsToIgnore = (modelsToIgnore||[]) modelsToIgnore = (modelsToIgnore||{})
modelsToIgnore.push(this.name); modelsToIgnore[this.name] = this;
for (var i = 0; i < this.properties.length; i++) { var i;
for (i = 0; i < this.properties.length; i++) {
prop = this.properties[i]; prop = this.properties[i];
result[prop.name] = prop.getSampleValue(modelsToIgnore); result[prop.name] = prop.getSampleValue(modelsToIgnore);
} }
modelsToIgnore.pop(this.name); delete modelsToIgnore[this.name];
return result; return result;
}; };
Model.prototype.getSampleValue = function() { Model.prototype.getSampleValue = function(modelsToIgnore) {
var i; var i;
var obj = {}; var obj = {};
for(i = 0; i < this.properties.length; i++ ) { for(i = 0; i < this.properties.length; i++ ) {
var property = this.properties[i]; var property = this.properties[i];
obj[property.name] = property.sampleValue(); obj[property.name] = property.sampleValue(false, modelsToIgnore);
} }
return obj; return obj;
} }
@@ -994,15 +1048,15 @@ Model.prototype.getMockSignature = function(modelsToIgnore) {
var classClose = strong + '}' + strongClose; var classClose = strong + '}' + strongClose;
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose; var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
if (!modelsToIgnore) if (!modelsToIgnore)
modelsToIgnore = []; modelsToIgnore = {};
modelsToIgnore.push(this.name); modelsToIgnore[this.name] = this;
var i; var i;
for (i = 0; i < this.properties.length; i++) { for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i]; var prop = this.properties[i];
var ref = prop['$ref']; var ref = prop['$ref'];
var model = models[ref]; var model = models[ref];
if (model && modelsToIgnore.indexOf(ref) === -1) { if (model && typeof modelsToIgnore === 'undefined') {
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore)); returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
} }
} }
@@ -1029,8 +1083,8 @@ var Property = function(name, obj, required) {
this.example = obj.example || null; this.example = obj.example || null;
} }
Property.prototype.getSampleValue = function () { Property.prototype.getSampleValue = function (modelsToIgnore) {
return this.sampleValue(false); return this.sampleValue(false, modelsToIgnore);
} }
Property.prototype.isArray = function () { Property.prototype.isArray = function () {
@@ -1043,13 +1097,14 @@ Property.prototype.isArray = function () {
Property.prototype.sampleValue = function(isArray, ignoredModels) { Property.prototype.sampleValue = function(isArray, ignoredModels) {
isArray = (isArray || this.isArray()); isArray = (isArray || this.isArray());
ignoredModels = (ignoredModels || {}) ignoredModels = (ignoredModels || {});
var type = getStringSignature(this.obj); var type = getStringSignature(this.obj);
var output; var output;
if(this['$ref']) { if(this['$ref']) {
var refModel = models[this['$ref']]; var refModel = models[this['$ref']];
if(refModel && typeof ignoredModels[refModel] === 'undefined') { if(refModel && typeof ignoredModels[type] === 'undefined') {
ignoredModels[type] = this;
output = refModel.getSampleValue(ignoredModels); output = refModel.getSampleValue(ignoredModels);
} }
else else
@@ -1057,31 +1112,27 @@ Property.prototype.sampleValue = function(isArray, ignoredModels) {
} }
else if(this.example) else if(this.example)
output = this.example; output = this.example;
else if(type === 'date-time') { else if(type === 'date-time')
output = new Date().toISOString(); output = new Date().toISOString();
} else if(type === 'string')
else if(type === 'string') {
output = 'string'; output = 'string';
} else if(type === 'integer')
else if(type === 'integer') {
output = 0; output = 0;
} else if(type === 'long')
else if(type === 'long') {
output = 0; output = 0;
} else if(type === 'float')
else if(type === 'float') {
output = 0.0; output = 0.0;
} else if(type === 'double')
else if(type === 'double') {
output = 0.0; output = 0.0;
} else if(type === 'boolean')
else if(type === 'boolean') {
output = true; output = true;
}
else else
output = {}; output = {};
if(isArray) return [output]; ignoredModels[type] = output;
else return output; if(isArray)
return [output];
else
return output;
} }
getStringSignature = function(obj) { getStringSignature = function(obj) {
@@ -1161,7 +1212,9 @@ e.authorizations = new SwaggerAuthorizations();
e.ApiKeyAuthorization = ApiKeyAuthorization; e.ApiKeyAuthorization = ApiKeyAuthorization;
e.PasswordAuthorization = PasswordAuthorization; e.PasswordAuthorization = PasswordAuthorization;
e.CookieAuthorization = CookieAuthorization; e.CookieAuthorization = CookieAuthorization;
e.SwaggerClient = SwaggerClient;/** e.SwaggerClient = SwaggerClient;
/**
* SwaggerHttp is a wrapper for executing requests * SwaggerHttp is a wrapper for executing requests
*/ */
var SwaggerHttp = function() {}; var SwaggerHttp = function() {};
@@ -1295,7 +1348,7 @@ var ShredHttpClient = function(options) {
} }
else else
this.Shred = require("shred"); this.Shred = require("shred");
this.shred = new this.Shred(); this.shred = new this.Shred(options);
} }
ShredHttpClient.prototype.initShred = function () { ShredHttpClient.prototype.initShred = function () {

17
dist/lib/swagger.js vendored
View File

@@ -975,9 +975,22 @@
var param = params[i]; var param = params[i];
if (param.paramType === 'query') { if (param.paramType === 'query') {
if (args[param.name] !== undefined) { if (args[param.name] !== undefined) {
var value = args[param.name];
if (queryParams !== '') if (queryParams !== '')
queryParams += "&"; queryParams += '&';
queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]); if (Array.isArray(value)) {
var j;
var output = '';
for(j = 0; j < value.length; j++) {
if(j > 0)
output += ',';
output += encodeURIComponent(value[j]);
}
queryParams += encodeURIComponent(param.name) + '=' + output;
}
else {
queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
}
} }
} }
} }

4
dist/swagger-ui.js vendored
View File

@@ -1,5 +1,5 @@
// swagger-ui.js // swagger-ui.js
// version 2.1.0 // version 2.1.0-alpha.1
$(function() { $(function() {
// Helper function for vertically aligning DOM elements // Helper function for vertically aligning DOM elements
@@ -1977,7 +1977,7 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
} }
} }
if (options.length > 0) { if (options.length > 0) {
return options.join(","); return options;
} else { } else {
return null; return null;
} }

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,61 @@
// swagger-client.js // swagger-client.js
// version 2.1.0 // version 2.1.0-alpha.1
/**
* Array Model
**/
var ArrayModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
this.type;
this.ref;
var requiredFields = definition.enum || [];
var items = definition.items;
if(items) {
var type = items.type;
if(items.type) {
this.type = typeFromJsonSchema(type.type, type.format);
}
else {
this.ref = items['$ref'];
}
}
}
ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
var result;
modelsToIgnore = (modelsToIgnore||{})
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].createJSONSample();
}
return [ result ];
};
ArrayModel.prototype.getSampleValue = function(modelsToIgnore) {
var result;
modelsToIgnore = (modelsToIgnore || {})
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].getSampleValue(modelsToIgnore);
}
return [ result ];
}
ArrayModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
if(this.ref) {
return models[simpleRef(this.ref)].getMockSignature();
}
};
/** /**
* SwaggerAuthorizations applys the correct authorization to an operation being executed * SwaggerAuthorizations applys the correct authorization to an operation being executed
@@ -177,7 +233,58 @@ Object.keys = Object.keys || (function () {
return result; return result;
}; };
})(); })();
var SwaggerClient = function(url, options) { /**
* PrimitiveModel
**/
var PrimitiveModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
this.type;
var requiredFields = definition.enum || [];
this.type = typeFromJsonSchema(definition.type, definition.format);
}
PrimitiveModel.prototype.createJSONSample = function(modelsToIgnore) {
var result = this.type;
return result;
};
PrimitiveModel.prototype.getSampleValue = function() {
var result = this.type;
return null;
}
PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
var i;
for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i];
propertiesStr.push(prop.toString());
}
var strong = '<span class="strong">';
var stronger = '<span class="stronger">';
var strongClose = '</span>';
var classOpen = strong + this.name + ' {' + strongClose;
var classClose = strong + '}' + strongClose;
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
if (!modelsToIgnore)
modelsToIgnore = {};
modelsToIgnore[this.name] = this;
var i;
for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i];
var ref = prop['$ref'];
var model = models[ref];
if (model && typeof modelsToIgnore[ref] === 'undefined') {
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
}
}
return returnVal;
};var SwaggerClient = function(url, options) {
this.isBuilt = false; this.isBuilt = false;
this.url = null; this.url = null;
this.debug = false; this.debug = false;
@@ -237,7 +344,7 @@ SwaggerClient.prototype.build = function() {
var responseObj = resp.obj || JSON.parse(resp.data); var responseObj = resp.obj || JSON.parse(resp.data);
self.swaggerVersion = responseObj.swaggerVersion; self.swaggerVersion = responseObj.swaggerVersion;
if(responseObj.swagger && responseObj.swagger === 2.0) { if(responseObj.swagger && parseInt(responseObj.swagger) === 2) {
self.swaggerVersion = responseObj.swagger; self.swaggerVersion = responseObj.swagger;
self.buildFromSpec(responseObj); self.buildFromSpec(responseObj);
self.isValid = true; self.isValid = true;
@@ -251,9 +358,7 @@ SwaggerClient.prototype.build = function() {
}; };
if(this.spec) { if(this.spec) {
var self = this; var self = this;
setTimeout(function() { setTimeout(function() { self.buildFromSpec(self.spec); }, 10);
self.buildFromSpec(self.spec);
}, 10);
} }
else { else {
var e = (typeof window !== 'undefined' ? window : exports); var e = (typeof window !== 'undefined' ? window : exports);
@@ -265,8 +370,8 @@ SwaggerClient.prototype.build = function() {
}; };
SwaggerClient.prototype.buildFromSpec = function(response) { SwaggerClient.prototype.buildFromSpec = function(response) {
if(this.isBuilt) if(this.isBuilt) return this;
return this;
this.info = response.info || {}; this.info = response.info || {};
this.title = response.title || ''; this.title = response.title || '';
this.host = response.host || ''; this.host = response.host || '';
@@ -333,7 +438,7 @@ SwaggerClient.prototype.buildFromSpec = function(response) {
// legacy UI feature // legacy UI feature
var j; var j;
var api = null; var api;
for(j = 0; j < this.apisArray.length; j++) { for(j = 0; j < this.apisArray.length; j++) {
if(this.apisArray[j].tag === tag) { if(this.apisArray[j].tag === tag) {
api = this.apisArray[j]; api = this.apisArray[j];
@@ -426,7 +531,10 @@ var Operation = function(parent, operationId, httpMethod, path, args, definition
for(i = 0; i < this.parameters.length; i++) { for(i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i]; var param = this.parameters[i];
type = this.getType(param); type = this.getType(param);
if(type.toLowerCase() === 'boolean') {
param.allowableValues = {};
param.allowableValues.values = ["true", "false"];
}
param.signature = this.getSignature(type, models); param.signature = this.getSignature(type, models);
param.sampleJSON = this.getSampleJSON(type, models); param.sampleJSON = this.getSampleJSON(type, models);
param.responseClassSignature = param.signature; param.responseClassSignature = param.signature;
@@ -577,14 +685,16 @@ Operation.prototype.getSignature = function(type, models) {
if (isPrimitive) { if (isPrimitive) {
return type; return type;
} else { } else {
if (listType != null) { if (listType != null)
return models[type].getMockSignature(); return models[type].getMockSignature();
} else { else
return models[type].getMockSignature(); return models[type].getMockSignature();
}
} }
}; };
/**
* gets sample response for a single operation
**/
Operation.prototype.getSampleJSON = function(type, models) { Operation.prototype.getSampleJSON = function(type, models) {
var isPrimitive, listType, sampleJson; var isPrimitive, listType, sampleJson;
@@ -613,11 +723,16 @@ Operation.prototype.getSampleJSON = function(type, models) {
} }
}; };
// legacy support /**
* legacy binding
**/
Operation.prototype["do"] = function(args, opts, callback, error, parent) { Operation.prototype["do"] = function(args, opts, callback, error, parent) {
return this.execute(args, opts, callback, error, parent); return this.execute(args, opts, callback, error, parent);
} }
/**
* executes an operation
**/
Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) { Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
var args = (arg1||{}); var args = (arg1||{});
var opts = {}, success, error; var opts = {}, success, error;
@@ -640,8 +755,9 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
var requiredParams = []; var requiredParams = [];
var missingParams = []; var missingParams = [];
// check required params // check required params, track the ones that are missing
for(var i = 0; i < this.parameters.length; i++) { var i;
for(i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i]; var param = this.parameters[i];
if(param.required === true) { if(param.required === true) {
requiredParams.push(param.name); requiredParams.push(param.name);
@@ -688,10 +804,25 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
} }
else if (param.in === 'header') else if (param.in === 'header')
headers[param.name] = args[param.name]; headers[param.name] = args[param.name];
else if (param.in === 'form') else if (param.in === 'formData')
formParams[param.name] = args[param.name]; formParams[param.name] = args[param.name];
} }
} }
// handle form params
if(headers['Content-Type'] === 'application/x-www-form-urlencoded') {
var encoded = "";
var key;
for(key in formParams) {
value = formParams[key];
if(typeof value !== 'undefined'){
if(encoded !== "")
encoded += "&";
encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
}
// todo append?
args.body = encoded;
}
var scheme = this.schemes[0]; var scheme = this.schemes[0];
var url = scheme + '://' + this.host + this.basePath + requestUrl + querystring; var url = scheme + '://' + this.host + this.basePath + requestUrl + querystring;
@@ -729,7 +860,7 @@ Operation.prototype.setContentTypes = function(args, opts) {
var i; var i;
for(i = 0; i < allDefinedParams.length; i++) { for(i = 0; i < allDefinedParams.length; i++) {
var param = allDefinedParams[i]; var param = allDefinedParams[i];
if(param.in === 'form') if(param.in === 'formData')
definedFormParams.push(param); definedFormParams.push(param);
else if(param.in === 'file') else if(param.in === 'file')
definedFileParams.push(param); definedFileParams.push(param);
@@ -788,19 +919,45 @@ Operation.prototype.setContentTypes = function(args, opts) {
Operation.prototype.encodeCollection = function(type, name, value) { Operation.prototype.encodeCollection = function(type, name, value) {
var encoded = ''; var encoded = '';
var i; var i;
if(type === 'jaxrs') { if(type === 'default' || type === 'multi') {
for(i = 0; i < value.length; i++) { for(i = 0; i < value.length; i++) {
if(i > 0) encoded += '&' if(i > 0) encoded += '&'
encoded += this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]); encoded += this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
} }
} }
else {
var separator = '';
if(type === 'csv')
separator = ',';
else if(type === 'ssv')
separator = '%20';
else if(type === 'tsv')
separator = '\\t';
else if(type === 'pipes')
separator = '|';
if(separator !== '') {
for(i = 0; i < value.length; i++) {
if(i == 0)
encoded = this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]);
else
encoded += separator + this.encodeQueryParam(value[i]);
}
}
}
// TODO: support the different encoding schemes here
return encoded; return encoded;
} }
/**
* TODO this encoding needs to be changed
**/
Operation.prototype.encodeQueryParam = function(arg) { Operation.prototype.encodeQueryParam = function(arg) {
return escape(arg); return escape(arg);
} }
/**
* TODO revisit, might not want to leave '/'
**/
Operation.prototype.encodePathParam = function(pathParam) { Operation.prototype.encodePathParam = function(pathParam) {
var encParts, part, parts, _i, _len; var encParts, part, parts, _i, _len;
pathParam = pathParam.toString(); pathParam = pathParam.toString();
@@ -817,127 +974,6 @@ Operation.prototype.encodePathParam = function(pathParam) {
} }
}; };
Operation.prototype.encodePathParam = function(pathParam) {
var encParts, part, parts, _i, _len;
pathParam = pathParam.toString();
if (pathParam.indexOf('/') === -1) {
return encodeURIComponent(pathParam);
} else {
parts = pathParam.split('/');
encParts = [];
for (_i = 0, _len = parts.length; _i < _len; _i++) {
part = parts[_i];
encParts.push(encodeURIComponent(part));
}
return encParts.join('/');
}
};
var ArrayModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
this.type;
this.ref;
var requiredFields = definition.enum || [];
var items = definition.items;
if(items) {
var type = items.type;
if(items.type) {
this.type = typeFromJsonSchema(type.type, type.format);
}
else {
this.ref = items['$ref'];
}
}
}
ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
var result;
var modelsToIgnore = (modelsToIgnore||[])
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].createJSONSample();
}
return [ result ];
};
ArrayModel.prototype.getSampleValue = function() {
var result;
var modelsToIgnore = (modelsToIgnore||[])
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].getSampleValue();
}
return [ result ];
}
ArrayModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
if(this.ref) {
return models[simpleRef(this.ref)].getMockSignature();
}
};
var PrimitiveModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
this.type;
var requiredFields = definition.enum || [];
this.type = typeFromJsonSchema(definition.type, definition.format);
}
PrimitiveModel.prototype.createJSONSample = function(modelsToIgnore) {
var result = this.type;
return result;
};
PrimitiveModel.prototype.getSampleValue = function() {
var result = this.type;
return null;
}
PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
var i;
for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i];
propertiesStr.push(prop.toString());
}
var strong = '<span class="strong">';
var stronger = '<span class="stronger">';
var strongClose = '</span>';
var classOpen = strong + this.name + ' {' + strongClose;
var classClose = strong + '}' + strongClose;
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
if (!modelsToIgnore)
modelsToIgnore = [];
modelsToIgnore.push(this.name);
var i;
for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i];
var ref = prop['$ref'];
var model = models[ref];
if (model && modelsToIgnore.indexOf(ref) === -1) {
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
}
}
return returnVal;
};
var Model = function(name, definition) { var Model = function(name, definition) {
this.name = name; this.name = name;
this.definition = definition || {}; this.definition = definition || {};
@@ -959,22 +995,23 @@ var Model = function(name, definition) {
Model.prototype.createJSONSample = function(modelsToIgnore) { Model.prototype.createJSONSample = function(modelsToIgnore) {
var result = {}; var result = {};
var modelsToIgnore = (modelsToIgnore||[]) modelsToIgnore = (modelsToIgnore||{})
modelsToIgnore.push(this.name); modelsToIgnore[this.name] = this;
for (var i = 0; i < this.properties.length; i++) { var i;
for (i = 0; i < this.properties.length; i++) {
prop = this.properties[i]; prop = this.properties[i];
result[prop.name] = prop.getSampleValue(modelsToIgnore); result[prop.name] = prop.getSampleValue(modelsToIgnore);
} }
modelsToIgnore.pop(this.name); delete modelsToIgnore[this.name];
return result; return result;
}; };
Model.prototype.getSampleValue = function() { Model.prototype.getSampleValue = function(modelsToIgnore) {
var i; var i;
var obj = {}; var obj = {};
for(i = 0; i < this.properties.length; i++ ) { for(i = 0; i < this.properties.length; i++ ) {
var property = this.properties[i]; var property = this.properties[i];
obj[property.name] = property.sampleValue(); obj[property.name] = property.sampleValue(false, modelsToIgnore);
} }
return obj; return obj;
} }
@@ -994,15 +1031,15 @@ Model.prototype.getMockSignature = function(modelsToIgnore) {
var classClose = strong + '}' + strongClose; var classClose = strong + '}' + strongClose;
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose; var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
if (!modelsToIgnore) if (!modelsToIgnore)
modelsToIgnore = []; modelsToIgnore = {};
modelsToIgnore.push(this.name); modelsToIgnore[this.name] = this;
var i; var i;
for (i = 0; i < this.properties.length; i++) { for (i = 0; i < this.properties.length; i++) {
var prop = this.properties[i]; var prop = this.properties[i];
var ref = prop['$ref']; var ref = prop['$ref'];
var model = models[ref]; var model = models[ref];
if (model && modelsToIgnore.indexOf(ref) === -1) { if (model && typeof modelsToIgnore === 'undefined') {
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore)); returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
} }
} }
@@ -1029,8 +1066,8 @@ var Property = function(name, obj, required) {
this.example = obj.example || null; this.example = obj.example || null;
} }
Property.prototype.getSampleValue = function () { Property.prototype.getSampleValue = function (modelsToIgnore) {
return this.sampleValue(false); return this.sampleValue(false, modelsToIgnore);
} }
Property.prototype.isArray = function () { Property.prototype.isArray = function () {
@@ -1043,13 +1080,14 @@ Property.prototype.isArray = function () {
Property.prototype.sampleValue = function(isArray, ignoredModels) { Property.prototype.sampleValue = function(isArray, ignoredModels) {
isArray = (isArray || this.isArray()); isArray = (isArray || this.isArray());
ignoredModels = (ignoredModels || {}) ignoredModels = (ignoredModels || {});
var type = getStringSignature(this.obj); var type = getStringSignature(this.obj);
var output; var output;
if(this['$ref']) { if(this['$ref']) {
var refModel = models[this['$ref']]; var refModel = models[this['$ref']];
if(refModel && typeof ignoredModels[refModel] === 'undefined') { if(refModel && typeof ignoredModels[type] === 'undefined') {
ignoredModels[type] = this;
output = refModel.getSampleValue(ignoredModels); output = refModel.getSampleValue(ignoredModels);
} }
else else
@@ -1057,31 +1095,27 @@ Property.prototype.sampleValue = function(isArray, ignoredModels) {
} }
else if(this.example) else if(this.example)
output = this.example; output = this.example;
else if(type === 'date-time') { else if(type === 'date-time')
output = new Date().toISOString(); output = new Date().toISOString();
} else if(type === 'string')
else if(type === 'string') {
output = 'string'; output = 'string';
} else if(type === 'integer')
else if(type === 'integer') {
output = 0; output = 0;
} else if(type === 'long')
else if(type === 'long') {
output = 0; output = 0;
} else if(type === 'float')
else if(type === 'float') {
output = 0.0; output = 0.0;
} else if(type === 'double')
else if(type === 'double') {
output = 0.0; output = 0.0;
} else if(type === 'boolean')
else if(type === 'boolean') {
output = true; output = true;
}
else else
output = {}; output = {};
if(isArray) return [output]; ignoredModels[type] = output;
else return output; if(isArray)
return [output];
else
return output;
} }
getStringSignature = function(obj) { getStringSignature = function(obj) {
@@ -1161,7 +1195,9 @@ e.authorizations = new SwaggerAuthorizations();
e.ApiKeyAuthorization = ApiKeyAuthorization; e.ApiKeyAuthorization = ApiKeyAuthorization;
e.PasswordAuthorization = PasswordAuthorization; e.PasswordAuthorization = PasswordAuthorization;
e.CookieAuthorization = CookieAuthorization; e.CookieAuthorization = CookieAuthorization;
e.SwaggerClient = SwaggerClient;/** e.SwaggerClient = SwaggerClient;
/**
* SwaggerHttp is a wrapper for executing requests * SwaggerHttp is a wrapper for executing requests
*/ */
var SwaggerHttp = function() {}; var SwaggerHttp = function() {};
@@ -1295,7 +1331,7 @@ var ShredHttpClient = function(options) {
} }
else else
this.Shred = require("shred"); this.Shred = require("shred");
this.shred = new this.Shred(); this.shred = new this.Shred(options);
} }
ShredHttpClient.prototype.initShred = function () { ShredHttpClient.prototype.initShred = function () {

View File

@@ -975,9 +975,22 @@
var param = params[i]; var param = params[i];
if (param.paramType === 'query') { if (param.paramType === 'query') {
if (args[param.name] !== undefined) { if (args[param.name] !== undefined) {
var value = args[param.name];
if (queryParams !== '') if (queryParams !== '')
queryParams += "&"; queryParams += '&';
queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]); if (Array.isArray(value)) {
var j;
var output = '';
for(j = 0; j < value.length; j++) {
if(j > 0)
output += ',';
output += encodeURIComponent(value[j]);
}
queryParams += encodeURIComponent(param.name) + '=' + output;
}
else {
queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
}
} }
} }
} }

View File

@@ -266,7 +266,7 @@ class OperationView extends Backbone.View
options = [] options = []
options.push opt.value for opt in select.options when opt.selected options.push opt.value for opt in select.options when opt.selected
if options.length > 0 if options.length > 0
options.join "," options
else else
null null