added response check for swagger 2.0/1.2 client

This commit is contained in:
Tony Tam
2014-09-16 17:40:33 -07:00
parent 394afba3e5
commit 91822035ef
7 changed files with 39 additions and 22 deletions

View File

@@ -184,6 +184,7 @@ var SwaggerClient = function(url, options) {
this.basePath = null;
this.authorizations = null;
this.authorizationScheme = null;
this.isValid = false;
this.info = null;
this.useJQuery = false;
@@ -236,6 +237,11 @@ SwaggerClient.prototype.build = function() {
if(responseObj.swagger && responseObj.swagger === 2.0) {
self.swaggerVersion = responseObj.swagger;
self.buildFromSpec(responseObj);
self.isValid = true;
}
else {
self.isValid = false;
self.failure()
}
}
}

3
dist/lib/swagger.js vendored
View File

@@ -99,6 +99,7 @@ var SwaggerApi = function(url, options) {
this.info = null;
this.useJQuery = false;
this.modelsArray = [];
this.isValid;
options = (options||{});
if (url)
@@ -213,6 +214,7 @@ SwaggerApi.prototype.buildFromSpec = function(response) {
this.apisArray.push(res);
}
}
this.isValid = true;
if (this.success) {
this.success();
}
@@ -260,6 +262,7 @@ SwaggerApi.prototype.buildFrom1_1Spec = function(response) {
this.apisArray.push(res);
}
}
this.isValid = true;
if (this.success) {
this.success();
}

15
dist/swagger-ui.js vendored
View File

@@ -1258,7 +1258,13 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
return _this.showMessage(d);
};
this.options.failure = function(d) {
if (_this.api && _this.api.isValid === false) {
log("not a valid 2.0 spec, loading legacy client");
_this.api = new SwaggerApi(_this.options);
return _this.api.build();
} else {
return _this.onLoadFailure(d);
}
};
this.headerView = new HeaderView({
el: $('#header')
@@ -1284,15 +1290,8 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
}
this.options.url = url;
this.headerView.update(url);
if (url.indexOf('swagger.json') > 0) {
this.api = new SwaggerClient(this.options);
this.api.build();
return this.api;
} else {
this.api = new SwaggerApi(this.options);
this.api.build();
return this.api;
}
return this.api.build();
};
SwaggerUi.prototype.render = function() {

File diff suppressed because one or more lines are too long

View File

@@ -184,6 +184,7 @@ var SwaggerClient = function(url, options) {
this.basePath = null;
this.authorizations = null;
this.authorizationScheme = null;
this.isValid = false;
this.info = null;
this.useJQuery = false;
@@ -236,6 +237,11 @@ SwaggerClient.prototype.build = function() {
if(responseObj.swagger && responseObj.swagger === 2.0) {
self.swaggerVersion = responseObj.swagger;
self.buildFromSpec(responseObj);
self.isValid = true;
}
else {
self.isValid = false;
self.failure()
}
}
}

View File

@@ -99,6 +99,7 @@ var SwaggerApi = function(url, options) {
this.info = null;
this.useJQuery = false;
this.modelsArray = [];
this.isValid;
options = (options||{});
if (url)
@@ -213,6 +214,7 @@ SwaggerApi.prototype.buildFromSpec = function(response) {
this.apisArray.push(res);
}
}
this.isValid = true;
if (this.success) {
this.success();
}
@@ -260,6 +262,7 @@ SwaggerApi.prototype.buildFrom1_1Spec = function(response) {
this.apisArray.push(res);
}
}
this.isValid = true;
if (this.success) {
this.success();
}

View File

@@ -22,9 +22,16 @@ class SwaggerUi extends Backbone.Router
@options = options
# Set the callbacks
@options.success = => @render()
@options.success = =>
@render()
@options.progress = (d) => @showMessage(d)
@options.failure = (d) => @onLoadFailure(d)
@options.failure = (d) =>
if @api and @api.isValid is false
log "not a valid 2.0 spec, loading legacy client"
@api = new SwaggerApi(@options)
@api.build()
else
@onLoadFailure(d)
# Create view to handle the header inputs
@headerView = new HeaderView({el: $('#header')})
@@ -48,15 +55,8 @@ class SwaggerUi extends Backbone.Router
@options.url = url
@headerView.update(url)
if url.indexOf('swagger.json') > 0
@api = new SwaggerClient(@options)
@api.build()
@api
else
@api = new SwaggerApi(@options)
@api.build()
@api
# This is bound to success handler for SwaggerApi
# so it gets called when SwaggerApi completes loading