Skip to content

(Client) Support Generic Body Type #182

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 4 commits into from
Mar 13, 2023
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
10 changes: 10 additions & 0 deletions http-client/src/main/java/io/avaje/http/client/BodyAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public interface BodyAdapter {
*/
<T> BodyWriter<T> beanWriter(Class<?> type);

/**
* Return a BodyWriter to write beans of this type as request content.
*
* @param type The type of the bean this writer is for
*/
default <T> BodyWriter<T> beanWriter(ParameterizedType type) {

throw new UnsupportedOperationException("Parameterized types not supported for this adapter");
}

/**
* Return a BodyReader to read response content and convert to a bean.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ <T> BodyContent write(T bean, Class<?> type, String contentType) {
return bodyAdapter.beanWriter(type).write(bean, contentType);
}

<T> BodyContent write(T bean, ParameterizedType type, String contentType) {
return bodyAdapter.beanWriter(type).write(bean, contentType);
}

<T> BodyReader<T> beanReader(Class<T> type) {
return bodyAdapter.beanReader(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ public HttpClientRequest body(Object bean, Class<?> type) {
return body(bean, type, null);
}

@Override
public HttpClientRequest body(Object bean, ParameterizedType type) {
encodedRequestBody = context.write(bean, type, null);
return this;
}

@Override
public HttpClientRequest body(Object bean, Class<?> type, String contentType) {
encodedRequestBody = context.write(bean, type, contentType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.http.client;

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

/**
* Set the body as a bean additionally specifying the type that will be
* used to serialise the content (e.g. JsonbAdapter).
* <p>
* Specifying the type allows the bean instance to be a type that extends
* a type that is known to JsonbAdapter / the body content adapter used.
*
* @param bean The body content as an instance
* @param type The parameterized type used by the body content adapter to write the body content
* @return The request being built
*/
HttpClientRequest body(Object bean, ParameterizedType type);

/**
* Set the body as a bean with the given content type and additionally specifying
* the type that will be used to serialise the content (e.g. JsonbAdapter).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public <T> BodyWriter<T> beanWriter(Class<?> cls) {
return (BodyWriter<T>) beanWriterCache.computeIfAbsent(cls, aClass -> new JWriter<>(jsonb.type(cls)));
}

@SuppressWarnings("unchecked")
@Override
public <T> BodyWriter<T> beanWriter(ParameterizedType type) {
return (BodyWriter<T>) beanWriterCache.computeIfAbsent(type, aClass -> new JWriter<>(jsonb.type(type)));
}

@SuppressWarnings("unchecked")
@Override
public <T> BodyReader<T> beanReader(Class<T> cls) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,12 @@ private void writeEnd() {
String known = KNOWN_RESPONSE.get(returnType.full());
if (known != null) {
writer.append(" %s", known).eol();
} else if (COMPLETABLE_FUTURE.equals(returnType.mainType())) {
writeAsyncResponse();
} else if (HTTP_CALL.equals(returnType.mainType())) {
writeCallResponse();
} else {
if (COMPLETABLE_FUTURE.equals(returnType.mainType())) {
writeAsyncResponse();
} else if (HTTP_CALL.equals(returnType.mainType())) {
writeCallResponse();
} else {
writeSyncResponse();
}
writeSyncResponse();
}
}
writer.append(" }").eol().eol();
Expand All @@ -141,27 +139,32 @@ private void writeResponse(UType type) {
if (isList(mainType)) {
writer.append(".list(");
writeGeneric(param1);
writer.append(");").eol();
} else if (isStream(mainType)) {
writer.append(".stream(");
writeGeneric(param1);
writer.append(");").eol();
} else if (isHttpResponse(mainType)) {
if (bodyHandlerParam == null) {
UType paramType = type.paramRaw();
if (paramType.mainType().equals("java.util.List")) {
final UType paramType = type.paramRaw();
if ("java.util.List".equals(paramType.mainType())) {
writer.append(".asList(");
writeGeneric(paramType.paramRaw());
} else if (paramType.mainType().equals("java.util.stream.Stream")) {
} else if ("java.util.stream.Stream".equals(paramType.mainType())) {
writer.append(".asStream(");
writeGeneric(paramType.paramRaw());
} else {
writer.append(".as(");
writeGeneric(paramType);
}
writer.append(");").eol();
} else {
writer.append(".handler(%s);", bodyHandlerParam.name()).eol(); }
writer.append(".handler(%s);", bodyHandlerParam.name()).eol();
}
} else {
writer.append(".bean(");
writeGeneric(type);
writer.append(");").eol();
}
}

Expand All @@ -177,19 +180,16 @@ void writeGeneric(UType type) {
} else {
writer.append("%s.class", Util.shortName(type.mainType()));
}
writer.append(");").eol();
}

private void writeQueryParams(PathSegments pathSegments) {
for (MethodParam param : method.params()) {
ParamType paramType = param.paramType();
if (paramType == ParamType.QUERYPARAM) {
if (pathSegments.segment(param.paramName()) == null) {
if (isMap(param)) {
writer.append(" .queryParam(%s)", param.name()).eol();
} else {
writer.append(" .queryParam(\"%s\", %s)", param.paramName(), param.name()).eol();
}
for (final MethodParam param : method.params()) {
final ParamType paramType = param.paramType();
if (paramType == ParamType.QUERYPARAM && pathSegments.segment(param.paramName()) == null) {
if (isMap(param)) {
writer.append(" .queryParam(%s)", param.name()).eol();
} else {
writer.append(" .queryParam(\"%s\", %s)", param.paramName(), param.name()).eol();
}
}
}
Expand Down Expand Up @@ -251,7 +251,9 @@ private void writeBody() {
for (MethodParam param : method.params()) {
ParamType paramType = param.paramType();
if (paramType == ParamType.BODY) {
writer.append(" .body(%s, %s.class)", param.name(), param.utype().shortType()).eol();
writer.append(" .body(%s, ", param.name());
writeGeneric(param.utype());
writer.append(")").eol();
}
}
}
Expand All @@ -278,19 +280,19 @@ private boolean isMap(MethodParam param) {
}

private boolean isMap(String type0) {
return type0.equals("java.util.Map");
return "java.util.Map".equals(type0);
}

private boolean isList(String type0) {
return type0.equals("java.util.List");
return "java.util.List".equals(type0);
}

private boolean isStream(String type0) {
return type0.equals("java.util.stream.Stream");
return "java.util.stream.Stream".equals(type0);
}

private boolean isHttpResponse(String type0) {
return type0.equals("java.net.http.HttpResponse");
return "java.net.http.HttpResponse".equals(type0);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.example.myapp.web.test;
package org.example.myapp.web;

public enum ServerType {
PROXY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Map;
import java.util.Set;

import org.example.myapp.web.ServerType;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Default;
import io.avaje.http.api.Form;
Expand Down