Skip to content

Commit 4690bf3

Browse files
authored
Merge pull request #14615 from LubomirIgonda1/fix-express-route-params
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
2 parents fde81a6 + 0e9c7f6 commit 4690bf3

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/tracing-internal/src/node/integrations/express.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,16 @@ export const extractOriginalRoute = (
413413
regexp?: Layer['regexp'],
414414
keys?: Layer['keys'],
415415
): string | undefined => {
416-
if (!path || !regexp || !keys || Object.keys(keys).length === 0 || !keys[0]?.offset) {
416+
if (
417+
!path ||
418+
!regexp ||
419+
!keys ||
420+
Object.keys(keys).length === 0 ||
421+
keys[0]?.offset === undefined ||
422+
keys[0]?.offset === null
423+
) {
417424
return undefined;
418425
}
419-
420426
const orderedKeys = keys.sort((a, b) => a.offset - b.offset);
421427

422428
// add d flag for getting indices from regexp result

packages/tracing-internal/test/node/express.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,20 @@ if (major >= 16) {
8787
expect(extractOriginalRoute(path, regex, [key])).toBeUndefined();
8888
});
8989

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

97+
it('should return the original route path when valid inputs are provided first dynamic value then static', () => {
98+
const path = '/123/router';
99+
const regex = /^(?:\/([^/]+?))\/router\/?(?=\/|$)/i;
100+
const keys = [{ name: 'pathParam', offset: 0, optional: false }];
101+
expect(extractOriginalRoute(path, regex, keys)).toBe('/:pathParam/router');
102+
});
103+
97104
it('should handle multiple parameters in the route', () => {
98105
const path = '/user/42/profile/username';
99106
const regex = /^\/user\/(\d+)\/profile\/(\w+)$/;

0 commit comments

Comments
 (0)