Failure to resolve lists (generics)
See original GitHub issueDescribe the bug If you register two lists and then resolve one inside a bean, the list contains elements of the wrong type. Probably not just limited to lists, but other generics.
To Reproduce
import org.junit.Test
import org.koin.KoinContext
import org.koin.dsl.module.applicationContext
import org.koin.standalone.StandAloneContext
import kotlin.reflect.KClass
class Type1
class Type2
class Type3(val list: List<Type1>)
class ListsTest {
@Test
fun `list resolution failure`() {
val type1Element = Type1()
StandAloneContext.startKoin(
listOf(
applicationContext {
bean { listOf(type1Element) }
},
applicationContext {
bean { listOf(Type2()) }
},
applicationContext {
bean {
Type3(get())
}
}
)
)
(StandAloneContext.koinContext as KoinContext).get<Type3>().list `should equal` listOf(type1Element)
}
}
java.lang.AssertionError:
Expected :[Type1@77b52d12]
Actual :[Type2@2d554825]
And if you try to use the contents of the list, you will get a class cast error.
(StandAloneContext.koinContext as KoinContext).get<Type3>().list.forEach { }
->
java.lang.ClassCastException: Type2 cannot be cast to Type1
Expected behavior
Type3
instance should have been resolved with the list of Type1
that was registered.
Koin project used and used version (please complete the following information):
koin-core version 0.9.3
Issue Analytics
- State:
- Created 5 years ago
- Comments:18 (9 by maintainers)
Top Results From Across the Web
Error about creating array of Generic List in Java
First Code. List<Integer>[] array = (List<Integer>[]) new Object[size];. The reason why the first code fails is because casting does not ...
Read more >Restrictions on Generics (The Java™ Tutorials > Learning ...
Because the Java compiler erases all type parameters in generic code, you cannot verify which parameterized type for a generic type is being...
Read more >Drools user can not use a generic type for a function's ...
[ function getMinFrom (line:4): Unable to resolve type List<String> while building function. java.lang.ClassNotFoundException: Unable to ...
Read more >[#GROOVY-8856] Trait with generic will fail compilation if it ...
Trait with generic will fail compilation if it has a static method that return the defined generic type. Status: Assignee: Priority: Resolution:.
Read more >11. Generic Lists
// remove the object at position 55 list.remove(55); This latter version which would fail because it attempts to assign the return value (an...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, sorry.
I rewrite your initial test with Koin 1.0:
I got an
BeanOverrideException
at start. Then, it’s protected against that case.Then to fix it, the name workaround as you mentioned:
I can add it to the documentation/quick refs to help people on such subject.
The name work around in full.