koin icon indicating copy to clipboard operation
koin copied to clipboard

Unable to register two singletons with Primary types of an interface.

Open MarcusDunn opened this issue 11 months ago • 4 comments

Describe the bug Missing definitions when underyling class is unknown by koin. I belive this is due to the name of the definition overriding another.

To Reproduce Steps to reproduce the behavior:

This test fails.

class TestGetAllWithLogicInSingleBlock {
    interface Foo

    class A : Foo
    class B : Foo

    @Test
    fun `check get all with logic in single block`() {
        val app = koinApplication {
            modules(module {
                single {
                        if (Random.nextBoolean()) {
                            A()
                        } else {
                            B()
                        }
                } bind Foo::class

                single {
                    if (Random.nextBoolean()) {
                        A()
                    } else {
                        B()
                    }
                } bind Foo::class
            })
        }
        app.koin.getAll<Foo>().shouldHaveSize(2)
    }
}

Expected behavior I would expect two instances of Foo to be available in getAll<Foo>()

Koin module and version: koin-core:4.0.1

Additional information adding qualifiers fixes the issue:

This test passes (as expected)

@Test
    fun `check get all with logic in single block`() {
        val app = koinApplication {
            modules(module {
                single(qualifier("1")) {
                        if (Random.nextBoolean()) {
                            A()
                        } else {
                            B()
                        }
                } bind Foo::class

                single(qualifier("2")) {
                    if (Random.nextBoolean()) {
                        A()
                    } else {
                        B()
                    }
                } bind Foo::class
            })
        }
        app.koin.getAll<Foo>().shouldHaveSize(2)
    }

MarcusDunn avatar Jan 14 '25 02:01 MarcusDunn

linked to this? https://github.com/InsertKoinIO/koin/issues/1811#issuecomment-2588619709

arnaudgiuliani avatar Jan 16 '25 17:01 arnaudgiuliani

I believe it's unrelated.

MarcusDunn avatar Jan 17 '25 17:01 MarcusDunn

For this one, the issue I think is that these both generate an index of com.example.Foo::_ROOT_, which is why the qualifier fixes it. I'm not sure exactly what a fix would be without breaking the working as intended overriding behavior.

MarcusDunn avatar Jan 17 '25 17:01 MarcusDunn

Problem in your case, you can override the current same index A :: Foo if you take both A on your script

arnaudgiuliani avatar Jan 28 '25 15:01 arnaudgiuliani