Skip to content

Commit 24ed110

Browse files
authored
Merge pull request #233 from SentryMan/nima-media
(nima-generator) Support `InputStream` Return Types
2 parents 8fc3fa2 + 6b2c63d commit 24ed110

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.io.IOException;
44
import java.util.List;
55
import java.util.Objects;
6+
import java.util.Optional;
67
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
79

810
import javax.annotation.processing.Filer;
911
import javax.annotation.processing.Messager;
@@ -91,10 +93,6 @@ public static void init(ProcessingEnvironment env, PlatformAdapter adapter) {
9193
init(env, adapter, true);
9294
}
9395

94-
private static boolean isTypeAvailable(String canonicalName) {
95-
return null != typeElement(canonicalName);
96-
}
97-
9896
public static TypeElement typeElement(String canonicalName) {
9997
return CTX.get().elementUtils.getTypeElement(canonicalName);
10098
}
@@ -198,4 +196,20 @@ public static boolean disabledDirectWrites() {
198196
public static Filer filer() {
199197
return CTX.get().filer;
200198
}
199+
200+
public static boolean isAssignable2Interface(String type, String superType) {
201+
return type.equals(superType)
202+
|| Optional.ofNullable(typeElement(type)).stream()
203+
.flatMap(ProcessingContext::superTypes)
204+
.anyMatch(superType::equals);
205+
}
206+
207+
static Stream<String> superTypes(Element element) {
208+
final Types types = CTX.get().typeUtils;
209+
return types.directSupertypes(element.asType()).stream()
210+
.filter(type -> !type.toString().contains("java.lang.Object"))
211+
.map(superType -> (TypeElement) types.asElement(superType))
212+
.flatMap(e -> Stream.concat(superTypes(e), Stream.of(e)))
213+
.map(Object::toString);
214+
}
201215
}

http-generator-nima/src/main/java/io/avaje/http/generator/helidon/nima/ControllerMethodWriter.java

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

3-
import static io.avaje.http.generator.core.ProcessingContext.disabledDirectWrites;
3+
import static io.avaje.http.generator.core.ProcessingContext.*;
44
import static io.avaje.http.generator.core.ProcessingContext.platform;
55

66
import java.util.List;
@@ -41,7 +41,7 @@ void writeRule() {
4141
}
4242

4343
void writeHandler(boolean requestScoped) {
44-
writer.append(" private void _%s(ServerRequest req, ServerResponse res) {", method.simpleName()).eol();
44+
writer.append(" private void _%s(ServerRequest req, ServerResponse res) throws Exception {", method.simpleName()).eol();
4545
final var bodyType = method.bodyType();
4646
if (bodyType != null) {
4747

@@ -80,11 +80,11 @@ void writeHandler(boolean requestScoped) {
8080
}
8181

8282
final var segments = method.pathSegments();
83-
if (!segments.isEmpty()) {
83+
if (segments.fullPath().contains("{")) {
8484
writer.append(" var pathParams = req.path().pathParameters();").eol();
8585
}
86-
final var matrixSegments = segments.matrixSegments();
87-
for (final PathSegments.Segment matrixSegment : matrixSegments) {
86+
87+
for (final PathSegments.Segment matrixSegment : segments.matrixSegments()) {
8888
matrixSegment.writeCreateSegment(writer, platform());
8989
}
9090

@@ -127,9 +127,14 @@ void writeHandler(boolean requestScoped) {
127127
writer.append(")");
128128
}
129129
writer.append(");").eol();
130+
130131
if (!method.isVoid()) {
131132
writeContextReturn();
132-
if (producesJson()) {
133+
134+
if (isAssignable2Interface(method.returnType().toString(), "java.io.InputStream")) {
135+
final var uType = UType.parse(method.returnType());
136+
writer.append(" result.transferTo(res.outputStream());", uType.shortName()).eol();
137+
} else if (producesJson()) {
133138
final var uType = UType.parse(method.returnType());
134139
writer.append(" %sJsonType.toJson(result, JsonOutput.of(res));", uType.shortName()).eol();
135140
} else {
@@ -166,7 +171,9 @@ private void writeContextReturn() {
166171
case APPLICATION_JSON -> writer.append(contentTypeString + "APPLICATION_JSON);").eol();
167172
case TEXT_HTML -> writer.append(contentTypeString + "TEXT_HTML);").eol();
168173
case TEXT_PLAIN -> writer.append(contentTypeString + "TEXT_PLAIN);").eol();
169-
case UNKNOWN -> writer.append(contentTypeString + "create(\"%s\"));", produces).eol();
174+
case UNKNOWN -> writer
175+
.append(contentTypeString + "create(\"%s\"));", producesOp.orElse("UNKNOWN"))
176+
.eol();
170177
}
171178
}
172179

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ String enumQueryImplied(String s, @QueryParam ServerType type) {
5757

5858
@InstrumentServerContext
5959
@Get(value = "/inputStream")
60-
String stream(InputStream stream) {
61-
return stream.toString();
60+
InputStream stream(InputStream stream) throws Exception {
61+
return stream;
6262
}
6363

6464

0 commit comments

Comments
 (0)