Skip to content

Kotlin (X) -> Unit type not recognized as Consumer in 4.2.0 #1225

@lbilger

Description

@lbilger

Describe the bug
I upgraded from an older version to 4.2.0 and noticed that my Kotlin classes that extend (Message<...>) -> Unit are no longer recognized as Consumers, but as Functions. I debugged through the code and noticed that in e.g. KotlinLambdaToFunctionAutoConfiguration.KotlinFunctionWrapper#isValidKotlinConsumer, the types handed in are actually Java types. This seems to be due to the code in FunctionTypeUtils#discoverFunctionTypeFromClass and #discoverFunctionTypeFromFunctionMethod, where any class that has an apply or invoke method will be resolved to java.util.Function.

If this is intended and isValidKotlinConsumer etc. have some other use, then maybe discoverFunctionTypeFromFunctionMethod should check for the return type of invoke methods and resolve to Consumer if it is void.

The problem this is causing me is that spring-cloud-stream-kafka is trying to bind an out-channel to these functions and falls back to localhost because nothing else is configured for these out-channels

Sample

@Component
class FooConsumer() : (Message<String>) -> Unit {
  override fun invoke(message: Message<String>) {
    println(message.payload)
  }
}

** Workaround **

Use Java functional types instead of Kotlin types, e.g.:

@Component
class FooConsumer() : Consumer<Message<String>> {
  override fun accept(message: Message<String>) {
    println(message.payload)
  }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions