Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin suspend functions return type is incorrect #2638

Closed
abelmulq opened this issue Feb 27, 2024 · 3 comments
Closed

Kotlin suspend functions return type is incorrect #2638

abelmulq opened this issue Feb 27, 2024 · 3 comments

Comments

@abelmulq
Copy link

abelmulq commented Feb 27, 2024

In what version(s) of Spring AMQP are you seeing this issue?

3.0.9(for my project) & 3.2.3(latest version for demo)

Describe the bug

It is known in #1210 this library added a kotlin coroutine support. This is fine as long as the function doesn't return a value. But as soon as our function returns a value the return value type resolver in AbstractAdaptableMessageListener.asyncSuccess throws an error because it gets the invocation result type of java.lang.Object which can't be cast to ParameterizedType.

To Reproduce

A kotlin coroutine with a return value of String? annotated with @RabbitListener

Expected behavior

The return type must be correctly set in InvocationResult

Sample
https://github.com/abelmulq/spring-amqp-2638-sample

@artembilan artembilan added this to the 3.1.3 milestone Feb 28, 2024
spring-builds pushed a commit that referenced this issue Feb 28, 2024
Fixes: #2638

The `AbstractAdaptableMessageListener.asyncSuccess()` assumes only `Mono` & `CompletableFuture` return types.
With Kotlin `suspend` it is just plain `Object`.

* Fix `AbstractAdaptableMessageListener.asyncSuccess()` to check if `returnType` is not simple `Object.class`
before going deep for its generic actual argument.

(cherry picked from commit 08ead3b)
@abelmulq
Copy link
Author

Thank you @artembilan for the quick fix.

@PsyBrainy
Copy link

Hello @abelmulq and @artembilan,

I've been following the discussion on Kotlin suspend function return types and appreciate the fixes and updates made. I am curious if similar support or modifications have been considered for the KafkaListener in the context of handling Kafka messages with Kotlin coroutines? Specifically, is there any ongoing work or planned updates to ensure that KafkaListeners can handle return types correctly when used with Kotlin suspend functions?

Thank you for your efforts and any information you can provide on this matter.

@artembilan
Copy link
Member

@PsyBrainy ,

It would be great if you ask such a question in Spring for Apache Kafka project by itself.

Yes, that work has been done for the @KafkaListener as well. See respective GH issue: spring-projects/spring-kafka#1189.

And docs for upcoming 3.2 version: https://docs.spring.io/spring-kafka/reference/3.2/whats-new.html#x32-async-return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants