diff --git a/dist/lib/swagger.js b/dist/lib/swagger.js index 86320991..80db363c 100644 --- a/dist/lib/swagger.js +++ b/dist/lib/swagger.js @@ -63,18 +63,10 @@ } }, response: function(rawResponse) { - if (/^[\],:{}\s]*$/.test(rawResponse.content.data.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { - try { - var response = JSON.parse(rawResponse.content.data); - } catch (e) { - var response = rawResponse.content.data; - } - } else { - var response = rawResponse.content.data; - } + var response; + response = JSON.parse(rawResponse.content.data); _this.swaggerVersion = response.swaggerVersion; - - if (_this.swaggerVersion === "1.2") { + if (_this.swaggerVersion === "1.2") { return _this.buildFromSpec(response); } else { return _this.buildFrom1_1Spec(response); @@ -298,7 +290,7 @@ this.api.fail("SwaggerResources must have a path."); } if (this.path.substring(0, 4) === 'http') { - this.url = this.path.replace('{format}', 'json'); + this.url = this.path.replace('{format}', 'json'); } else { this.url = this.api.basePath + this.path.replace('{format}', 'json'); } @@ -313,16 +305,7 @@ }, response: function(rawResponse) { var response; - if (/^[\],:{}\s]*$/.test(rawResponse.content.data.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { - try { - var response = JSON.parse(rawResponse.content.data); - } catch (e) { - var response = rawResponse.content.data; - } - } else { - var response = rawResponse.content.data; - } - + response = JSON.parse(rawResponse.content.data); return _this.addApiDeclaration(response); } } @@ -338,6 +321,25 @@ } } + SwaggerResource.prototype.getAbsoluteBasePath = function(relativeBasePath) { + var parts, pos, url; + url = this.api.basePath; + pos = url.lastIndexOf(relativeBasePath); + if (pos === -1) { + parts = url.split("/"); + url = parts[0] + "//" + parts[2]; + if (relativeBasePath.indexOf("/") === 0) { + return url + relativeBasePath; + } else { + return url + "/" + relativeBasePath; + } + } else if (relativeBasePath === "/") { + return url.substring(0, pos); + } else { + return url.substring(0, pos) + relativeBasePath; + } + }; + SwaggerResource.prototype.addApiDeclaration = function(response) { var endpoint, _i, _len, _ref; if (response.produces != null) { @@ -347,7 +349,7 @@ this.consumes = response.consumes; } if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) { - this.basePath = response.basePath; + this.basePath = response.basePath.indexOf("http") === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath; } this.addModels(response.models); if (response.apis) { @@ -425,7 +427,7 @@ } } o.nickname = this.sanitize(o.nickname); - op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, type, responseMessages, this, consumes, produces); + op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, type, responseMessages, this, consumes, produces, o.authorizations); this.operations[op.nickname] = op; _results.push(this.operationsArray.push(op)); } @@ -617,7 +619,7 @@ })(); SwaggerOperation = (function() { - function SwaggerOperation(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces) { + function SwaggerOperation(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces, authorizations) { var parameter, v, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _this = this; this.nickname = nickname; @@ -631,6 +633,7 @@ this.resource = resource; this.consumes = consumes; this.produces = produces; + this.authorizations = authorizations; this["do"] = __bind(this["do"], this); if (this.nickname == null) { this.resource.api.fail("SwaggerOperations must have a nickname."); @@ -658,10 +661,10 @@ parameter = _ref1[_i]; parameter.name = parameter.name || parameter.type || parameter.dataType; type = parameter.type || parameter.dataType; - if (typeof(type) != 'undefined' && type.toLowerCase() === 'boolean') { - parameter.allowableValues = {}; - parameter.allowableValues.values = ["true", "false"]; - } + if (type.toLowerCase() === 'boolean') { + parameter.allowableValues = {}; + parameter.allowableValues.values = ["true", "false"]; + } parameter.signature = this.getSignature(type, this.resource.models); parameter.sampleJSON = this.getSampleJSON(type, this.resource.models); if (parameter["enum"] != null) { @@ -673,12 +676,12 @@ v = _ref2[_j]; if ((parameter.defaultValue != null) && parameter.defaultValue === v) { parameter.allowableValues.descriptiveValues.push({ - value: v, + value: String(v), isDefault: true }); } else { parameter.allowableValues.descriptiveValues.push({ - value: v, + value: String(v), isDefault: false }); } @@ -719,11 +722,11 @@ } SwaggerOperation.prototype.isListType = function(type) { - if (typeof(type) != 'undefined' && type.indexOf('[') >= 0) { - return type.substring(type.indexOf('[') + 1, type.indexOf(']')); - } else { - return void 0; - } + if (type.indexOf('[') >= 0) { + return type.substring(type.indexOf('[') + 1, type.indexOf(']')); + } else { + return void 0; + } }; SwaggerOperation.prototype.getSignature = function(type, models) { @@ -932,7 +935,7 @@ SwaggerRequest = (function() { function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) { - var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, urlEncoded, value, values, + var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, status, urlEncoded, value, values, _this = this; this.type = type; this.url = url; @@ -981,18 +984,18 @@ return _results; }).call(this)).length > 0) { type = param.type || param.dataType; - if (((function() { + if ((function() { var _i, _len, _ref, _results; _ref = this.operation.parameters; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { param = _ref[_i]; - if (type.toLowerCase() === "file") { + if ((typeof type !== 'undefined' && type.toLowerCase() === "file").length > 0) { _results.push(param); } } return _results; - }).call(this)).length > 0) { + }).call(this)) { requestContentType = "multipart/form-data"; } else { requestContentType = "application/x-www-form-urlencoded"; @@ -1091,9 +1094,13 @@ } else { e = exports; } - e.authorizations.apply(obj); + status = e.authorizations.apply(obj, this.operation.authorizations); if (opts.mock == null) { - new SwaggerHttp().execute(obj); + if (status !== false) { + new SwaggerHttp().execute(obj); + } else { + obj.canceled = true; + } } else { console.log(obj); return obj; @@ -1176,15 +1183,25 @@ return auth; }; - SwaggerAuthorizations.prototype.apply = function(obj) { - var key, value, _ref, _results; + SwaggerAuthorizations.prototype.remove = function(name) { + return delete this.authz[name]; + }; + + SwaggerAuthorizations.prototype.apply = function(obj, authorizations) { + var key, result, status, value, _ref; + status = null; _ref = this.authz; - _results = []; for (key in _ref) { value = _ref[key]; - _results.push(value.apply(obj)); + result = value.apply(obj, authorizations); + if (result === false) { + status = false; + } + if (result === true) { + status = true; + } } - return _results; + return status; }; return SwaggerAuthorizations; @@ -1204,7 +1221,7 @@ this.type = type; } - ApiKeyAuthorization.prototype.apply = function(obj) { + ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { if (this.type === "query") { if (obj.url.indexOf('?') > 0) { obj.url = obj.url + "&" + this.name + "=" + this.value; @@ -1213,7 +1230,8 @@ } return true; } else if (this.type === "header") { - return obj.headers[this.name] = this.value; + obj.headers[this.name] = this.value; + return true; } }; @@ -1222,6 +1240,8 @@ })(); PasswordAuthorization = (function() { + PasswordAuthorization._btoa = null; + PasswordAuthorization.prototype.name = null; PasswordAuthorization.prototype.username = null; @@ -1232,10 +1252,20 @@ this.name = name; this.username = username; this.password = password; + PasswordAuthorization._ensureBtoa(); } - PasswordAuthorization.prototype.apply = function(obj) { - return obj.headers["Authorization"] = "Basic " + btoa(this.username + ":" + this.password); + PasswordAuthorization.prototype.apply = function(obj, authorizations) { + obj.headers["Authorization"] = "Basic " + PasswordAuthorization._btoa(this.username + ":" + this.password); + return true; + }; + + PasswordAuthorization._ensureBtoa = function() { + if (typeof window !== 'undefined') { + return this._btoa = btoa; + } else { + return this._btoa = require("btoa"); + } }; return PasswordAuthorization; diff --git a/dist/swagger-ui.js b/dist/swagger-ui.js index de0a126f..656b6ec2 100644 --- a/dist/swagger-ui.js +++ b/dist/swagger-ui.js @@ -1509,16 +1509,14 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {}; contentTypeModel.produces = this.model.produces; _ref5 = this.model.parameters; for (_i = 0, _len = _ref5.length; _i < _len; _i++) { - param = _ref5[_i]; - type = param.type || param.dataType; - - if (typeof(type) != 'undefined' && type.toLowerCase() === 'file') { - if (!contentTypeModel.consumes) { - console.log("set content type "); - contentTypeModel.consumes = 'multipart/form-data'; - } - } - + param = _ref5[_i]; + type = param.type || param.dataType; + if (type.toLowerCase() === 'file') { + if (!contentTypeModel.consumes) { + console.log("set content type "); + contentTypeModel.consumes = 'multipart/form-data'; + } + } } responseContentTypeView = new ResponseContentTypeView({ model: contentTypeModel @@ -1906,8 +1904,8 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {}; if (this.model.paramType === 'body') { this.model.isBody = true; } - if (typeof(type) != 'undefined' && type.toLowerCase() === 'file') { - this.model.isFile = true; + if (type.toLowerCase() === 'file') { + this.model.isFile = true; } template = this.template(); $(this.el).html(template(this.model)); diff --git a/lib/swagger.js b/lib/swagger.js index 86320991..bbdca4f6 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -63,18 +63,10 @@ } }, response: function(rawResponse) { - if (/^[\],:{}\s]*$/.test(rawResponse.content.data.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { - try { - var response = JSON.parse(rawResponse.content.data); - } catch (e) { - var response = rawResponse.content.data; - } - } else { - var response = rawResponse.content.data; - } + var response; + response = JSON.parse(rawResponse.content.data); _this.swaggerVersion = response.swaggerVersion; - - if (_this.swaggerVersion === "1.2") { + if (_this.swaggerVersion === "1.2") { return _this.buildFromSpec(response); } else { return _this.buildFrom1_1Spec(response); @@ -298,7 +290,7 @@ this.api.fail("SwaggerResources must have a path."); } if (this.path.substring(0, 4) === 'http') { - this.url = this.path.replace('{format}', 'json'); + this.url = this.path.replace('{format}', 'json'); } else { this.url = this.api.basePath + this.path.replace('{format}', 'json'); } @@ -313,16 +305,7 @@ }, response: function(rawResponse) { var response; - if (/^[\],:{}\s]*$/.test(rawResponse.content.data.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { - try { - var response = JSON.parse(rawResponse.content.data); - } catch (e) { - var response = rawResponse.content.data; - } - } else { - var response = rawResponse.content.data; - } - + response = JSON.parse(rawResponse.content.data); return _this.addApiDeclaration(response); } } @@ -338,6 +321,25 @@ } } + SwaggerResource.prototype.getAbsoluteBasePath = function(relativeBasePath) { + var parts, pos, url; + url = this.api.basePath; + pos = url.lastIndexOf(relativeBasePath); + if (pos === -1) { + parts = url.split("/"); + url = parts[0] + "//" + parts[2]; + if (relativeBasePath.indexOf("/") === 0) { + return url + relativeBasePath; + } else { + return url + "/" + relativeBasePath; + } + } else if (relativeBasePath === "/") { + return url.substring(0, pos); + } else { + return url.substring(0, pos) + relativeBasePath; + } + }; + SwaggerResource.prototype.addApiDeclaration = function(response) { var endpoint, _i, _len, _ref; if (response.produces != null) { @@ -347,7 +349,7 @@ this.consumes = response.consumes; } if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) { - this.basePath = response.basePath; + this.basePath = response.basePath.indexOf("http") === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath; } this.addModels(response.models); if (response.apis) { @@ -425,7 +427,7 @@ } } o.nickname = this.sanitize(o.nickname); - op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, type, responseMessages, this, consumes, produces); + op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, type, responseMessages, this, consumes, produces, o.authorizations); this.operations[op.nickname] = op; _results.push(this.operationsArray.push(op)); } @@ -617,7 +619,7 @@ })(); SwaggerOperation = (function() { - function SwaggerOperation(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces) { + function SwaggerOperation(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces, authorizations) { var parameter, v, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _this = this; this.nickname = nickname; @@ -631,6 +633,7 @@ this.resource = resource; this.consumes = consumes; this.produces = produces; + this.authorizations = authorizations; this["do"] = __bind(this["do"], this); if (this.nickname == null) { this.resource.api.fail("SwaggerOperations must have a nickname."); @@ -658,10 +661,10 @@ parameter = _ref1[_i]; parameter.name = parameter.name || parameter.type || parameter.dataType; type = parameter.type || parameter.dataType; - if (typeof(type) != 'undefined' && type.toLowerCase() === 'boolean') { - parameter.allowableValues = {}; - parameter.allowableValues.values = ["true", "false"]; - } + if (typeof type !== 'undefined' && type.toLowerCase() === 'boolean') { + parameter.allowableValues = {}; + parameter.allowableValues.values = ["true", "false"]; + } parameter.signature = this.getSignature(type, this.resource.models); parameter.sampleJSON = this.getSampleJSON(type, this.resource.models); if (parameter["enum"] != null) { @@ -673,12 +676,12 @@ v = _ref2[_j]; if ((parameter.defaultValue != null) && parameter.defaultValue === v) { parameter.allowableValues.descriptiveValues.push({ - value: v, + value: String(v), isDefault: true }); } else { parameter.allowableValues.descriptiveValues.push({ - value: v, + value: String(v), isDefault: false }); } @@ -719,11 +722,11 @@ } SwaggerOperation.prototype.isListType = function(type) { - if (typeof(type) != 'undefined' && type.indexOf('[') >= 0) { - return type.substring(type.indexOf('[') + 1, type.indexOf(']')); - } else { - return void 0; - } + if (type.indexOf('[') >= 0) { + return type.substring(type.indexOf('[') + 1, type.indexOf(']')); + } else { + return void 0; + } }; SwaggerOperation.prototype.getSignature = function(type, models) { @@ -932,7 +935,7 @@ SwaggerRequest = (function() { function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) { - var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, urlEncoded, value, values, + var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, status, urlEncoded, value, values, _this = this; this.type = type; this.url = url; @@ -987,7 +990,7 @@ _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { param = _ref[_i]; - if (type.toLowerCase() === "file") { + if (typeof type !== 'undefined' && type.toLowerCase() === "file") { _results.push(param); } } @@ -1091,9 +1094,13 @@ } else { e = exports; } - e.authorizations.apply(obj); + status = e.authorizations.apply(obj, this.operation.authorizations); if (opts.mock == null) { - new SwaggerHttp().execute(obj); + if (status !== false) { + new SwaggerHttp().execute(obj); + } else { + obj.canceled = true; + } } else { console.log(obj); return obj; @@ -1176,15 +1183,25 @@ return auth; }; - SwaggerAuthorizations.prototype.apply = function(obj) { - var key, value, _ref, _results; + SwaggerAuthorizations.prototype.remove = function(name) { + return delete this.authz[name]; + }; + + SwaggerAuthorizations.prototype.apply = function(obj, authorizations) { + var key, result, status, value, _ref; + status = null; _ref = this.authz; - _results = []; for (key in _ref) { value = _ref[key]; - _results.push(value.apply(obj)); + result = value.apply(obj, authorizations); + if (result === false) { + status = false; + } + if (result === true) { + status = true; + } } - return _results; + return status; }; return SwaggerAuthorizations; @@ -1204,7 +1221,7 @@ this.type = type; } - ApiKeyAuthorization.prototype.apply = function(obj) { + ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { if (this.type === "query") { if (obj.url.indexOf('?') > 0) { obj.url = obj.url + "&" + this.name + "=" + this.value; @@ -1213,7 +1230,8 @@ } return true; } else if (this.type === "header") { - return obj.headers[this.name] = this.value; + obj.headers[this.name] = this.value; + return true; } }; @@ -1222,6 +1240,8 @@ })(); PasswordAuthorization = (function() { + PasswordAuthorization._btoa = null; + PasswordAuthorization.prototype.name = null; PasswordAuthorization.prototype.username = null; @@ -1232,10 +1252,20 @@ this.name = name; this.username = username; this.password = password; + PasswordAuthorization._ensureBtoa(); } - PasswordAuthorization.prototype.apply = function(obj) { - return obj.headers["Authorization"] = "Basic " + btoa(this.username + ":" + this.password); + PasswordAuthorization.prototype.apply = function(obj, authorizations) { + obj.headers["Authorization"] = "Basic " + PasswordAuthorization._btoa(this.username + ":" + this.password); + return true; + }; + + PasswordAuthorization._ensureBtoa = function() { + if (typeof window !== 'undefined') { + return this._btoa = btoa; + } else { + return this._btoa = require("btoa"); + } }; return PasswordAuthorization;