Skip to content

Commit 3a3b26c

Browse files
committed
Merge pull request #237 from swagger-api/feature/legacy-cleanup
Feature/legacy cleanup
2 parents 968f56e + bb5549b commit 3a3b26c

22 files changed

+224
-208
lines changed

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var banner = ['/**',
2121
var basename = 'swagger-client';
2222
var paths = {
2323
sources: ['src/js/*.js'],
24-
tests: ['test/*.js'],
24+
tests: ['test/*.js', 'test/compat/*.js'],
2525
dist: 'lib'
2626
};
2727

lib/swagger-client.js

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* swagger-client - swagger.js is a javascript client for use with swaggering APIs.
3-
* @version v2.1.2-M1
3+
* @version v2.1.3-M1
44
* @link http://swagger.io
55
* @license apache 2.0
66
*/
@@ -82,10 +82,10 @@ SwaggerAuthorizations.prototype.remove = function(name) {
8282

8383
SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
8484
var status = null;
85-
var key, value, result;
85+
var key, name, value, result;
8686

8787
// if the "authorizations" key is undefined, or has an empty array, add all keys
88-
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length == 0) {
88+
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length === 0) {
8989
for (key in this.authz) {
9090
value = this.authz[key];
9191
result = value.apply(obj, authorizations);
@@ -280,6 +280,10 @@ PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
280280
}
281281
return returnVal;
282282
};
283+
var addModel = function(name, model) {
284+
models[name] = model;
285+
};
286+
283287
var SwaggerClient = function(url, options) {
284288
this.isBuilt = false;
285289
this.url = null;
@@ -291,13 +295,14 @@ var SwaggerClient = function(url, options) {
291295
this.isValid = false;
292296
this.info = null;
293297
this.useJQuery = false;
298+
this.resourceCount = 0;
294299

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

299304
SwaggerClient.prototype.initialize = function (url, options) {
300-
this.models = models;
305+
this.models = models = {};
301306

302307
options = (options||{});
303308

@@ -386,7 +391,6 @@ SwaggerClient.prototype.build = function(mock) {
386391
return obj;
387392
new SwaggerHttp().execute(obj);
388393
}
389-
390394
return this;
391395
};
392396

@@ -502,8 +506,11 @@ SwaggerClient.prototype.buildFromSpec = function(response) {
502506
}
503507
}
504508
this.isBuilt = true;
505-
if (this.success)
509+
if (this.success) {
510+
this.isValid = true;
511+
this.isBuilt = true;
506512
this.success();
513+
}
507514
return this;
508515
};
509516

@@ -592,18 +599,18 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
592599
}
593600
}
594601

