Commit 105c516f authored by Tony Tam's avatar Tony Tam
Browse files

Merge pull request #365 from xinvincible21/master

safe-JSON-parsing-check-for-type-undefined
parents 97e96ba3 542d4fe4
......@@ -63,10 +63,18 @@
}
},
response: function(rawResponse) {
var response;
response = JSON.parse(rawResponse.content.data);
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;
}
_this.swaggerVersion = response.swaggerVersion;
if (_this.swaggerVersion === "1.2") {
if (_this.swaggerVersion === "1.2") {
return _this.buildFromSpec(response);
} else {
return _this.buildFrom1_1Spec(response);
......@@ -290,7 +298,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');
}
......@@ -305,7 +313,16 @@
},
response: function(rawResponse) {
var response;
response = JSON.parse(rawResponse.content.data);
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;
}
return _this.addApiDeclaration(response);
}
}
......@@ -641,10 +658,10 @@
parameter = _ref1[_i];
parameter.name = parameter.name || parameter.type || parameter.dataType;
type = parameter.type || parameter.dataType;
if (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) {
......@@ -702,11 +719,11 @@
}
SwaggerOperation.prototype.isListType = function(type) {
if (type.indexOf('[') >= 0) {
return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
} else {
return void 0;
}
if (typeof(type) != 'undefined' && type.indexOf('[') >= 0) {
return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
} else {
return void 0;
}
};
SwaggerOperation.prototype.getSignature = function(type, models) {
......
......@@ -1509,14 +1509,16 @@ 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 (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 (typeof(type) != 'undefined' && type.toLowerCase() === 'file') {
if (!contentTypeModel.consumes) {
console.log("set content type ");
contentTypeModel.consumes = 'multipart/form-data';
}
}
}
responseContentTypeView = new ResponseContentTypeView({
model: contentTypeModel
......@@ -1904,8 +1906,8 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
if (this.model.paramType === 'body') {
this.model.isBody = true;
}
if (type.toLowerCase() === 'file') {
this.model.isFile = true;
if (typeof(type) != 'undefined' && type.toLowerCase() === 'file') {
this.model.isFile = true;
}
template = this.template();
$(this.el).html(template(this.model));
......
......@@ -63,10 +63,18 @@
}
},
response: function(rawResponse) {
var response;
response = JSON.parse(rawResponse.content.data);
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;
}
_this.swaggerVersion = response.swaggerVersion;
if (_this.swaggerVersion === "1.2") {
if (_this.swaggerVersion === "1.2") {
return _this.buildFromSpec(response);
} else {
return _this.buildFrom1_1Spec(response);
......@@ -290,7 +298,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');
}
......@@ -305,7 +313,16 @@
},
response: function(rawResponse) {
var response;
response = JSON.parse(rawResponse.content.data);
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;
}
return _this.addApiDeclaration(response);
}
}
......@@ -641,10 +658,10 @@
parameter = _ref1[_i];
parameter.name = parameter.name || parameter.type || parameter.dataType;
type = parameter.type || parameter.dataType;
if (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) {
......@@ -702,11 +719,11 @@
}
SwaggerOperation.prototype.isListType = function(type) {
if (type.indexOf('[') >= 0) {
return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
} else {
return void 0;
}
if (typeof(type) != 'undefined' && type.indexOf('[') >= 0) {
return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
} else {
return void 0;
}
};
SwaggerOperation.prototype.getSignature = function(type, models) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment