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) { (Array.isArray(type) && type.some(function (t) {
return types.indexOf(t) === -1; 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 // 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,7 +575,15 @@ module.exports.validateContentType = function (contentType, supportedTypes, resu
* @returns {string} schemaPath - The constructed schema path. * @returns {string} schemaPath - The constructed schema path.
*/ */
module.exports.constructSchemaPathFromPtr = function (ptr, schema) { 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) { if (schema) {
schemaPath += "['schema']"; schemaPath += "['schema']";
@ -613,3 +621,12 @@ var walk = module.exports.walk = function (obj, fn) {
doWalk([], obj, []); 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) { if (!skipValidation) {
// Validate against JSON Schema // Validate against JSON Schema
schemaObj = parameterObject.pathObject.api.definition; schemaObj = parameterObject.pathObject.api.definition;
schemaObj = escapePaths(schemaObj)
schemaPath = helpers.constructSchemaPathFromPtr(parameterObject.ptr, parameterObject.definition.schema); schemaPath = helpers.constructSchemaPathFromPtr(parameterObject.ptr, parameterObject.definition.schema);
result = helpers.validateAgainstSchema(helpers.getJSONSchemaValidator(), schemaObj, value, schemaPath); 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; module.exports = ParameterValue;

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

@ -79,10 +79,10 @@ function Parameter (opOrPathObject, definition, definitionFullyResolved, pathToD
} }
this.pathObject.api._debug(' %s%s (in: %s) at %s', this.pathObject.api._debug(' %s%s (in: %s) at %s',
_.isUndefined(this.operationObject) ? '' : ' ', _.isUndefined(this.operationObject) ? '' : ' ',
definitionFullyResolved.name, definitionFullyResolved.name,
definitionFullyResolved.in, definitionFullyResolved.in,
this.ptr); this.ptr);
} }
/** /**
@ -132,80 +132,82 @@ Parameter.prototype.getValue = function (req) {
var value, undef, urlObj; var value, undef, urlObj;
switch (this.in) { switch (this.in) {
case 'body': case 'body':
value = req.body; value = req.body;
break; break;
case 'formData': 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 // 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'. // value since the JSON Schema for formData parameters does not allow a type of 'object'.
if (type === 'file') { if (type === 'file') {
if (_.isUndefined(req.files)) { 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) { 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 { } else {
break; break;
} }
} }
value = req.files[this.name]; value = helpers.getHeaderValue(req.headers, this.name);
} else { break;
if (_.isUndefined(req.body)) { 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) { 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 { } else {
break; break;
} }
} }
value = req.body[this.name];
} value = _.get(req.query, this.name);
break; if (typeof value === 'string') {
case 'header': value = decodeURIComponent(value);
if (_.isUndefined(req.headers)) {
if (this.required) {
throw new Error('req.headers must be provided for \'header\' parameters');
} else {
break;
} }
}
value = helpers.getHeaderValue(req.headers, this.name); break;
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;
// no default // no default
} }