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

kotlin: Java type mismatch on @Transactional during compile time #3477

Closed jonaskahn closed 1 month ago

jonaskahn commented 1 month ago

I have a simple controller like this one:

package io.github.jonaskahn.app.controller.authen

import io.github.jonaskahn.app.repositories.UsersRepository
import io.jooby.annotation.GET
import io.jooby.annotation.Path
import io.jooby.annotation.Transactional
import jakarta.inject.Inject

@Path("/auth")
class AuthenticationController @Inject constructor(private val userRepository: UsersRepository) {

    @GET("/token")
    @Transactional(true)
    fun generateToken(): String {
        val data = userRepository.findByUsername("admin")
        data?.fullName = "Jonas"
        data?.password = "1111"
        data?.let { userRepository.save(it) }
        return ""
    }
}

When I run compile, it throw me an error like Java type mismatch expected (Mutable)Map<String!, Any!> but found (Mutable)Map<String!, Boolean!>!. Use explicit cast I found out something went wrong with generate code in Jooby. Here is it:

@Throws(Exception::class)
override fun install(app: io.jooby.Jooby) {
      /** See [AuthenticationController.generateToken] */
      app.get("/auth/token", this::generateToken)
        .setAttributes(java.util.Map.of(
            "Transactional", true))
        .setReturnType(String::class.java)
        .setMvcMethod(AuthenticationController::class.java.getMethod("generateToken"))
    }

Could anyone help me overcome with this error?

edgar-espina-wpp commented 1 month ago

yea kotlin type system is bit harder, going to look.

Could anyone help me overcome with this error?

You could try to remove or comment the annotation for now, which makes all the routes transactional by default: https://jooby.io/modules/hibernate/#hibernate-transactional-request

jonaskahn commented 1 month ago

Yeah, I changed to global transaction for all request by default. But it would be great to control the transaction in specific cases. ^^