jooby-project / jooby

The modular web framework for Java and Kotlin
https://jooby.io
Apache License 2.0
1.7k stars 199 forks source link

jooby-apt: kotlin: should generates a code with !! null operator for nullable types #3507

Closed jonaskahn closed 3 weeks ago

jonaskahn commented 3 weeks ago

I will go straight to the problem. I make a controller with return type is nullable, but it will make generate source error because the generate source somehow think return type is always not null

bug-report

jknack commented 3 weeks ago

What will be the output if your controller returns null?

If we make the mapping function to returns a nullable response, a new error is generated:

    @Throws(Exception::class)
    override fun install(app: io.jooby.Jooby) {
      /** See [Controller.nullable] */
      app.get("/", this::nullable)
                    ^^^^^^^^^^^^^ problem is here
        .setReturnType(String::class.java)
        .setMvcMethod(Controller::class.java.getMethod("nullable"))
    }

    fun nullable(ctx: io.jooby.Context): String? {
      val c = this.factory.apply(ctx)
      return c.nullable()
    }
 ERROR: Inapplicable candidate(s): fun nullable(ctx: Context): String?

One solution could be:

fun nullable(ctx: io.jooby.Context): String {
      val c = this.factory.apply(ctx)
      return c.nullable()!!
}