question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Optional with CompletableFuture?

See original GitHub issue

Hello,

I am using retrofit 2.5.0 and have an API like:

CompletableFuture<Optional<String>> getCustomer(int customerId)

But the response is null instead of Optional.empty().

Here’s my retrofit builder:

new Retrofit.Builder()
                .baseUrl(getBaseUrl())
                .addConverterFactory(new NullOnEmptyConverterFactory())
                .client(getOkHttpClient(getOAuthInterceptor(decrypter())))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(create(objectMapper))
                .build();

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, Apr 20, 2019

NullOnEmpty is for broken servers who return 0-length bodies instead of JSON literals of null for an absent body.

Right now there’s not a good way to differentiate this case because the HTTP status code bypasses the converter. If you don’t care about distinguishing between 204 and 205 you can use an OkHttp interceptor to rewrite them to 200’s with a null literal as the root element.

Beyond that, we probably need to add a method to Converter.Factory for handling HTTP 204/205 since there is no body but the user requested an instance of a particular type. I need to think about it more.

0reactions
RaviHcommented, Apr 20, 2019

Great. I might have a question later but thank you for the explanation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

From Optional<CompletableFuture<T>> to ... - Stack Overflow
Show activity on this post. I would like to convert Optional<CompletableFuture<T>> to CompletableFuture<Optional<T>> .
Read more >
CompletableFuture with Optional : r/javahelp - Reddit
So I'd think you'd check the optional and throw an exception if it's an empty. I'm assuming exceptions work in lambdas though. I'm...
Read more >
Optional in Java 8: Part 2 - Educative.io
This method returns the value present in the optional. If no value is present, then it throws the exception created by the provided...
Read more >
Write Clean Asynchronous Code With CompletableFuture ...
CompletableFuture allows us to write non-blocking code by running a task on a separate thread than the main application thread and notifying the ......
Read more >
20 Practical Examples: Java's CompletableFuture - DZone
In addition to implementing the CompletionStage interface, CompletableFuture also implements Future , which represents a pending asynchronous ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found