getAll
with function-based singletons only returns a single instance
#165
Labels
Milestone
getAll
with function-based singletons only returns a single instance
#165
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.
The text was updated successfully, but these errors were encountered: