Skip to content

Commit 70cce65

Browse files
frankkeeferfkeefercdimascio
authored
feat(path-to-regexp): path-to-regexp 8.1.0 update (#976)
* feat(path-to-regexp): path-to-regexp update to 8.1.0 * feat(path-to-regexp): cleanup notes for PR * feat(path-to-regexp): potential version bump if approved * feat(path-to-regexp): pr change request + added notes for changes --------- Co-authored-by: fkeefer <[email protected]> Co-authored-by: Carmine DiMascio <[email protected]>
1 parent 8e422b2 commit 70cce65

File tree

5 files changed

+59
-18
lines changed

5 files changed

+59
-18
lines changed

Diff for: package-lock.json

+38-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-openapi-validator",
3-
"version": "5.3.6",
3+
"version": "5.3.7",
44
"description": "Automatically validate API requests and responses with OpenAPI 3 and Express.",
55
"main": "dist/index.js",
66
"scripts": {
@@ -45,7 +45,7 @@
4545
"media-typer": "^1.1.0",
4646
"multer": "^1.4.5-lts.1",
4747
"ono": "^7.1.3",
48-
"path-to-regexp": "^6.3.0"
48+
"path-to-regexp": "^8.1.0"
4949
},
5050
"devDependencies": {
5151
"@types/cookie-parser": "^1.4.2",

Diff for: src/framework/base.path.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class BasePath {
2626
}
2727
if (/{\w+}/.test(urlPath)) {
2828
// has variable that we need to check out
29-
urlPath = urlPath.replace(/{(\w+)}/g, (substring, p1) => `:${p1}(.*)`);
29+
urlPath = urlPath.replace(/{(\w+)}/g, (substring, p1) => `:"${p1}"`);
3030
}
3131
this.expressPath = urlPath;
3232
for (const variable in server.variables) {
@@ -69,7 +69,9 @@ export class BasePath {
6969
}, []);
7070

7171
const allParamCombos = cartesian(...allParams);
72-
const toPath = compile(this.expressPath);
72+
// path-to-regexp v 8.x.x requires we escape the open and close parentheses `(`,`)` added a replace function to catch that use case.
73+
const filteredExpressPath = this.expressPath.replace(/[(]/g, '\\\\(').replace(/[)]/g, '\\\\)');
74+
const toPath = compile(filteredExpressPath);
7375
const paths = new Set<string>();
7476
for (const combo of allParamCombos) {
7577
paths.add(toPath(combo));

Diff for: src/framework/openapi.spec.loader.ts

+12
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,22 @@ export class OpenApiSpecLoader {
113113
// {/path} => /path(*) <--- RFC 6570 format (not supported by openapi)
114114
// const pass1 = part.replace(/\{(\/)([^\*]+)(\*)}/g, '$1:$2$3');
115115

116+
//if wildcard path use new path-to-regex expected model
117+
if(/[*]/g.test(part)){
118+
// /v1/{path}* => /v1/*path)
119+
// /v1/{path}(*) => /v1/*path)
120+
const pass1 = part.replace(/\/{([^}]+)}\({0,1}(\*)\){0,1}/g, '/$2$1');
121+
122+
// substitute params with express equivalent
123+
// /path/{multi}/test/{/*path}=> /path/:multi/test/{/*path}
124+
return pass1.replace(/\{([^\/}]+)}/g, ':$1');
125+
//return pass1;
126+
}
116127
// instead create our own syntax that is compatible with express' pathToRegex
117128
// /{path}* => /:path*)
118129
// /{path}(*) => /:path*)
119130
const pass1 = part.replace(/\/{([^}]+)}\({0,1}(\*)\){0,1}/g, '/:$1$2');
131+
120132
// substitute params with express equivalent
121133
// /path/{id} => /path/:id
122134
return pass1.replace(/\{([^}]+)}/g, ':$1');

Diff for: src/middlewares/openapi.metadata.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,16 @@ export function applyOpenApiMetadata(
8080
const pathKey = openApiRoute.substring((<any>methods).basePath.length);
8181
const schema = openApiContext.apiDoc.paths[pathKey][method.toLowerCase()];
8282
const _schema = responseApiDoc?.paths[pathKey][method.toLowerCase()];
83-
84-
const keys = [];
8583
const strict = !!req.app.enabled('strict routing');
8684
const sensitive = !!req.app.enabled('case sensitive routing');
8785
const pathOpts = {
8886
sensitive,
8987
strict,
9088
};
91-
const regexp = pathToRegexp(expressRoute, keys, pathOpts);
92-
const matchedRoute = regexp.exec(path);
93-
89+
const regexpObj = pathToRegexp(expressRoute, pathOpts);
90+
const matchedRoute = regexpObj.regexp.exec(path);
9491
if (matchedRoute) {
95-
const paramKeys = keys.map((k) => k.name);
92+
const paramKeys = regexpObj.keys.map((k) => k.name);
9693
try {
9794
const paramsVals = matchedRoute.slice(1).map(decodeURIComponent);
9895
const pathParams = zipObject(paramKeys, paramsVals);

0 commit comments

Comments
 (0)