refactor: ignore non-objects in metadata accessor #1418
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
Currently, the
onModuleInit
hook of the schedule module will traverse every controller and provider to determine the schedulers that are set up in them or anywhere in their dependency tree. If the prototype of any of these classes contains a method that no longer exists later on, the explorer will try to process that method too, eventually resulting in the following error which prevents the Nest application from starting:Now, this will likely not be an issue for the majority of Nest apps, however, in our particular case, we have a PrismaService which extends from the PrismaClient and we had to do some dependency injection magic that sets up some logging mechanisms and then registers a few extensions. According to the Prisma docs, extending a PrismaClient will get rid of some of the methods (which, by extension, were also methods in our PrismaService), so the reflector will fail when it reaches them, throwing the abovementioned exception.
The relevant line in
reflect-metadata
: https://github.com/rbuckton/reflect-metadata/blob/3aeb98af4030be664a66f49bfd164936e0ba1825/Reflect.ts#L994What is the new behavior?
The behavior hasn't changed much, as the methods from the metadata accessor class can already return
undefined
if they wanted to. What I did was making sure that the instance we pass to these methods is an object before attempting to do reflection on them (using the same logic that's found inside thereflect-metadata
package), so that they won't trigger the exception.For now, we're using
patch-package
in our codebase and it does work as intended. Tests also pass.Does this PR introduce a breaking change?
Other information
Perhaps a similar fix should be implemented for all official Nest modules that use this method. Currently the only module we use that had this problem was the scheduler module.