Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var banner = ['/**',
var basename = 'swagger-client';
var paths = {
sources: ['src/js/*.js'],
tests: ['test/*.js'],
tests: ['test/*.js', 'test/compat/*.js'],
dist: 'lib'
};

Expand Down
87 changes: 52 additions & 35 deletions lib/swagger-client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* swagger-client - swagger.js is a javascript client for use with swaggering APIs.
* @version v2.1.2-M1
* @version v2.1.3-M1
* @link http://swagger.io
* @license apache 2.0
*/
Expand Down Expand Up @@ -82,10 +82,10 @@ SwaggerAuthorizations.prototype.remove = function(name) {

SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
var status = null;
var key, value, result;
var key, name, value, result;

// if the "authorizations" key is undefined, or has an empty array, add all keys
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length == 0) {
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length === 0) {
for (key in this.authz) {
value = this.authz[key];
result = value.apply(obj, authorizations);
Expand Down Expand Up @@ -280,6 +280,10 @@ PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
}
return returnVal;
};
var addModel = function(name, model) {
models[name] = model;
};

var SwaggerClient = function(url, options) {
this.isBuilt = false;
this.url = null;
Expand All @@ -291,13 +295,14 @@ var SwaggerClient = function(url, options) {
this.isValid = false;
this.info = null;
this.useJQuery = false;
this.resourceCount = 0;

if(typeof url !== 'undefined')
return this.initialize(url, options);
};

SwaggerClient.prototype.initialize = function (url, options) {
this.models = models;
this.models = models = {};

options = (options||{});

Expand Down Expand Up @@ -386,7 +391,6 @@ SwaggerClient.prototype.build = function(mock) {
return obj;
new SwaggerHttp().execute(obj);
}

return this;
};

Expand Down Expand Up @@ -502,8 +506,11 @@ SwaggerClient.prototype.buildFromSpec = function(response) {
}
}
this.isBuilt = true;
if (this.success)
if (this.success) {
this.isValid = true;
this.isBuilt = true;
this.success();
}
return this;
};

Expand Down Expand Up @@ -592,18 +599,18 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
}
}

var i, model;

if(definitions) {
// add to global models
var key;
for(key in this.definitions) {
var model = new Model(key, definitions[key]);
model = new Model(key, definitions[key]);
if(model) {
models[key] = model;
}
}
}

var i;
for(i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
if(param.type === 'array') {
Expand Down Expand Up @@ -641,7 +648,7 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
param.responseClassSignature = param.signature;
}

var defaultResponseCode, response, model, responses = this.responses;
var defaultResponseCode, response, responses = this.responses;

if(responses['200']) {
response = responses['200'];
Expand Down Expand Up @@ -995,9 +1002,9 @@ Operation.prototype.execute = function(arg1, arg2, arg3, arg4, parent) {
var allHeaders = this.getHeaderParams(args);
var contentTypeHeaders = this.setContentTypes(args, opts);

var headers = {};
for (var attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
for (var attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
var headers = {}, attrname;
for (attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
for (attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }

var body = this.getBody(headers, args);
var url = this.urlify(args);
Expand Down Expand Up @@ -1439,7 +1446,7 @@ Property.prototype.toString = function() {
type = '';
}
else {
this.schema.type;
type = this.schema.type;
}

if (this.default)
Expand Down Expand Up @@ -1500,7 +1507,7 @@ Property.prototype.toString = function() {

optionHtml = function(label, value) {
return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
}
};

typeFromJsonSchema = function(type, format) {
var str;
Expand Down Expand Up @@ -1533,7 +1540,7 @@ var cookies = {};
var models = {};

SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
if (response.apiVersion != null) {
if (response.apiVersion !== null) {
this.apiVersion = response.apiVersion;
}
this.apis = {};
Expand Down Expand Up @@ -1569,6 +1576,7 @@ SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
this.apisArray.push(res);
} else {
var k;
this.expectedResourceCount = response.apis.length;
for (k = 0; k < response.apis.length; k++) {
var resource = response.apis[k];
res = new SwaggerResource(resource, this);
Expand All @@ -1577,15 +1585,21 @@ SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
}
}
this.isValid = true;
return this;
};

SwaggerClient.prototype.finish = function() {
if (typeof this.success === 'function') {
this.isValid = true;
this.isBuilt = true;
this.selfReflect();
this.success();
}
return this;
}
};

SwaggerClient.prototype.buildFrom1_1Spec = function (response) {
log('This API is using a deprecated version of Swagger! Please see http://github.com/wordnik/swagger-core/wiki for more info');
if (response.apiVersion != null)
if (response.apiVersion !== null)
this.apiVersion = response.apiVersion;
this.apis = {};
this.apisArray = [];
Expand Down Expand Up @@ -1631,7 +1645,7 @@ SwaggerClient.prototype.buildFrom1_1Spec = function (response) {

SwaggerClient.prototype.convertInfo = function (resp) {
if(typeof resp == 'object') {
var info = {}
var info = {};

info.title = resp.title;
info.description = resp.description;
Expand Down Expand Up @@ -1660,9 +1674,6 @@ SwaggerClient.prototype.selfReflect = function () {
}
this.setConsolidatedModels();
this.ready = true;
if (typeof this.success === 'function') {
return this.success();
}
};

SwaggerClient.prototype.setConsolidatedModels = function () {
Expand Down Expand Up @@ -1702,7 +1713,7 @@ var SwaggerResource = function (resourceObj, api) {
this.operations = {};
this.operationsArray = [];
this.modelsArray = [];
this.models = {};
this.models = api.models || {};
this.rawModels = {};
this.useJQuery = (typeof api.useJQuery !== 'undefined') ? api.useJQuery : null;

Expand All @@ -1728,9 +1739,11 @@ var SwaggerResource = function (resourceObj, api) {
on: {
response: function (resp) {
var responseObj = resp.obj || JSON.parse(resp.data);
_this.api.resourceCount += 1;
return _this.addApiDeclaration(responseObj);
},
error: function (response) {
_this.api.resourceCount += 1;
return _this.api.fail('Unable to read api \'' +
_this.name + '\' from path ' + _this.url + ' (server returned ' + response.statusText + ')');
}
Expand Down Expand Up @@ -1784,7 +1797,9 @@ SwaggerResource.prototype.addApiDeclaration = function (response) {
}
this.api[this.name] = this;
this.ready = true;
return this.api.selfReflect();
if(this.api.resourceCount === this.api.expectedResourceCount)
this.api.finish();
return this;
};

SwaggerResource.prototype.addModels = function (models) {
Expand Down Expand Up @@ -2077,7 +2092,7 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no

this.path = this.path.replace('{format}', 'json');
this.method = this.method.toLowerCase();
this.isGetMethod = this.method === 'GET';
this.isGetMethod = this.method === 'get';

var i, j, v;
this.resourceName = this.resource.name;
Expand Down Expand Up @@ -2128,17 +2143,17 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no
}
}
}
else if (param.allowableValues != null) {
else if (param.allowableValues) {
if (param.allowableValues.valueType === 'RANGE')
param.isRange = true;
else
param.isList = true;
if (param.allowableValues != null) {
if (param.allowableValues) {
param.allowableValues.descriptiveValues = [];
if (param.allowableValues.values) {
for (j = 0; j < param.allowableValues.values.length; j++) {
v = param.allowableValues.values[j];
if (param.defaultValue != null) {
if (param.defaultValue !== null) {
param.allowableValues.descriptiveValues.push({
value: String(v),
isDefault: (v === param.defaultValue)
Expand Down Expand Up @@ -2208,7 +2223,7 @@ SwaggerOperation.prototype.getSampleJSON = function (type, models) {
var isPrimitive, listType, val;
listType = this.isListType(type);
isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true;
val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[type].createJSONSample());
val = isPrimitive ? void 0 : (listType ? models[listType].createJSONSample() : models[type].createJSONSample());
if (val) {
val = listType ? [val] : val;
if (typeof val == 'string')
Expand Down Expand Up @@ -2243,7 +2258,7 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {
callback = function (response) {
var content;
content = null;
if (response != null) {
if (response !== null) {
content = response.data;
} else {
content = 'no data';
Expand All @@ -2254,7 +2269,7 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {

params = {};
params.headers = [];
if (args.headers != null) {
if (args.headers) {
params.headers = args.headers;
delete args.headers;
}
Expand Down Expand Up @@ -2375,7 +2390,7 @@ SwaggerOperation.prototype.urlify = function (args) {
}
}
}
if ((queryParams != null) && queryParams.length > 0)
if ((queryParams) && queryParams.length > 0)
url += '?' + queryParams;
return url;
};
Expand Down Expand Up @@ -2584,7 +2599,7 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa
}

var obj;
if (!((this.headers != null) && (this.headers.mock != null))) {
if (!((this.headers) && (this.headers.mock))) {
obj = {
url: this.url,
method: this.type,
Expand Down Expand Up @@ -2953,7 +2968,9 @@ e.ApiKeyAuthorization = ApiKeyAuthorization;
e.PasswordAuthorization = PasswordAuthorization;
e.CookieAuthorization = CookieAuthorization;
e.SwaggerClient = SwaggerClient;
e.SwaggerApi = SwaggerClient;
e.Operation = Operation;
e.Model = Model;
e.models = models;
e.addModel = addModel;

})();
4 changes: 2 additions & 2 deletions lib/swagger-client.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "swagger-client",
"author": "Tony Tam <fehguy@gmail.com>",
"description": "swagger.js is a javascript client for use with swaggering APIs.",
"version": "2.1.2-M1",
"version": "2.1.3-M1",
"homepage": "http://swagger.io",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions src/js/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ SwaggerAuthorizations.prototype.remove = function(name) {

SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
var status = null;
var key, value, result;
var key, name, value, result;

// if the "authorizations" key is undefined, or has an empty array, add all keys
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length == 0) {
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length === 0) {
for (key in this.authz) {
value = this.authz[key];
result = value.apply(obj, authorizations);
Expand Down
Loading