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

getAll with function-based singletons only returns a single instance #165

Open
SIMULATAN opened this issue Aug 28, 2024 · 2 comments
Open
Labels
status:checking Ticket is currently being checked type:issue Something isn't working
Milestone

Comments

@SIMULATAN
Copy link

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

  1. Create a service interface
  2. Implement it anonymously in an arbitrary (2+) number of function bodies
  3. Annotate those functions with @Single
  4. Attempt to use getAll - only one function is returned

Please 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 internal instanceRegistry._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.
reproducer in intellij with the debugger showing the internal koin map
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.

@arnaudgiuliani arnaudgiuliani added this to the 2.0 milestone Sep 17, 2024
@arnaudgiuliani arnaudgiuliani added the status:checking Ticket is currently being checked label Sep 17, 2024
@arnaudgiuliani
Copy link
Member

Interesting. Can you just share the generated DSL? A definition should allow several type binding. Annotations should take original class and second type binding

@SIMULATAN
Copy link
Author

@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)

@arnaudgiuliani arnaudgiuliani added the type:issue Something isn't working label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:checking Ticket is currently being checked type:issue Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants