spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.66k stars 38.14k forks source link

Refine `KotlinDetector.isKotlinType` documentation for Kotlin 2.x lambdas #32905

Closed antechrestos closed 5 months ago

antechrestos commented 5 months ago

Using the spring cloud stream with reactive used to work well by instanciating kotlin function as follows

@Configuration(proxyBeanMethods = false)
class MessageConfiguration {

   @Bean
   fun myMessageConsumer() = { rawMessagesFlux ->
            rawMessagesFlux
                .flatMap { // ... }
                .then()
   }

Moving to kotlin 2.0 fails as generated bean class returns false when passing it to KotlinDetector.isKotlinType. Hence starting of spring cloud stream module fails as KotlinFunctionWrapper is not put and the Configuration expects java Function, Consumer or Supplier ...

sdeleuze commented 5 months ago

@antechrestos Can you please provide a reproducer as an attached project or a link to a repository?

antechrestos commented 5 months ago

@sdeleuze yes: https://github.com/antechrestos/spring-function-kotlin-2.0-bug

sdeleuze commented 5 months ago

I confirm this change of behavior caused by the fact that generation of lambda functions using invokedynamic is now the default, and I don't think we can do something about it at Spring Framework level except in terms of documentation so I turn it into a documentation issue.

FYI SAM-converted lambda like HelloHandler { ... } were already generated via invokedynamic as of Kotlin 1.5, for non regular lambda { ... } that new in Kotlin 2.0, and @JvmSerializableLambda can be used as a workaround (fun helloConsumer(helloHandler: HelloHandler): (Flux<Message<ByteArray>>) -> Mono<Void> = @JvmSerializableLambda { ...).

@antechrestos Could you please create a related issue in Spring Cloud Function linking the repro in order to allow @olegz and @sobychacko to review how BeanFactoryAwareFunctionRegistry (sources) logic could be changed?

antechrestos commented 5 months ago

done

antechrestos commented 5 months ago

I confirm that the workaround using@JvmSerializableLambda makes the trick.

@sdeleuze do you want me to close this PR and let the PR on cloud-function repository be the reference?

sdeleuze commented 5 months ago

@antechrestos if I am not mistaken, this is an issue not a PR and I have repurposed it to refine the documentation, so please keep it opened, I will close it with a related commit.