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

Multibindings fails with jakarta.inject.Provider #4572

Closed
natros opened this issue Jan 12, 2025 · 1 comment
Closed

Multibindings fails with jakarta.inject.Provider #4572

natros opened this issue Jan 12, 2025 · 1 comment
Assignees

Comments

@natros
Copy link

natros commented Jan 12, 2025

In the release notes of dagger 2.55 it seems that there is some support for jakarta.inject.Provider but I do not know if this is supposed to work:

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import dagger.multibindings.IntoMap;
import dagger.multibindings.StringKey;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.inject.Singleton;
import java.util.Map;

public class Foo {
  @Inject
  Foo(Server server) {}

  public static void main(String[] args) {}

  @Singleton
  @Component(modules = MyModule.class)
  interface MyComponent {
    Foo foo();
  }

  @Module
  interface MyModule {
    @Provides
    @IntoMap
    @StringKey("/logout")
    static Filter logoutFilter() {
      return new LogoutFilter();
    }

    @Provides
    @Singleton
    static Server server(Map<String, Provider<Filter>> filters) {
      return new Server();
    }
  }

  interface Filter {}

  static class LogoutFilter implements Filter {}

  static class Server {}
}

it fails with the following error

DaggerFoo_MyComponent.java:60: error: incompatible types: MapFactory<String,Filter> cannot be converted to dagger.internal.Provider<Map<String,jakarta.inject.Provider<Filter>>>
      this.mapOfStringAndProviderOfFilterProvider = MapFactory.<String, Foo.Filter>builder(1).put("/logout", Foo_MyModule_LogoutFilterFactory.create()).build();

The example works with javax.inject.Provider but not with jakarta.inject.Provider. Replacing the provider code with @Binds compiles:

@Binds
    @IntoMap
    @StringKey("/logout")
    Filter logoutFilter(LogoutFilter filter);

I'm testing with dagger 2.55

@Chang-Eric
Copy link
Member

Thanks for reporting this, it seems I did miss a case with @Provides. I will try to get a fix out soon, thanks!

@Chang-Eric Chang-Eric self-assigned this Jan 14, 2025
copybara-service bot pushed a commit that referenced this issue Jan 16, 2025
…es requests for a `Map<K, Provider<V>>` would fail to compile. Similarly, fix support for `@LazyClassKey` with `jakarta.inject.Provider`.

Fixes #4572.

RELNOTES=Fixes #4572. Fix issue with `jakarta.inject.Provider` support where in certain cases requests for a `Map<K, Provider<V>>` would fail to compile.
PiperOrigin-RevId: 715952799
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment