safe-JSON-parsing-check-for-type-undefined

This commit is contained in:
invincible
2013-12-27 13:51:50 -05:00
parent 97e96ba3d9
commit 542d4fe478
3 changed files with 74 additions and 38 deletions

View File

@@ -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) {