InsertKoinIO / koin

Koin - a pragmatic lightweight dependency injection framework for Kotlin & Kotlin Multiplatform
https://insert-koin.io
Apache License 2.0
9.08k stars 719 forks source link

InstanceCreationException when passing mix of nullable and non-nullable parameters #1740

Closed aradomski closed 5 months ago

aradomski commented 11 months ago

When I'm passing mix of nullable and non-nullable parameters using parameterArrayOf, InstanceCreationException appears because koin attempts to cast null as next parameter type

To Reproduce See snippet

Expected behavior When using method getOrNull null is returned, when null was provided and internal index of ParametersHolder is incremented . Next call get or getOrNull should provide appropriate item from array provided by parameterArrayOf

Koin module and version: koin-core:3.5.0

Snippet or Sample project to help reproduce Tested on android

fun initKoin(appDeclaration: KoinAppDeclaration = {}) =
    startKoin {
        appDeclaration()
        modules(viewModelModule)
    }

val viewModelModule = module {
    factory {
        val projectId: String? = it.getOrNull()
        val workspaceId: String = it.get()
        val imageId: String? = it.getOrNull()
        val someData:SomeData = it.get()
        SomeViewModel(projectId, workspaceId, imageId, someData)
    }
}
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val viewModel: SomeViewModel = GlobalContext.get().get {
                val projectId: String? = "ProjectId"
                val workspaceId: String = "WorkspaceId"
                val imageId: String? = null
                val someData: SomeData = SomeData("Some data")
                parameterArrayOf(projectId, workspaceId, imageId, someData)
            }

            Text(
                text = "projectId=${viewModel.projectId} " +
                        "workspaceId = ${viewModel.workspaceId} " +
                        "imageId = ${viewModel.imageId} " +
                        "someData = ${viewModel.data} "
            )
        }
    }
aradomski commented 11 months ago

Proposition of solution #1741

Nek-12 commented 10 months ago

Duplicate of #1456

stale[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Nek-12 commented 5 months ago

@arnaudgiuliani don't tell me you followed the accompanist footsteps and added this garbage bot. Please remove it as all open issues are still valid. It is simpler for me to remove Koin from my project than to keep the issues not fixed for years open by commenting on them over and over.

Nek-12 commented 5 months ago

@arnaudgiuliani this is not completed