This commit is contained in:
mcardosos 2018-04-23 17:17:39 -07:00
Родитель 02ede38a83
Коммит 5b558e5b86
3 изменённых файлов: 96 добавлений и 68 удалений

Просмотреть файл

@ -213,7 +213,7 @@ var convertValue = module.exports.convertValue = function (schema, options, valu
(Array.isArray(type) && type.some(function (t) {
return types.indexOf(t) === -1;
}))))) {
throw new TypeError('Invalid \'type\' value: ' + type);
throw new TypeError('Invalid \'type\' value: ' + type);
}
// Since JSON Schema allows you to not specify a type and it is treated as a wildcard of sorts, we should not do any
@ -575,8 +575,16 @@ module.exports.validateContentType = function (contentType, supportedTypes, resu
* @returns {string} schemaPath - The constructed schema path.
*/
module.exports.constructSchemaPathFromPtr = function (ptr, schema) {
var schemaPath = "['" + JsonRefs.pathFromPtr(ptr).join("']['") + "']";
var refs = JsonRefs.pathFromPtr(ptr)
var paths = []
refs.forEach(function (element) {
element = escapeSingleQuotes(element)
paths.push(element)
});
var schemaPath = "['" + paths.join("']['") + "']";
if (schema) {
schemaPath += "['schema']";
}
@ -613,3 +621,12 @@ var walk = module.exports.walk = function (obj, fn) {
doWalk([], obj, []);
}
/**
* Escape single quotes in a string
*
* @param {string} [value] - The value to escape
*/
var escapeSingleQuotes = module.exports.escapeSingleQuotes = function (value) {
return value.split("'").join("%27")
}

Просмотреть файл

@ -126,6 +126,7 @@ function ParameterValue (parameterObject, raw) {
if (!skipValidation) {
// Validate against JSON Schema
schemaObj = parameterObject.pathObject.api.definition;
schemaObj = escapePaths(schemaObj)
schemaPath = helpers.constructSchemaPathFromPtr(parameterObject.ptr, parameterObject.definition.schema);
result = helpers.validateAgainstSchema(helpers.getJSONSchemaValidator(), schemaObj, value, schemaPath);
}
@ -224,4 +225,12 @@ function ParameterValue (parameterObject, raw) {
});
}
function escapePaths(schema) {
_.each(schema['paths'], function (value, key) {
var newKey = helpers.escapeSingleQuotes(key);
schema['paths'][newKey] = value;
})
return schema
}
module.exports = ParameterValue;

Просмотреть файл

@ -79,10 +79,10 @@ function Parameter (opOrPathObject, definition, definitionFullyResolved, pathToD
}
this.pathObject.api._debug(' %s%s (in: %s) at %s',
_.isUndefined(this.operationObject) ? '' : ' ',
definitionFullyResolved.name,
definitionFullyResolved.in,
this.ptr);
_.isUndefined(this.operationObject) ? '' : ' ',
definitionFullyResolved.name,
definitionFullyResolved.in,
this.ptr);
}
/**
@ -132,80 +132,82 @@ Parameter.prototype.getValue = function (req) {
var value, undef, urlObj;
switch (this.in) {
case 'body':
value = req.body;
break;
case 'formData':
// For formData, either the value is a file or a property of req.body. req.body as a whole can never be the
// value since the JSON Schema for formData parameters does not allow a type of 'object'.
if (type === 'file') {
if (_.isUndefined(req.files)) {
case 'body':
value = req.body;
break;
case 'formData':
// For formData, either the value is a file or a property of req.body. req.body as a whole can never be the
// value since the JSON Schema for formData parameters does not allow a type of 'object'.
if (type === 'file') {
if (_.isUndefined(req.files)) {
if (this.required) {
throw new Error('req.files must be provided for \'formData\' parameters of type \'file\'');
} else {
break;
}
}
value = req.files[this.name];
} else {
if (_.isUndefined(req.body)) {
if (this.required) {
throw new Error('req.body must be provided for \'formData\' parameters');
} else {
break;
}
}
value = req.body[this.name];
}
break;
case 'header':
if (_.isUndefined(req.headers)) {
if (this.required) {
throw new Error('req.files must be provided for \'formData\' parameters of type \'file\'');
throw new Error('req.headers must be provided for \'header\' parameters');
} else {
break;
}
}
value = req.files[this.name];
} else {
if (_.isUndefined(req.body)) {
value = helpers.getHeaderValue(req.headers, this.name);
break;
case 'path':
urlObj = parseUrl(req.originalUrl || req.url);
if (_.isUndefined(req.originalUrl) && _.isUndefined(req.url)) {
throw new Error('req.originalUrl or req.url must be provided for \'path\' parameters');
}
urlObj.pathname = decodeURIComponent(urlObj.pathname)
urlObj.path = decodeURIComponent(urlObj.path)
if (this.pathObject.hostTemplate) {
// hostname needs to be a part of the data for doing the regex match while processing
// x-ms-parameterized-host extension.
pathMatch = this.pathObject.regexp.exec(urlObj.hostname + urlObj.pathname);
} else {
pathMatch = this.pathObject.regexp.exec(urlObj.pathname);
}
if (pathMatch) {
// decode URI component here to avoid issues with encoded slashes
value = decodeURIComponent(pathMatch[_.findIndex(this.pathObject.regexp.keys, function (key) {
return key.name === that.name;
}) + 1]);
}
break;
case 'query':
undef = _.isUndefined(req.query);
if ((!undef && _.isUndefined(req.query[this.name])) || undef) {
if (this.required) {
throw new Error('req.body must be provided for \'formData\' parameters');
throw new Error('req.query must be provided for \'query\' parameters');
} else {
break;
}
}
value = req.body[this.name];
}
break;
case 'header':
if (_.isUndefined(req.headers)) {
if (this.required) {
throw new Error('req.headers must be provided for \'header\' parameters');
} else {
break;
value = _.get(req.query, this.name);
if (typeof value === 'string') {
value = decodeURIComponent(value);
}
}
value = helpers.getHeaderValue(req.headers, this.name);
break;
case 'path':
urlObj = parseUrl(req.originalUrl || req.url);
if (_.isUndefined(req.originalUrl) && _.isUndefined(req.url)) {
throw new Error('req.originalUrl or req.url must be provided for \'path\' parameters');
}
if (this.pathObject.hostTemplate) {
// hostname needs to be a part of the data for doing the regex match while processing
// x-ms-parameterized-host extension.
pathMatch = this.pathObject.regexp.exec(urlObj.hostname + urlObj.pathname);
} else {
pathMatch = this.pathObject.regexp.exec(urlObj.pathname);
}
if (pathMatch) {
// decode URI component here to avoid issues with encoded slashes
value = decodeURIComponent(pathMatch[_.findIndex(this.pathObject.regexp.keys, function (key) {
return key.name === that.name;
}) + 1]);
}
break;
case 'query':
undef = _.isUndefined(req.query);
if ((!undef && _.isUndefined(req.query[this.name])) || undef) {
if (this.required) {
throw new Error('req.query must be provided for \'query\' parameters');
} else {
break;
}
}
value = _.get(req.query, this.name);
if (typeof value === 'string') {
value = decodeURIComponent(value);
}
break;
break;
// no default
}