Koin: [2.0.0-GA5] Named instances resolutions are broken

Created on 17 May 2019  路  7Comments  路  Source: InsertKoinIO/koin

Just migrated from rc-2 to GA5, but I noticed that named definitions resolution is buggy. Root scope named definitions are hidden by un-named definitions in the current scope:

val fooModule = module {
  single(named("NAMED")) { Foo() }

  scope(named("MY_SCOPE")) {
     scoped { Foo() }
  }
} 

val barModule = module {
  // will get un-named Foo from scope instead of named Foo from root ! 
  single { Bar(get<Foo>(named("NAMED"))) }
}

getKoin().getOrCreateScope("scope_id", named( "MY_SCOPE")).get<Bar>()

The bug is located at Scope.findDefinition() since BeanRegistry.findDefinition() will return a definition matching the class (on the current scope !) if there's no named definition into that scope:

fun findDefinition(
            qualifier: Qualifier? = null,
            clazz: KClass<*>
    ): BeanDefinition<*>? =
            qualifier?.let { findDefinitionByName(it.toString()) }
                    ?: findDefinitionByClass(clazz)

core accepted issue

All 7 comments

Your scope is shadowing definitions from the global space, which is normal: when you define a scope and resolve from it, we are looking firstly from the scope definitions and then from the global scope.

Apart from that your case here is quite tricky ^^... as I wouldn't advise spreading scoped instances around to leak it?

Mmm... I'm asking for a named instance, and I'm ending up with an unnamed one! It was working perfectly on rc2. Could you read the issue carefully again?

If there's no definitions matching the qualifier on the current scope, we should look into the root scope before looking for a "class-only" definition.

The look-up for definitions should be : qualifier?.let { findByName(it) ?: root.findByName(it) } ?: findByClass(clazz) ?: root.findByClass(clazz)

yes I see

try with 2.0.0-GA6

Wow, that was fast! Will test that tomorrow, I'm AFK. Thanks 馃憤

GA6 fixed it! Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

luna-vulpo picture luna-vulpo  路  4Comments

erikhuizinga picture erikhuizinga  路  3Comments

caleb-allen picture caleb-allen  路  4Comments

Jeevuz picture Jeevuz  路  4Comments

CristianMG picture CristianMG  路  3Comments