Closed nielsvanvelzen closed 1 year ago
yep, I saw that. A patch can be done quickly
@nielsvanvelzen in your case, do you use koinApplication
or startKoin
to run Koin outside of Ktor?
I forgot to add it in the reproducible sample but we do actually call startKoin a few lines down.
val koinApp = koinApplication {
// Add modules
modules(
// ....
)
}
// ....
startKoin(koinApp)
Another reproducer, aka issue: This example fails with 3.5.0 but works with 3.4.3:
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.*
import io.ktor.server.response.respond
import io.ktor.server.routing.*
import io.ktor.server.testing.testApplication
import org.junit.jupiter.api.Test
import org.koin.dsl.module
import org.koin.ktor.plugin.Koin
class ReproducerTest {
@Test
fun `minimalistic reproducer`() {
testMyApplication {
val response = it.get("testurl") {}
response.status.shouldBe(HttpStatusCode.OK)
response.bodyAsText().shouldContain("Test response")
}
}
private fun testMyApplication(test: suspend (jsonClient: HttpClient) -> Unit) = testApplication {
application {
install(Koin) {
modules(
module {
single { this@application }
single(createdAtStart = true) { KtorMyModule(get()) }
},
)
}
}
test.invoke(createClient {})
}
}
class KtorMyModule(application: Application) {
init {
application.routing {
get("testurl") { call.respond(HttpStatusCode.OK, "Test response") }
}
}
}
Will deploy Koin 3.5.1 to help unlock for ktor
@arnaudgiuliani
funny, for us, koin 3.5.0 was working fine with ktor, but 3.5.1 is failing, reporting
No Koin instance started. Use install(Koin) or startKoin()
I will try to investigate it further at a later time, but for now we will return to use koin 3.5.0, that is working fine with ktor (at least for us).
I've just tested the update and everything is working again for us with 3.5.1. Thanks for the fast response!
@arnaudgiuliani funny, for us, koin 3.5.0 was working fine with ktor, but 3.5.1 is failing, reporting
No Koin instance started. Use install(Koin) or startKoin()
I will try to investigate it further at a later time, but for now we will return to use koin 3.5.0, that is working fine with ktor (at least for us).
@arnaudgiuliani Moving "createdAtStart" from the module to the individual single's, fixed my issue with koin 3.5.1 (again, everything was working fine prior to 3.5.1).
@hnljp can you share more about your config and usage of Koin. Seems that your are using it out of Koin Ktor plugin?
We have used the following pattern in our Ktor application:
install(Koin) { configure() }
install(StatusPages) { configure(get(LoggerFactory::class.java)) }
This works perfectly in 3.4.1 but does not work in 3.5.x and fails with KoinApplication has not been started
. Bug in Koin? Or what would be the suggested pattern?
There was a problem in 3.5.0. Are you using koin-ktor 3.5.1 @samzurcher?
Yes, I was using 3.5.1. We have now added a manual startKoin(this)
to start koin and it is working now.
weird, don't understand what's changed here for you 🤔
Describe the bug
Starting with version 3.5.0 Koin is not working inside a ktor server anymore and throws the following exception:
(stacktrace reduced to only the API part of our application)
I couldn't find anything in the changelog but the ktor documentation page now says "Koin Ktor plugin uses isolated Koin context. You won't be able to start Koin outside of Ktor". Which might explain this behavior. This won't do for us though as the ktor part of our app is just one of many parts, and Koin must me initialized on a higher level.
To Reproduce Steps to reproduce the behavior:
Expected behavior Koin should just work, like in previous versions.
Koin module and version: 3.5.0
Snippet or Sample project to help reproduce
This snippet reproduces the issue.