Skip to content

Controller method cannot return CompletableFuture if any method argument is Mono #604

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

Conversation

koenpunt
Copy link
Contributor

@koenpunt koenpunt commented Feb 2, 2023

When returning CompletableFutures from datafetchers we sometimes encounter an error in the lines of Unknown value 'java.util.concurrent.CompletableFuture@e77cd7a[Completed normally]'.
By converting the future to a mono it should be handled properly.

This is probably not a complete solution, and maybe the returned CompletableFuture should be handled somewhere different, but this at least seems to solve the issue.

@koenpunt koenpunt changed the title allow returning CompletableFuture's from datafetchers Allow returning CompletableFuture's from datafetchers Feb 2, 2023
Comment on lines 117 to 123
Object result = validateAndInvoke(args, environment);

if (result instanceof CompletableFuture<?>) {
return Mono.fromFuture((CompletableFuture<?>) result);
}

return result;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if more types should be checked here? Or whether this "shortcut" of not processing the return value should be removed completely, because even when there aren't any Mono types in the arguments, the return type still could be a Publisher or CompletableFuture.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 2, 2023
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Mar 7, 2023

Generally, we mean to let CompletableFuture pass through, with no additional processing needed, since GraphQL Java anyway expects asynchronous values to be of that type. The way support Reactor types is to turn Mono into a future in ContextDataFetcherDecorator. So now with this change we would turn the future to Mono, only to turn it back to a future again before it gets to GraphQL Java, which shouldn't be necessary.

Can you provide more details on the original problem, what do you observe and what happens exactly? Is it an error log message, or an error in the response, what component creates that "Unknown value" error message, etc

@koenpunt
Copy link
Contributor Author

koenpunt commented Mar 7, 2023

Can you provide more details on the original problem, what do you observe and what happens exactly?

I can't verify it myself right now, but I believe that if you run the test without the implementation changes you should be able to observe the problem yourself.

@koenpunt
Copy link
Contributor Author

koenpunt commented Mar 7, 2023

But the issue we were experiencing was along the lines of that a resolver function with a signature like fun name(user: User): String, with a parent type like fun user(): CompleteableFuture<User> was failing on the fact that it couldn't find a method on the controller that accepted a CompleteableFuture<User>.

For all those cases we added toMono(), because a signature like fun user(): Mono<User> works allright, but I would expect that to work for CompleteableFutures as well.

@koenpunt
Copy link
Contributor Author

koenpunt commented Mar 7, 2023

I did look into it again, and I now remember what was going wrong; the returned CompleteableFuture is wrapped in a Mono when one of the parameters is a Mono (which is the case when for example there's a Principal argument defined in the resolver method).

This mono is then unwrapped somewhere, but then it's still a CompleteableFuture, and thus no matching resolver method can be found.

@koenpunt koenpunt force-pushed the allow-returning-CompletableFuture-from-datafetcher branch from b4329a4 to 6d125bd Compare March 7, 2023 19:52
When returning `CompletableFuture`s from datafetchers we sometimes encounter an error in the lines of `Unknown value 'java.util.concurrent.CompletableFuture@e77cd7a[Completed normally]'`.
By converting the future to a mono it should be handled properly.
@koenpunt koenpunt force-pushed the allow-returning-CompletableFuture-from-datafetcher branch from 6d125bd to 9b62386 Compare March 7, 2023 19:53
@rstoyanchev rstoyanchev changed the title Allow returning CompletableFuture's from datafetchers Controller method cannot return CompletableFuture if any method argument is Mono Mar 8, 2023
@rstoyanchev rstoyanchev self-assigned this Mar 8, 2023
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 8, 2023
@rstoyanchev rstoyanchev added this to the 1.1.3 milestone Mar 8, 2023
rstoyanchev pushed a commit that referenced this pull request Mar 8, 2023
@rstoyanchev rstoyanchev added the status: backported An issue that has been backported to maintenance branches label Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants