Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(path-to-regexp): path-to-regexp 8.1.0 update #976

Merged
merged 6 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

@cvchauhan cvchauhan Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove let keys = []; and keys = regexpObj.keys;
and directly use
const { regexpObj, keys } = pathToRegexp(expressRoute, pathOpts);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in requested changes.

  • removed let keys =[] and related uses
  • removed const regexp = regepObj.regexp; and related uses
    All current unit tests still pass locally

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
Loading