Skip to content

Commit

Permalink
feat(path-to-regexp): path-to-regexp 8.1.0 update (#976)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Sep 14, 2024
1 parent 8e422b2 commit 70cce65
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
46 changes: 38 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-openapi-validator",
"version": "5.3.6",
"version": "5.3.7",
"description": "Automatically validate API requests and responses with OpenAPI 3 and Express.",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -45,7 +45,7 @@
"media-typer": "^1.1.0",
"multer": "^1.4.5-lts.1",
"ono": "^7.1.3",
"path-to-regexp": "^6.3.0"
"path-to-regexp": "^8.1.0"
},
"devDependencies": {
"@types/cookie-parser": "^1.4.2",
Expand Down
6 changes: 4 additions & 2 deletions src/framework/base.path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class BasePath {
}
if (/{\w+}/.test(urlPath)) {
// has variable that we need to check out
urlPath = urlPath.replace(/{(\w+)}/g, (substring, p1) => `:${p1}(.*)`);
urlPath = urlPath.replace(/{(\w+)}/g, (substring, p1) => `:"${p1}"`);
}
this.expressPath = urlPath;
for (const variable in server.variables) {
Expand Down Expand Up @@ -69,7 +69,9 @@ export class BasePath {
}, []);

const allParamCombos = cartesian(...allParams);
const toPath = compile(this.expressPath);
// path-to-regexp v 8.x.x requires we escape the open and close parentheses `(`,`)` added a replace function to catch that use case.
const filteredExpressPath = this.expressPath.replace(/[(]/g, '\\\\(').replace(/[)]/g, '\\\\)');
const toPath = compile(filteredExpressPath);
const paths = new Set<string>();
for (const combo of allParamCombos) {
paths.add(toPath(combo));
Expand Down
12 changes: 12 additions & 0 deletions src/framework/openapi.spec.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,22 @@ export class OpenApiSpecLoader {
// {/path} => /path(*) <--- RFC 6570 format (not supported by openapi)
// const pass1 = part.replace(/\{(\/)([^\*]+)(\*)}/g, '$1:$2$3');

//if wildcard path use new path-to-regex expected model
if(/[*]/g.test(part)){
// /v1/{path}* => /v1/*path)
// /v1/{path}(*) => /v1/*path)
const pass1 = part.replace(/\/{([^}]+)}\({0,1}(\*)\){0,1}/g, '/$2$1');

// substitute params with express equivalent
// /path/{multi}/test/{/*path}=> /path/:multi/test/{/*path}
return pass1.replace(/\{([^\/}]+)}/g, ':$1');
//return pass1;
}
// instead create our own syntax that is compatible with express' pathToRegex
// /{path}* => /:path*)
// /{path}(*) => /:path*)
const pass1 = part.replace(/\/{([^}]+)}\({0,1}(\*)\){0,1}/g, '/:$1$2');

// substitute params with express equivalent
// /path/{id} => /path/:id
return pass1.replace(/\{([^}]+)}/g, ':$1');
Expand Down
9 changes: 3 additions & 6 deletions src/middlewares/openapi.metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,16 @@ export function applyOpenApiMetadata(
const pathKey = openApiRoute.substring((<any>methods).basePath.length);
const schema = openApiContext.apiDoc.paths[pathKey][method.toLowerCase()];
const _schema = responseApiDoc?.paths[pathKey][method.toLowerCase()];

const keys = [];
const strict = !!req.app.enabled('strict routing');
const sensitive = !!req.app.enabled('case sensitive routing');
const pathOpts = {
sensitive,
strict,
};
const regexp = pathToRegexp(expressRoute, keys, pathOpts);
const matchedRoute = regexp.exec(path);

const regexpObj = pathToRegexp(expressRoute, pathOpts);
const matchedRoute = regexpObj.regexp.exec(path);
if (matchedRoute) {
const paramKeys = keys.map((k) => k.name);
const paramKeys = regexpObj.keys.map((k) => k.name);
try {
const paramsVals = matchedRoute.slice(1).map(decodeURIComponent);
const pathParams = zipObject(paramKeys, paramsVals);
Expand Down

0 comments on commit 70cce65

Please sign in to comment.