Skip to content

Commit

Permalink
Merge pull request #14615 from LubomirIgonda1/fix-express-route-params
Browse files Browse the repository at this point in the history
This PR fix use case when lrp keys offset is calculate to 0. I discover
this case after update express from v4.19.2 to v4.21.2
For example:

api/v1/users/1/posts
Old behavior

sentry build req._reconstructedRoute as:
api/v1/users/1/posts
New behavior

sentry build req._reconstructedRoute as:
api/v1/users/:param/posts
  • Loading branch information
AbhiPrasad authored Dec 11, 2024
2 parents fde81a6 + 0e9c7f6 commit 4690bf3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/tracing-internal/src/node/integrations/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,16 @@ export const extractOriginalRoute = (
regexp?: Layer['regexp'],
keys?: Layer['keys'],
): string | undefined => {
if (!path || !regexp || !keys || Object.keys(keys).length === 0 || !keys[0]?.offset) {
if (
!path ||
!regexp ||
!keys ||
Object.keys(keys).length === 0 ||
keys[0]?.offset === undefined ||
keys[0]?.offset === null
) {
return undefined;
}

const orderedKeys = keys.sort((a, b) => a.offset - b.offset);

// add d flag for getting indices from regexp result
Expand Down
9 changes: 8 additions & 1 deletion packages/tracing-internal/test/node/express.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ if (major >= 16) {
expect(extractOriginalRoute(path, regex, [key])).toBeUndefined();
});

it('should return the original route path when valid inputs are provided', () => {
it('should return the original route path when valid inputs are provided first static value then dynamic', () => {
const path = '/router/123';
const regex = /^\/router\/(\d+)$/;
const keys = [{ name: 'pathParam', offset: 8, optional: false }];
expect(extractOriginalRoute(path, regex, keys)).toBe('/router/:pathParam');
});

it('should return the original route path when valid inputs are provided first dynamic value then static', () => {
const path = '/123/router';
const regex = /^(?:\/([^/]+?))\/router\/?(?=\/|$)/i;
const keys = [{ name: 'pathParam', offset: 0, optional: false }];
expect(extractOriginalRoute(path, regex, keys)).toBe('/:pathParam/router');
});

it('should handle multiple parameters in the route', () => {
const path = '/user/42/profile/username';
const regex = /^\/user\/(\d+)\/profile\/(\w+)$/;
Expand Down

0 comments on commit 4690bf3

Please sign in to comment.