Open SIMULATAN opened 2 months ago
Interesting. Can you just share the generated DSL? A definition should allow several type binding. Annotations should take original class and second type binding
@Definition("me.simulatan.koin.getall")
public fun Module.defineFirstResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.FirstResource() } bind(me.simulatan.koin.getall.Resource::class)
@Definition("me.simulatan.koin.getall")
public fun Module.defineSecondResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.SecondResource() } bind(me.simulatan.koin.getall.Resource::class)
@Definition("me.simulatan.koin.getall")
public fun Module.definefirstResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.firstResource() } bind(me.simulatan.koin.getall.Resource::class)
@Definition("me.simulatan.koin.getall")
public fun Module.definesecondResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.secondResource() } bind(me.simulatan.koin.getall.Resource::class)
@Definition("me.simulatan.koin.getall")
public fun Module.definethirdResource() : KoinDefinition<*> = single() { me.simulatan.koin.getall.thirdResource() } bind(me.simulatan.koin.getall.Resource::class)
(I removed the boilerplate)
Describe the bug Using
Koin#getAll()
to get@Single
annotated functions sharing a return type only returns a single instance. The others get lost during registration and don't even show up in the internal Koin_instances
map.To Reproduce
@Single
getAll
- only one function is returnedPlease find a dead simple reproducer here: https://github.com/SIMULATAN/koin-getall-reproducer
Expected behavior All instances are returned.
Koin project used and used version koin-core 4.0.0-RC1 koin-annotations 1.4.0-RC4
Analysis From my analysis, the problem is that koin-annotations registers the singletons using the same
binds
class: the function return type. This poses an issue with koin's internalinstanceRegistry._instances
map, which stores the instances using the class name as the key. As established, this class name is the interface type, which causes an arbitrary number of singletons with that type to only result in a single getable instance. In short: using subclasses,getAll
works as koin saves the implementations by their concrete class name in the internal map. However, with@Single fun
, the "concrete class" becomes the superclass, resulting in overrides in the internal map.Proposed solution Naming beans created from functions, for example based on the function name. Although not perfect, this may prevent collisions and it serves a similar system as class implementations.