From 542d4fe478694093aa8de6434e496c482087a278 Mon Sep 17 00:00:00 2001 From: invincible Date: Fri, 27 Dec 2013 13:51:50 -0500 Subject: [PATCH] safe-JSON-parsing-check-for-type-undefined --- dist/lib/swagger.js | 45 +++++++++++++++++++++++++++++++-------------- dist/swagger-ui.js | 22 ++++++++++++---------- lib/swagger.js | 45 +++++++++++++++++++++++++++++++-------------- 3 files changed, 74 insertions(+), 38 deletions(-) diff --git a/dist/lib/swagger.js b/dist/lib/swagger.js index e45e62f8..86320991 100644 --- a/dist/lib/swagger.js +++ b/dist/lib/swagger.js @@ -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) { diff --git a/dist/swagger-ui.js b/dist/swagger-ui.js index 656b6ec2..de0a126f 100644 --- a/dist/swagger-ui.js +++ b/dist/swagger-ui.js @@ -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)); diff --git a/lib/swagger.js b/lib/swagger.js index e45e62f8..86320991 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -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) {