Skip to content

Commit 2661f79

Browse files
committed
support generic body type
1 parent cec0011 commit 2661f79

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public interface BodyAdapter {
1717
*/
1818
<T> BodyWriter<T> beanWriter(Class<?> type);
1919

20+
/**
21+
* Return a BodyWriter to write beans of this type as request content.
22+
*
23+
* @param type The type of the bean this writer is for
24+
*/
25+
default <T> BodyWriter<T> beanWriter(ParameterizedType type) {
26+
27+
throw new UnsupportedOperationException("Parameterized types not supported for this adapter");
28+
}
29+
2030
/**
2131
* Return a BodyReader to read response content and convert to a bean.
2232
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ <T> BodyContent write(T bean, Class<?> type, String contentType) {
269269
return bodyAdapter.beanWriter(type).write(bean, contentType);
270270
}
271271

272+
<T> BodyContent write(T bean, ParameterizedType type, String contentType) {
273+
return bodyAdapter.beanWriter(type).write(bean, contentType);
274+
}
275+
272276
<T> BodyReader<T> beanReader(Class<T> type) {
273277
return bodyAdapter.beanReader(type);
274278
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ public HttpClientRequest body(Object bean, Class<?> type) {
281281
return body(bean, type, null);
282282
}
283283

284+
@Override
285+
public HttpClientRequest body(Object bean, ParameterizedType type) {
286+
encodedRequestBody = context.write(bean, type, null);
287+
return this;
288+
}
289+
284290
@Override
285291
public HttpClientRequest body(Object bean, Class<?> type, String contentType) {
286292
encodedRequestBody = context.write(bean, type, contentType);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.avaje.http.client;
22

33
import java.io.InputStream;
4+
import java.lang.reflect.ParameterizedType;
45
import java.net.http.HttpRequest;
56
import java.net.http.HttpResponse;
67
import java.nio.file.Path;
@@ -312,6 +313,19 @@ default HttpClientRequest queryParam(String name, Collection<String> values) {
312313
*/
313314
HttpClientRequest body(Object bean, Class<?> type);
314315

316+
/**
317+
* Set the body as a bean additionally specifying the type that will be
318+
* used to serialise the content (e.g. JsonbAdapter).
319+
* <p>
320+
* Specifying the type allows the bean instance to be a type that extends
321+
* a type that is known to JsonbAdapter / the body content adapter used.
322+
*
323+
* @param bean The body content as an instance
324+
* @param type The parameterized type used by the body content adapter to write the body content
325+
* @return The request being built
326+
*/
327+
HttpClientRequest body(Object bean, ParameterizedType type);
328+
315329
/**
316330
* Set the body as a bean with the given content type and additionally specifying
317331
* the type that will be used to serialise the content (e.g. JsonbAdapter).

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public <T> BodyWriter<T> beanWriter(Class<?> cls) {
4747
return (BodyWriter<T>) beanWriterCache.computeIfAbsent(cls, aClass -> new JWriter<>(jsonb.type(cls)));
4848
}
4949

50+
@SuppressWarnings("unchecked")
51+
@Override
52+
public <T> BodyWriter<T> beanWriter(ParameterizedType type) {
53+
return (BodyWriter<T>) beanWriterCache.computeIfAbsent(type, aClass -> new JWriter<>(jsonb.type(type)));
54+
}
55+
5056
@SuppressWarnings("unchecked")
5157
@Override
5258
public <T> BodyReader<T> beanReader(Class<T> cls) {

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ private void writeBody() {
251251
for (MethodParam param : method.params()) {
252252
ParamType paramType = param.paramType();
253253
if (paramType == ParamType.BODY) {
254-
writer.append(" .body(%s, %s.class)", param.name(), param.utype().shortType()).eol();
254+
writer.append(" .body(%s, ", param.name());
255+
writeGeneric(param.utype());
255256
}
256257
}
257258
}

0 commit comments

Comments
 (0)