Skip to content

Commit 566c7b7

Browse files
authored
httpclient: #426 Fix support for 204 no response body for HttpClient reading content (#430)
* httpclient: #427 Fix support for 204 no response body for HttpClient reading content * Update test NimaProcessorTest to use release 21
1 parent 3ba92ac commit 566c7b7

File tree

8 files changed

+76
-7
lines changed

8 files changed

+76
-7
lines changed

http-client/src/main/java/io/avaje/http/client/BodyAdapter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public interface BodyAdapter {
2323
* @param type The type of the bean this writer is for
2424
*/
2525
default <T> BodyWriter<T> beanWriter(Type type) {
26-
2726
throw new UnsupportedOperationException("java.lang.reflect.Type is not supported for this adapter");
2827
}
2928

http-client/src/main/java/io/avaje/http/client/BodyContent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ static BodyContent asJson(byte[] content) {
5656
* Return the content as UTF8 string.
5757
*/
5858
String contentAsUtf8();
59+
60+
/**
61+
* Return true if the content is empty.
62+
*/
63+
boolean isEmpty();
5964
}

http-client/src/main/java/io/avaje/http/client/DBodyContent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public byte[] content() {
3636
return content;
3737
}
3838

39+
@Override
40+
public boolean isEmpty() {
41+
return content.length == 0;
42+
}
43+
3944
@Override
4045
public String contentAsUtf8() {
4146
return new String(content, StandardCharsets.UTF_8);

http-client/src/main/java/io/avaje/http/client/DBodyContentS.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public byte[] content() {
2525
return content.getBytes(StandardCharsets.UTF_8);
2626
}
2727

28+
@Override
29+
public boolean isEmpty() {
30+
return content == null || content.isEmpty();
31+
}
32+
2833
@Override
2934
public String contentAsUtf8() {
3035
return content;

http-client/src/main/java/io/avaje/http/client/DHttpClientContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.http.HttpRequest;
66
import java.net.http.HttpResponse;
77
import java.time.Duration;
8+
import java.util.Collections;
89
import java.util.List;
910
import java.util.Map;
1011
import java.util.concurrent.CompletableFuture;
@@ -274,20 +275,32 @@ <T> BodyReader<T> beanReader(Type type) {
274275
}
275276

276277
<T> T readBean(Class<T> type, BodyContent content) {
278+
if (content.isEmpty()) {
279+
return null;
280+
}
277281
return bodyAdapter.beanReader(type).read(content);
278282
}
279283

280284
<T> List<T> readList(Class<T> type, BodyContent content) {
285+
if (content.isEmpty()) {
286+
return Collections.emptyList();
287+
}
281288
return bodyAdapter.listReader(type).read(content);
282289
}
283290

284291
@SuppressWarnings("unchecked")
285292
<T> T readBean(Type type, BodyContent content) {
293+
if (content.isEmpty()) {
294+
return null;
295+
}
286296
return (T) bodyAdapter.beanReader(type).read(content);
287297
}
288298

289299
@SuppressWarnings("unchecked")
290300
<T> List<T> readList(Type type, BodyContent content) {
301+
if (content.isEmpty()) {
302+
return Collections.emptyList();
303+
}
291304
return (List<T>) bodyAdapter.listReader(type).read(content);
292305
}
293306

tests/test-nima-jsonb/src/main/java/org/example/TestController.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package org.example;
22

33
import java.io.InputStream;
4-
import java.util.LinkedHashMap;
5-
import java.util.List;
6-
import java.util.Map;
7-
import java.util.Set;
4+
import java.util.*;
85

96
import io.avaje.http.api.BodyString;
107
import io.avaje.http.api.Controller;
@@ -155,4 +152,14 @@ String pattern(String patty) {
155152
String patternPlus(String plus) {
156153
return plus.toString();
157154
}
155+
156+
@Get("/maybe/{maybe}")
157+
Person maybePerson(boolean maybe) {
158+
return maybe ? new Person(9, "hi") : null;
159+
}
160+
161+
@Get("/maybeList/{maybe}")
162+
List<Person> maybePersonList(boolean maybe) {
163+
return maybe ? List.of(new Person(9, "hi")) : null; // Collections.emptyList();
164+
}
158165
}

tests/test-nima-jsonb/src/test/java/io/avaje/http/generator/NimaProcessorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void runAnnotationProcessor() throws Exception {
4545

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

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

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

6767
assertThat(task.call()).isTrue();

tests/test-nima-jsonb/src/test/java/org/example/TestControllerTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import java.net.http.HttpResponse;
6+
import java.util.List;
67

78
import org.junit.jupiter.api.AfterAll;
89
import org.junit.jupiter.api.Test;
@@ -121,4 +122,38 @@ void ithrowIllegalStateException() {
121122

122123
assertThat(res.statusCode()).isEqualTo(503);
123124
}
125+
126+
@Test
127+
void testNoBodyResponse() {
128+
HttpResponse<Person> res = client.request()
129+
.path("test/maybe/true")
130+
.GET().as(Person.class);
131+
132+
assertThat(res.statusCode()).isEqualTo(200);
133+
assertThat(res.body().name()).isEqualTo("hi");
134+
135+
HttpResponse<Person> resNoBody = client.request()
136+
.path("test/maybe/false")
137+
.GET().as(Person.class);
138+
139+
assertThat(resNoBody.statusCode()).isEqualTo(204);
140+
assertThat(resNoBody.body()).isNull();
141+
}
142+
143+
@Test
144+
void testNoBodyListResponse() {
145+
HttpResponse<List<Person>> res = client.request()
146+
.path("test/maybeList/true")
147+
.GET().asList(Person.class);
148+
149+
assertThat(res.statusCode()).isEqualTo(200);
150+
assertThat(res.body().getFirst().name()).isEqualTo("hi");
151+
152+
HttpResponse<List<Person>> resNoBody = client.request()
153+
.path("test/maybeList/false")
154+
.GET().asList(Person.class);
155+
156+
assertThat(resNoBody.statusCode()).isEqualTo(204);
157+
assertThat(resNoBody.body()).isEmpty();
158+
}
124159
}

0 commit comments

Comments
 (0)