Skip to content

fix(nestjs): Copy metadata in custom decorators #15598

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

Merged
merged 3 commits into from
Mar 5, 2025
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
3 changes: 2 additions & 1 deletion packages/nestjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
},
"devDependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0"
"@nestjs/core": "^10.0.0",
"reflect-metadata": "^0.2.2"
},
"peerDependencies": {
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0",
Expand Down
47 changes: 39 additions & 8 deletions packages/nestjs/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig):
monitorConfig,
);
};

copyFunctionNameAndMetadata({ originalMethod, descriptor });

return descriptor;
};
};
Expand All @@ -28,7 +31,7 @@ export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig):
* A decorator usable to wrap arbitrary functions with spans.
*/
export function SentryTraced(op: string = 'function') {
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
return function (_target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value as (...args: unknown[]) => Promise<unknown> | unknown; // function can be sync or async

descriptor.value = function (...args: unknown[]) {
Expand All @@ -43,13 +46,7 @@ export function SentryTraced(op: string = 'function') {
);
};

// preserve the original name on the decorated function
Object.defineProperty(descriptor.value, 'name', {
value: originalMethod.name,
configurable: true,
enumerable: true,
writable: true,
});
copyFunctionNameAndMetadata({ originalMethod, descriptor });

return descriptor;
};
Expand All @@ -71,6 +68,40 @@ export function SentryExceptionCaptured() {
return originalCatch.apply(this, [exception, host, ...args]);
};

copyFunctionNameAndMetadata({ originalMethod: originalCatch, descriptor });

return descriptor;
};
}

/**
* Copies the function name and metadata from the original method to the decorated method.
* This ensures that the decorated method maintains the same name and metadata as the original.
*
* @param {Function} params.originalMethod - The original method being decorated
* @param {PropertyDescriptor} params.descriptor - The property descriptor containing the decorated method
*/
function copyFunctionNameAndMetadata({
originalMethod,
descriptor,
}: {
descriptor: PropertyDescriptor;
originalMethod: (...args: unknown[]) => unknown;
}): void {
// preserve the original name on the decorated function
Object.defineProperty(descriptor.value, 'name', {
value: originalMethod.name,
configurable: true,
enumerable: true,
writable: true,
});

// copy metadata
if (typeof Reflect !== 'undefined' && typeof Reflect.getMetadataKeys === 'function') {
const originalMetaData = Reflect.getMetadataKeys(originalMethod);
for (const key of originalMetaData) {
const value = Reflect.getMetadata(key, originalMethod);
Reflect.defineMetadata(key, value, descriptor.value);
}
}
}
Loading
Loading