Skip to content

httpclient: #426 Fix support for 204 no response body for HttpClient reading content #430

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

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public interface BodyAdapter {
* @param type The type of the bean this writer is for
*/
default <T> BodyWriter<T> beanWriter(Type type) {

throw new UnsupportedOperationException("java.lang.reflect.Type is not supported for this adapter");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ static BodyContent asJson(byte[] content) {
* Return the content as UTF8 string.
*/
String contentAsUtf8();

/**
* Return true if the content is empty.
*/
boolean isEmpty();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public byte[] content() {
return content;
}

@Override
public boolean isEmpty() {
return content.length == 0;
}

@Override
public String contentAsUtf8() {
return new String(content, StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public byte[] content() {
return content.getBytes(StandardCharsets.UTF_8);
}

@Override
public boolean isEmpty() {
return content == null || content.isEmpty();
}

@Override
public String contentAsUtf8() {
return content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -274,20 +275,32 @@ <T> BodyReader<T> beanReader(Type type) {
}

<T> T readBean(Class<T> type, BodyContent content) {
if (content.isEmpty()) {
return null;
}
return bodyAdapter.beanReader(type).read(content);
}

<T> List<T> readList(Class<T> type, BodyContent content) {
if (content.isEmpty()) {
return Collections.emptyList();
}
return bodyAdapter.listReader(type).read(content);
}

@SuppressWarnings("unchecked")
<T> T readBean(Type type, BodyContent content) {
if (content.isEmpty()) {
return null;
}
return (T) bodyAdapter.beanReader(type).read(content);
}

@SuppressWarnings("unchecked")
<T> List<T> readList(Type type, BodyContent content) {
if (content.isEmpty()) {
return Collections.emptyList();
}
return (List<T>) bodyAdapter.listReader(type).read(content);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.example;

import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import io.avaje.http.api.BodyString;
import io.avaje.http.api.Controller;
Expand Down Expand Up @@ -155,4 +152,14 @@ String pattern(String patty) {
String patternPlus(String plus) {
return plus.toString();
}

@Get("/maybe/{maybe}")
Person maybePerson(boolean maybe) {
return maybe ? new Person(9, "hi") : null;
}

@Get("/maybeList/{maybe}")
List<Person> maybePersonList(boolean maybe) {
return maybe ? List.of(new Person(9, "hi")) : null; // Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void runAnnotationProcessor() throws Exception {

final var task =
compiler.getTask(
new PrintWriter(System.out), null, null, List.of("--release=20", "-AdisableDirectWrites=true"), null, files);
new PrintWriter(System.out), null, null, List.of("--release=21", "-AdisableDirectWrites=true"), null, files);
task.setProcessors(List.of(new HelidonProcessor()));

assertThat(task.call()).isTrue();
Expand All @@ -61,7 +61,7 @@ void runAnnotationProcessorWithJsonB() throws Exception {

final var task =
compiler.getTask(
new PrintWriter(System.out), null, null, List.of("--release=19"), null, files);
new PrintWriter(System.out), null, null, List.of("--release=21"), null, files);
task.setProcessors(List.of(new HelidonProcessor()));

assertThat(task.call()).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.net.http.HttpResponse;
import java.util.List;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -121,4 +122,38 @@ void ithrowIllegalStateException() {

assertThat(res.statusCode()).isEqualTo(503);
}

@Test
void testNoBodyResponse() {
HttpResponse<Person> res = client.request()
.path("test/maybe/true")
.GET().as(Person.class);

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body().name()).isEqualTo("hi");

HttpResponse<Person> resNoBody = client.request()
.path("test/maybe/false")
.GET().as(Person.class);

assertThat(resNoBody.statusCode()).isEqualTo(204);
assertThat(resNoBody.body()).isNull();
}

@Test
void testNoBodyListResponse() {
HttpResponse<List<Person>> res = client.request()
.path("test/maybeList/true")
.GET().asList(Person.class);

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body().getFirst().name()).isEqualTo("hi");

HttpResponse<List<Person>> resNoBody = client.request()
.path("test/maybeList/false")
.GET().asList(Person.class);

assertThat(resNoBody.statusCode()).isEqualTo(204);
assertThat(resNoBody.body()).isEmpty();
}
}