Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.58k stars 591 forks source link

kong.unirest.HttpRequest.asEmpty() returns raw type #461

Closed Adrodoc closed 1 year ago

Adrodoc commented 1 year ago

Describe the bug The return type of method kong.unirest.HttpRequest.asEmpty() is HttpResponse, but it should be HttpResponse<Empty>, just like the return type of kong.unirest.HttpRequest.asEmptyAsync() is CompletableFuture<HttpResponse<Empty>> and the return type of kong.unirest.HttpRequest.asString() is HttpResponse<String>.

To Reproduce The following code does not compile:

String path = "...";
Object dto = new Object();
Unirest.post(path) //
    .body(dto) //
    .asEmpty() //
    .ifFailure(response -> {
      throw new RuntimeException(response.getStatusText());
    });

You get the compiler warning Type safety: The method ifFailure(Consumer) belongs to the raw type HttpResponse. References to generic type HttpResponse<T> should be parameterized and the error The method getStatusText() is undefined for the type Object. The code can be rewritten to

String path = "...";
Object dto = new Object();
HttpResponse<?> asEmpty = Unirest.post(path) //
    .body(dto) //
    .asEmpty();
asEmpty //
    .ifFailure(response -> {
      throw new RuntimeException(response.getStatusText());
    });

to make it work, but this prevents method chaining. If you replace asEmpty() with asString() it also compiles as expected. It's odd that asEmpty() seems to be the only Method in HttpRequest that returns a raw type.

Expected behavior The return type of method kong.unirest.HttpRequest.asEmpty() is HttpResponse, but it should be HttpResponse<Empty>. If a response type of HttpResponse<Empty> does not work for some reason (like backwards compatibility) the return type should be at least HttpResponse<?> to allow method chaining.

Environmental Data:

ryber commented 1 year ago

this is complete in 3.14.0 / 4.0.0-RC6