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

Update Ktor to 3.0.3 #2066

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

timemanx
Copy link

📝 Description

Updated Ktor and updated failing tests and fixed compile issues resulting from the update.
Also updated some libraries.

🔗 Related Issues

#2037

@timemanx timemanx mentioned this pull request Dec 21, 2024
every { application } returns mockk(relaxed = true) {
every { receivePipeline } returns ApplicationReceivePipeline()
}
coEvery { receiveNullable<Any>(any()) } answers { callOriginal() }
Copy link
Author

@timemanx timemanx Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

receiveNullable() (which is called from ApplicationCall.receive() in KtorGraphQLRequestParser.parsePostRequest()) is now an instance method instead of an extension function which is why ApplicationCall.receive() returns null when using a mock.
Using callOriginal() calls the instance method instead.

In Ktor 2.3.12
https://github.com/ktorio/ktor/blob/2.3.12/ktor-server/ktor-server-core/jvmAndNix/src/io/ktor/server/request/ApplicationReceiveFunctions.kt#L95

In Ktor 3.0.x
https://github.com/ktorio/ktor/blob/main/ktor-server/ktor-server-core/common/src/io/ktor/server/application/PipelineCall.kt#L84

@@ -31,11 +31,11 @@ class CustomScalarKotlinxTests {

@Test
fun `verify custom scalars are correctly serialized and deserialized`() {
val engine = embeddedServer(CIO, port = 0, module = Application::graphQLModule)
val embeddedServer = embeddedServer(CIO, port = 0, module = Application::graphQLModule)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

embeddedServer() now returns an EmbeddedServer which contains the ApplicationEngine.
https://ktor.io/docs/migrating-3.html#EmbeddedServer


fun Application.graphQLModule() {
install(WebSockets) {
pingPeriod = Duration.ofSeconds(1)
pingPeriod = 1.seconds
Copy link
Author

@timemanx timemanx Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


private fun testModule(block: suspend io.ktor.server.testing.ApplicationTestBuilder.() -> kotlin.Unit) = testApplication {
environment {
config = ApplicationConfig(("application.conf"))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modules need to be explicitly loaded.
https://ktor.io/docs/migrating-3.html#test-module-loading

@@ -61,6 +62,8 @@ open class KtorGraphQLRequestParser(

private suspend fun parsePostRequest(request: ApplicationRequest): GraphQLServerRequest? = try {
request.call.receive()
} catch (e: CannotTransformContentToTypeException) {
throw e
} catch (e: IOException) {
throw IllegalStateException("Invalid HTTP request - unable to parse GraphQL request from POST payload", e)
Copy link
Author

@timemanx timemanx Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed InvalidPayloadException that was added in the commit 30c32b9.

Instead, CannotTransformContentToTypeException can be re-thrown to maintain current behaviour (and also pass this test).

The reason being that ContentTransformationException (super class of CannotTransformContentToTypeException) derives from IOException in Ktor 3.x which results in the exception being caught here in parsePostRequest() instead of being passed on in the chain to Ktor's default interceptor (which is where the 415 error response is returned).
Re-throwing this exception allows Ktor's internal interceptor to catch it and maintain existing behaviour.

Reference: ContentTransformationException in 2.3.13 vs 3.x.

An alternative to this solution could be to instead throw UnsupportedMediaTypeException. However, that would also require handling it in GraphQLStatusPages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants