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

Classes with the same names are not injected properly #5306

Closed
stevenhair opened this issue Aug 21, 2020 · 2 comments
Closed

Classes with the same names are not injected properly #5306

stevenhair opened this issue Aug 21, 2020 · 2 comments
Labels
needs triage This issue has not been looked into

Comments

@stevenhair
Copy link

stevenhair commented Aug 21, 2020

Bug Report

Current behavior

If you inject two classes with the same name, the first class in the providers list will be injected for both classes. This is a pretty rare issue, but many third-party packages export classes with generic names such as "Client".

A couple workarounds:

  • Restructure your dependency tree so that you don't have both classes present in the same module (this may not be practical depending on your code)
  • Extend the third-party class using a less generic name and use that throughout your code (this could lead to some bugs if you're not careful to always use your wrapper):
    import { Client } from 'foo';
    
    export class FooClient extends Client {}
  • If you control one of the classes, simply rename it

Input Code

Here is a reproduction of this bug: https://github.com/stevenhair/congenial-palm-tree

Imagine that you have the module:

import { Client as FooClient } from 'foo';
import { Client as BarClient } from 'bar';

@Module({
  providers: [
    FooClient,
    BarClient,
    MyService,
  ]
})
export class MyModule {}

and a service:

import { Client as FooClient } from 'foo';
import { Client as BarClient } from 'bar';

export class MyService {
  constructor(
    private foo: FooClient,  // this will be an instance of FooClient
    private bar: BarClient,  // this will also be an instance of FooClient
  ) {}
}

Expected behavior

The correct class should be injected

Possible Solution

I suspect that somewhere along the line, the class name is being converted to a string. I'm not sure if this is in Nest.js code or the Typescript metadata. If it's in Nest.js code, hopefully it could be fixed by using a symbol or a more unique string. If it's due to the Typescript metadata, I'm not sure if this is solvable in Nest.

Environment


Nest version: 7.0.0
@stevenhair stevenhair added the needs triage This issue has not been looked into label Aug 21, 2020
@jmcdo29
Copy link
Member

jmcdo29 commented Aug 21, 2020

Seems like a duplicate of this: #789

@stevenhair
Copy link
Author

@jmcdo29 I believe that you are correct, it does appear to be a duplicate. I tried searching before opening the bug, but I guess that I didn't search hard enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants