Skip to content

Support InputStream/byte[] Body Types #176

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 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public static Map<String, UType> jsonTypes(ControllerReader reader) {
methodReader -> {
addJsonBodyType(methodReader, addToMap);
if (!methodReader.isVoid()) {
UType uType = UType.parse(methodReader.returnType());
var uType = UType.parse(methodReader.returnType());

if (uType.mainType().equals("java.util.concurrent.CompletableFuture")) {
if ("java.util.concurrent.CompletableFuture".equals(uType.mainType())) {
uType = uType.paramRaw();
}

Expand All @@ -51,6 +51,8 @@ private static void addJsonBodyType(MethodReader methodReader, Consumer<UType> a
methodReader.params().stream()
.filter(MethodParam::isBody)
.map(MethodParam::utype)
.filter(s -> !s.full().startsWith("java.io.InputStream"))
.filter(s -> !s.full().startsWith("byte[]"))
.forEach(addToMap);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ public boolean isBodyMethodParam() {

@Override
public String bodyAsClass(UType type) {
if (useJsonB) {
return type.shortName() + "JsonType.fromJson(ctx.bodyInputStream())";
if (type.full().startsWith("java.io.InputStream")) {
return "ctx.bodyInputStream()";
} else if (type.full().startsWith("byte[]")) {
return "ctx.bodyAsBytes()";
} else {
if (useJsonB) {
return type.shortName() + "JsonType.fromJson(ctx.bodyInputStream())";
}
return "ctx.bodyAsClass(" + type.mainType() + ".class)";
}
return "ctx.bodyAsClass(" + type.mainType() + ".class)";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.avaje.http.generator.helidon.nima;

import static io.avaje.http.generator.core.ProcessingContext.platform;

import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -40,7 +41,10 @@ void writeHandler(boolean requestScoped) {
writer.append(" private void _%s(ServerRequest req, ServerResponse res) {", method.simpleName()).eol();
final var bodyType = method.bodyType();
if (bodyType != null) {
if (useJsonB) {

if (bodyType.equals("InputStream")) {
writer.append(" var %s = req.content().inputStream();", method.bodyName()).eol();
} else if (useJsonB) {
final var fieldName =
method.params().stream()
.filter(MethodParam::isBody)
Expand All @@ -53,18 +57,20 @@ void writeHandler(boolean requestScoped) {
} else {
// use default helidon content negotiation
method.params().stream()
.filter(MethodParam::isBody)
.forEach(
param -> {
final var type = param.utype();
writer.append(" var %s = req.content().as(", method.bodyName());
if (type.param0() != null) {
writer.append("new io.helidon.common.GenericType<%s>() {}", type.full());
} else {
writer.append("%s.class", type.full());
}
writer.append(");").eol();
});
.filter(MethodParam::isBody)
.forEach(
param -> {
final var type = param.utype();

writer.append(" var %s = req.content()", method.bodyName());
writer.append(".as(");
if (type.param0() != null) {
writer.append("new io.helidon.common.GenericType<%s>() {}", type.full());
} else {
writer.append("%s.class", type.full());
}
writer.append(");").eol();
});
}
} else if (usesFormParams()) {
writer.append(" var formParams = req.content().as(Parameters.class);").eol();
Expand Down Expand Up @@ -111,7 +117,7 @@ void writeHandler(boolean requestScoped) {
if (!method.isVoid()) {
writeContextReturn();
if (producesJson()) {
final UType uType = UType.parse(method.returnType());
final var uType = UType.parse(method.returnType());
writer.append(" %sJsonType.toJson(result, res.outputStream());", uType.shortName()).eol();
} else {
writer.append(" res.send(result);").eol();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.example.myapp.web.test;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -47,4 +48,14 @@ String enumQueryImplied(String s, @QueryParam ServerType type) {
String mapTest(Map<String, List<String>> strings) {
return strings.toString();
}

@Get("/inputStream")
String stream(InputStream stream) {
return stream.toString();
}

@Get("/byteArray")
String bytes(byte[] array) {
return array.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.example;

import java.io.InputStream;
import java.util.Set;

import io.avaje.http.api.Controller;
Expand Down Expand Up @@ -45,4 +46,9 @@ String enumMultiQuery(@QueryParam @Default({"FFA", "PROXY"}) Set<ServerType> typ
String enumQueryImplied(String s, @QueryParam ServerType type) {
return type.name();
}

@Get("/inputStream")
String stream(InputStream stream) {
return stream.toString();
}
}