Add support for processing 'x-ms-parameterized-host' extension.

This commit is contained in:
Amar Zavery 2017-11-20 13:52:25 -08:00
Родитель b54dceada9
Коммит 5d63ce93d1
2 изменённых файлов: 24 добавлений и 3 удалений

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

@ -173,7 +173,14 @@ Parameter.prototype.getValue = function (req) {
throw new Error('req.url must be provided for \'path\' parameters');
}
pathMatch = this.pathObject.regexp.exec(parseUrl(req.url).pathname);
var urlObj = parseUrl(req.url);
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

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

@ -70,6 +70,14 @@ function Path (api, path, definition, definitionFullyResolved, pathToDefinition)
basePathPrefix = basePathPrefix.substring(0, basePathPrefix.length - 1);
}
// Process 'x-ms-parameterized-host' extension if present.
var xmsParameterizedHost = api.definitionFullyResolved['x-ms-parameterized-host'];
var hostTemplate = '';
if (xmsParameterizedHost && xmsParameterizedHost.hostTemplate) {
hostTemplate = xmsParameterizedHost.hostTemplate;
}
this.hostTemplate = hostTemplate;
// Assign local properties
this.api = api;
this.definition = definition;
@ -77,7 +85,10 @@ function Path (api, path, definition, definitionFullyResolved, pathToDefinition)
this.path = path;
this.pathToDefinition = pathToDefinition;
this.ptr = JsonRefs.pathToPtr(pathToDefinition);
this.regexp = pathToRegexp(basePathPrefix + path.replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\{/g, ':').replace(/\}/g, ''), {sensitive: true});
var processedPath = hostTemplate.replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\{/g, ':').replace(/\}/g, '')
+ basePathPrefix
+ path.replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\{/g, ':').replace(/\}/g, '')
this.regexp = pathToRegexp(processedPath, {sensitive: true});
// Whenever the path property is set, the regexp should also be updated accordingly.
Object.defineProperty(this, 'path', {
@ -86,7 +97,10 @@ function Path (api, path, definition, definitionFullyResolved, pathToDefinition)
},
set: function (value) {
path = value;
this.regexp = pathToRegexp(basePathPrefix + path.replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\{/g, ':').replace(/\}/g, ''), {sensitive: true});
processedPath = hostTemplate.replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\{/g, ':').replace(/\}/g, '')
+ basePathPrefix
+ path.replace(/\(/g, '\\(').replace(/\)/g, '\\)').replace(/\{/g, ':').replace(/\}/g, '');
this.regexp = pathToRegexp(processedPath, {sensitive: true});
}
});