I was developing an app in android when I encountered this problem that I don't really know if it's a known bug or if I should file a bug report. Here's the code:
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
data class Status(val msg: String, val integer: Int, val msg2:String)
interface API {
@GET("/json/gson")
suspend fun gson(): Status
}
val gsonApiCalls = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://localhost:8080")
.build()
.create(API::class.java)
suspend fun main(){
val result = gsonApiCalls.gson()
println(result)
result.msg2?.let(::println) ?: println("Null result!")
}
The local server code (Ktor):
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.routing.*
import io.ktor.http.*
import io.ktor.gson.*
import io.ktor.features.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
data class StandardResponse(val msg: String, val integer: Int, val msg2:String?)
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(ContentNegotiation) {
gson {
}
}
routing {
get("/") {
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
}
get("/json/gson") {
call.respond(StandardResponse("Hello",34, null))
}
}
}
And the result is:
Status(msg=Hello, integer=34, msg2=null)
Null result!
The code above is an example I made for testing this behaviour but the first time I realised this "bug" was in the android app when I got a NullPointerException, and I didn't understand why.
The app was using a remote node server and the same thing happens. Capture showing the linter thinks this can't be possible
If this is a bug, should I report this to Kotlin or Retrofit?