602+
var i, model;
603+
595604
if(definitions) {
596605
// add to global models
597606
var key;
598607
for(key in this.definitions) {
599-
var model = new Model(key, definitions[key]);
608+
model = new Model(key, definitions[key]);
600609
if(model) {
601610
models[key] = model;
602611
}
603612
}
604613
}
605-
606-
var i;
607614
for(i = 0; i < this.parameters.length; i++) {
608615
var param = this.parameters[i];
609616
if(param.type === 'array') {
@@ -641,7 +648,7 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
641648
param.responseClassSignature = param.signature;
642649
}
643650

644-
var defaultResponseCode, response, model, responses = this.responses;
651+
var defaultResponseCode, response, responses = this.responses;
645652

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

998-
var headers = {};
999-
for (var attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
1000-
for (var attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
1005+
var headers = {}, attrname;
1006+
for (attrname in allHeaders) { headers[attrname] = allHeaders[attrname]; }
1007+
for (attrname in contentTypeHeaders) { headers[attrname] = contentTypeHeaders[attrname]; }
10011008

10021009
var body = this.getBody(headers, args);
10031010
var url = this.urlify(args);
@@ -1439,7 +1446,7 @@ Property.prototype.toString = function() {
14391446
type = '';
14401447
}
14411448
else {
1442-
this.schema.type;
1449+
type = this.schema.type;
14431450
}
14441451

14451452
if (this.default)
@@ -1500,7 +1507,7 @@ Property.prototype.toString = function() {
15001507

15011508
optionHtml = function(label, value) {
15021509
return '<tr><td class="optionName">' + label + ':</td><td>' + value + '</td></tr>';
1503-
}
1510+
};
15041511

15051512
typeFromJsonSchema = function(type, format) {
15061513
var str;
@@ -1533,7 +1540,7 @@ var cookies = {};
15331540
var models = {};
15341541

15351542
SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
1536-
if (response.apiVersion != null) {
1543+
if (response.apiVersion !== null) {
15371544
this.apiVersion = response.apiVersion;
15381545
}
15391546
this.apis = {};
@@ -1569,6 +1576,7 @@ SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
15691576
this.apisArray.push(res);
15701577
} else {
15711578
var k;
1579+
this.expectedResourceCount = response.apis.length;
15721580
for (k = 0; k < response.apis.length; k++) {
15731581
var resource = response.apis[k];
15741582
res = new SwaggerResource(resource, this);
@@ -1577,15 +1585,21 @@ SwaggerClient.prototype.buildFrom1_2Spec = function (response) {
15771585
}
15781586
}
15791587
this.isValid = true;
1588+
return this;
1589+
};
1590+
1591+
SwaggerClient.prototype.finish = function() {
15801592
if (typeof this.success === 'function') {
1593+
this.isValid = true;
1594+
this.isBuilt = true;
1595+
this.selfReflect();
15811596
this.success();
1582-
}
1583-
return this;
1597+
}
15841598
};
15851599

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

16321646
SwaggerClient.prototype.convertInfo = function (resp) {
16331647
if(typeof resp == 'object') {
1634-
var info = {}
1648+
var info = {};
16351649

16361650
info.title = resp.title;
16371651
info.description = resp.description;
@@ -1660,9 +1674,6 @@ SwaggerClient.prototype.selfReflect = function () {
16601674
}
16611675
this.setConsolidatedModels();
16621676
this.ready = true;
1663-
if (typeof this.success === 'function') {
1664-
return this.success();
1665-
}
16661677
};
16671678

16681679
SwaggerClient.prototype.setConsolidatedModels = function () {
@@ -1702,7 +1713,7 @@ var SwaggerResource = function (resourceObj, api) {
17021713
this.operations = {};
17031714
this.operationsArray = [];
17041715
this.modelsArray = [];
1705-
this.models = {};
1716+
this.models = api.models || {};
17061717
this.rawModels = {};
17071718
this.useJQuery = (typeof api.useJQuery !== 'undefined') ? api.useJQuery : null;
17081719

@@ -1728,9 +1739,11 @@ var SwaggerResource = function (resourceObj, api) {
17281739
on: {
17291740
response: function (resp) {
17301741
var responseObj = resp.obj || JSON.parse(resp.data);
1742+
_this.api.resourceCount += 1;
17311743
return _this.addApiDeclaration(responseObj);
17321744
},
17331745
error: function (response) {
1746+
_this.api.resourceCount += 1;
17341747
return _this.api.fail('Unable to read api \'' +
17351748
_this.name + '\' from path ' + _this.url + ' (server returned ' + response.statusText + ')');
17361749
}
@@ -1784,7 +1797,9 @@ SwaggerResource.prototype.addApiDeclaration = function (response) {
17841797
}
17851798
this.api[this.name] = this;
17861799
this.ready = true;
1787-
return this.api.selfReflect();
1800+
if(this.api.resourceCount === this.api.expectedResourceCount)
1801+
this.api.finish();
1802+
return this;
17881803
};
17891804

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

20782093
this.path = this.path.replace('{format}', 'json');
20792094
this.method = this.method.toLowerCase();
2080-
this.isGetMethod = this.method === 'GET';
2095+
this.isGetMethod = this.method === 'get';
20812096

20822097
var i, j, v;
20832098
this.resourceName = this.resource.name;
@@ -2128,17 +2143,17 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no
21282143
}
21292144
}
21302145
}
2131-
else if (param.allowableValues != null) {
2146+
else if (param.allowableValues) {
21322147
if (param.allowableValues.valueType === 'RANGE')
21332148
param.isRange = true;
21342149
else
21352150
param.isList = true;
2136-
if (param.allowableValues != null) {
2151+
if (param.allowableValues) {
21372152
param.allowableValues.descriptiveValues = [];
21382153
if (param.allowableValues.values) {
21392154
for (j = 0; j < param.allowableValues.values.length; j++) {
21402155
v = param.allowableValues.values[j];
2141-
if (param.defaultValue != null) {
2156+
if (param.defaultValue !== null) {
21422157
param.allowableValues.descriptiveValues.push({
21432158
value: String(v),
21442159
isDefault: (v === param.defaultValue)
@@ -2208,7 +2223,7 @@ SwaggerOperation.prototype.getSampleJSON = function (type, models) {
22082223
var isPrimitive, listType, val;
22092224
listType = this.isListType(type);
22102225
isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true;
2211-
val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[type].createJSONSample());
2226+
val = isPrimitive ? void 0 : (listType ? models[listType].createJSONSample() : models[type].createJSONSample());
22122227
if (val) {
22132228
val = listType ? [val] : val;
22142229
if (typeof val == 'string')
@@ -2243,7 +2258,7 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {
22432258
callback = function (response) {
22442259
var content;
22452260
content = null;
2246-
if (response != null) {
2261+
if (response !== null) {
22472262
content = response.data;
22482263
} else {
22492264
content = 'no data';
@@ -2254,7 +2269,7 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) {
22542269

22552270
params = {};
22562271
params.headers = [];
2257-
if (args.headers != null) {
2272+
if (args.headers) {
22582273
params.headers = args.headers;
22592274
delete args.headers;
22602275
}
@@ -2375,7 +2390,7 @@ SwaggerOperation.prototype.urlify = function (args) {
23752390
}
23762391
}
23772392
}
2378-
if ((queryParams != null) && queryParams.length > 0)
2393+
if ((queryParams) && queryParams.length > 0)
23792394
url += '?' + queryParams;
23802395
return url;
23812396
};
@@ -2584,7 +2599,7 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa
25842599
}
25852600

25862601
var obj;
2587-
if (!((this.headers != null) && (this.headers.mock != null))) {
2602+
if (!((this.headers) && (this.headers.mock))) {
25882603
obj = {
25892604
url: this.url,
25902605
method: this.type,
@@ -2953,7 +2968,9 @@ e.ApiKeyAuthorization = ApiKeyAuthorization;
29532968
e.PasswordAuthorization = PasswordAuthorization;
29542969
e.CookieAuthorization = CookieAuthorization;
29552970
e.SwaggerClient = SwaggerClient;
2971+
e.SwaggerApi = SwaggerClient;
29562972
e.Operation = Operation;
29572973
e.Model = Model;
2958-
e.models = models;
2974+
e.addModel = addModel;
2975+
29592976
})();

lib/swagger-client.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "swagger-client",
33
"author": "Tony Tam <[email protected]>",
44
"description": "swagger.js is a javascript client for use with swaggering APIs.",
5-
"version": "2.1.2-M1",
5+
"version": "2.1.3-M1",
66
"homepage": "http://swagger.io",
77
"repository": {
88
"type": "git",

src/js/auth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ SwaggerAuthorizations.prototype.remove = function(name) {
1717

1818
SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
1919
var status = null;
20-
var key, value, result;
20+
var key, name, value, result;
2121

2222
// if the "authorizations" key is undefined, or has an empty array, add all keys
23-
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length == 0) {
23+
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length === 0) {
2424
for (key in this.authz) {
2525
value = this.authz[key];
2626
result = value.apply(obj, authorizations);

0 commit comments

Comments
 (0)