Skip to content

Commit 831f644

Browse files
authored
[http-client] Fix Non-Json bodytypes/Add InputStream body method (#200)
* input stream body * fix non json generation
1 parent cd6fe76 commit 831f644

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

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
@@ -312,6 +312,12 @@ public HttpClientRequest body(Supplier<? extends InputStream> streamSupplier) {
312312
return this;
313313
}
314314

315+
@Override
316+
public HttpClientRequest body(InputStream stream) {
317+
this.body = HttpRequest.BodyPublishers.ofInputStream(() -> stream);
318+
return this;
319+
}
320+
315321
@Override
316322
public HttpClientRequest body(Path file) {
317323
try {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ default HttpClientRequest queryParam(String name, Collection<String> values) {
364364
*/
365365
HttpClientRequest body(Supplier<? extends InputStream> supplier);
366366

367+
/**
368+
* Set the body content with supplied InputStream.
369+
*
370+
* @param supplier The InputStream content to send as body content
371+
* @return The request being built
372+
*/
373+
HttpClientRequest body(InputStream stream);
374+
367375
/**
368376
* Set the body content with supplied InputStream.
369377
*

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ private void methodStart(Append writer) {
5555
if (count++ > 0) {
5656
writer.append(", ");
5757
}
58-
writer.append(param.utype().shortType()).append(" ");
58+
var paramType =
59+
"java.util.function.Supplier<?extendsjava.io.InputStream>".equals(param.utype().full())
60+
? "Supplier<? extends InputStream>"
61+
: param.utype().shortType();
62+
writer.append(paramType).append(" ");
5963
writer.append(param.name());
6064
}
6165
writer.append(") {").eol();
@@ -250,10 +254,29 @@ private void writeFormParam(MethodParam param, ParamType paramType) {
250254
private void writeBody() {
251255
for (MethodParam param : method.params()) {
252256
ParamType paramType = param.paramType();
257+
253258
if (paramType == ParamType.BODY) {
254-
writer.append(" .body(%s, ", param.name());
255-
writeGeneric(param.utype());
256-
writer.append(")").eol();
259+
260+
var type = param.utype().full();
261+
if ("java.net.http.HttpRequest.BodyPublisher".equals(type)
262+
|| "java.lang.String".equals(type)
263+
|| "byte[]".equals(type)
264+
|| "java.io.InputStream".equals(type)
265+
|| "java.util.function.Supplier<?extendsjava.io.InputStream>".equals(type)
266+
|| "java.util.function.Supplier<java.io.InputStream>".equals(type)
267+
|| "java.nio.file.Path".equals(type)
268+
|| "io.avaje.http.client.BodyContent".equals(type)) {
269+
270+
writer.append(" .body(%s)", param.name()).eol();
271+
272+
} else {
273+
274+
writer.append(" .body(%s, ", param.name());
275+
writeGeneric(param.utype());
276+
writer.append(")").eol();
277+
}
278+
279+
return;
257280
}
258281
}
259282
}

http-generator-core/src/main/java/io/avaje/http/generator/core/UType.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,13 @@ public Set<String> importTypes() {
203203
for (String type : allTypes) {
204204
if (!type.startsWith("java.lang.") && type.indexOf('.') > -1) {
205205
if (type.startsWith("java")) {
206-
set.add(type.replace("[]", ""));
206+
set.add(type.replace("[]", "").replace("?extends", ""));
207207
} else {
208-
set.add(innerTypesImport(type).replace("[]", ""));
208+
set.add(innerTypesImport(type).replace("[]", "").replace("?extends", ""));
209209
}
210210
}
211211
}
212+
set.remove("?");
212213
return set;
213214
}
214215

0 commit comments

Comments
 (0)