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

ScheduleExplorer will throw if a provider has a null prototype #142

Closed
cavanmflynn opened this issue Apr 17, 2020 · 3 comments
Closed

ScheduleExplorer will throw if a provider has a null prototype #142

cavanmflynn opened this issue Apr 17, 2020 · 3 comments

Comments

@cavanmflynn
Copy link
Contributor

I'm submitting a...


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

The ScheduleExplorer will throw an unhandled promise rejection upon module initialization if the discovery service returned any providers that have a null prototype.

The provider will make it past the instance check and the error will ultimately be thrown inside the getAllFilteredMethodNames method when attempting to access properties of the null prototype:

(node:81446) UnhandledPromiseRejectionWarning: TypeError: Cannot convert undefined or null to object
    at Function.getOwnPropertyNames (<anonymous>)
    at MetadataScanner.getAllFilteredMethodNames (/source/project/node_modules/@nestjs/core/metadata-scanner.js:24:45)
    at getAllFilteredMethodNames.next (<anonymous>)
    at new Set (<anonymous>)
    at MetadataScanner.scanFromPrototype (/source/project/node_modules/@nestjs/core/metadata-scanner.js:9:29)
    at /source/project/node_modules/@nestjs/schedule/dist/schedule.explorer.js:36:34
    at Array.forEach (<anonymous>)
    at ScheduleExplorer.explore (/source/project/node_modules/@nestjs/schedule/dist/schedule.explorer.js:30:19)
    at ScheduleExplorer.onModuleInit (/source/project/node_modules/@nestjs/schedule/dist/schedule.explorer.js:26:14)
    at MapIterator.iteratee (/source/project/node_modules/@nestjs/core/hooks/on-module-init.hook.js:21:43)
    at MapIterator.next (/source/project/node_modules/iterare/src/map.ts:9:39)
    at IteratorWithOperators.next (/source/project/node_modules/iterare/src/iterate.ts:19:28)
    at Function.from (<anonymous>)
    at IteratorWithOperators.toArray (/source/project/node_modules/iterare/src/iterate.ts:227:22)
    at callOperator (/source/project/node_modules/@nestjs/core/hooks/on-module-init.hook.js:22:10)
    at Object.callModuleInitHook (/source/project/node_modules/@nestjs/core/hooks/on-module-init.hook.js:42:23)

Expected behavior

The ScheduleExplorer gracefully handles a provider with a null prototype. Could be as simple as returning early if the prototype doesn't exist:

if (!instance || !Object.getPrototypeOf(instance)) {
  return;
}

I can open a PR with the above change if it's acceptable.

Minimal reproduction of the problem with instructions

This is reproducible using any provider created with Object.create(null). In my case, it's a custom provider that consists of a proxy where the target is an object created with Object.create(null).

What is the motivation / use case for changing the behavior?

Environment


Nest version:
@nestjs/common: 7.0.2
@nestjs/core: 7.0.2
@nestjs/schedule: 0.3.0
 
For Tooling issues:
- Node version: v12.16.1
- Platform: Mac

Others:

@kamilmysliwiec
Copy link
Member

Thanks for reporting! Would you like to create a PR for this issue?

@cavanmflynn
Copy link
Contributor Author

Will do 👍

@kamilmysliwiec
Copy link
Member

Merged! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants