InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

Moving from Symbol.for() to Symbol() doesn't resolve dependencies correctly

Open Rmonik opened this issue 1 year ago • 1 comments

Given that you use a global object which holds Symbol("Something") values and having bound a class to that identifier. If I try to get that identifier from the container, i get a "No bindings found" error. When changing the Symbol to a Symbol.for, this issue is not present.

Expected Behavior

Symbol() should work just as well as Symbol.for.

Current Behavior

Using a Symbol() doesn't correctly resolve dependencies.

Possible Solution

Steps to Reproduce (for bugs)

  1. Create a constant: const a = Symbol("a");
  2. Bind something to it: container.bind(a).to(MyClass);
  3. Get it from the container: container.getAsync(a);
  4. Error: No matching bindings found for Symbol(a)

Context

I would like to use true Symbols (as opposed to Symbol.for, which i don't see the point in using) as we're facing naming collisions.

Your Environment

  • Version used: 6.0.1

Stack trace

Rmonik avatar Feb 22 '24 15:02 Rmonik

I cannot reproduce this issue. The test case i use:

import { expect } from "chai";
import { Container } from "../../src/inversify";
import { injectable } from "../../src/annotation/injectable";
@injectable()
class World {
  hello() {
    return "Hello World!";
  }
}

describe("Symbol binding", () => {
  it("Should support Symbol() as service identifier", () => {
    const fooIdentifier = Symbol("FooInterface");
    const container = new Container();
    container.bind(fooIdentifier).to(World);
    container.getAsync(fooIdentifier).then((foo: any) => {
      foo.hello();
    });
    expect(container.get(fooIdentifier)).to.be.instanceof(World);
  });
});

Version: 6.02

saifeiLee avatar Feb 29 '24 05:02 saifeiLee