Skip to content

Commit 634b4f2

Browse files
committed
void body parameters
1 parent f89db13 commit 634b4f2

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ void writeHandler(boolean requestScoped) {
102102
writer.append(" ");
103103
if (!method.isVoid()) {
104104
writer.append("var result = ");
105+
} else if (method.isVoid()
106+
&& params.stream().noneMatch(p -> "ServerResponse".equals(p.getShortType()))) {
107+
throw new IllegalStateException(
108+
"Void controller methods must have a ServerResponse parameter");
105109
}
106110

107111
if (method.includeValidate()) {

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.util.List;
55
import java.util.Optional;
6-
import java.util.function.Predicate;
76

87
import javax.lang.model.type.DeclaredType;
98

@@ -19,7 +18,6 @@ class ControllerWriter extends BaseControllerWriter {
1918

2019
private static final String AT_GENERATED = "@Generated(\"avaje-helidon-nima-generator\")";
2120
private final boolean useJsonB;
22-
private List<MethodReader> jsonBMethodList;
2321

2422
ControllerWriter(ControllerReader reader, ProcessingContext ctx, boolean jsonB)
2523
throws IOException {
@@ -28,14 +26,6 @@ class ControllerWriter extends BaseControllerWriter {
2826
if (useJsonB) {
2927
reader.addImportType("io.avaje.jsonb.Jsonb");
3028
reader.addImportType("io.avaje.jsonb.JsonType");
31-
jsonBMethodList =
32-
reader.getMethods().stream()
33-
.filter(MethodReader::isWebMethod)
34-
.filter(Predicate.not(MethodReader::isVoid))
35-
.filter(m -> !"byte[]".equals(m.getReturnType().toString()))
36-
.filter(
37-
m -> m.getProduces() == null || m.getProduces().toLowerCase().contains("json"))
38-
.toList();
3929
}
4030
// reader.addImportType("io.helidon.common.http.FormParams");
4131
reader.addImportType("io.helidon.nima.webserver.http.HttpRules");
@@ -103,8 +93,18 @@ private void writeClassStart() {
10393
writer.append(" private final Validator validator;").eol();
10494
}
10595

96+
List<MethodReader> jsonMethods;
10697
if (useJsonB) {
107-
writeJsonBTypeFields();
98+
jsonMethods =
99+
reader.getMethods().stream()
100+
.filter(MethodReader::isWebMethod)
101+
.filter(m -> !"byte[]".equals(m.getReturnType().toString()))
102+
.filter(
103+
m -> m.getProduces() == null || m.getProduces().toLowerCase().contains("json"))
104+
.toList();
105+
writeJsonBTypeFields(jsonMethods);
106+
} else {
107+
jsonMethods = null;
108108
}
109109

110110
writer.eol();
@@ -125,14 +125,14 @@ private void writeClassStart() {
125125
}
126126

127127
if (useJsonB) {
128-
writeJsonBTypeAssignments();
128+
writeJsonBTypeAssignments(jsonMethods);
129129
}
130130

131131
writer.append(" }").eol().eol();
132132
}
133133

134-
public void writeJsonBTypeFields() {
135-
for (final MethodReader methodReader : jsonBMethodList) {
134+
public void writeJsonBTypeFields(List<MethodReader> jsonMethods) {
135+
for (final MethodReader methodReader : jsonMethods) {
136136
// body types
137137
if (methodReader.getBodyType() != null) {
138138
methodReader.getParams().stream()
@@ -145,6 +145,11 @@ public void writeJsonBTypeFields() {
145145
param.getUType().full(), methodReader.simpleName())
146146
.eol());
147147
}
148+
149+
if (methodReader.isVoid()) {
150+
continue;
151+
}
152+
148153
// return types
149154
if (methodReader.getReturnType() instanceof final DeclaredType fullType) {
150155
final var typeArgs = fullType.getTypeArguments();
@@ -168,11 +173,16 @@ public void writeJsonBTypeFields() {
168173
}
169174
}
170175

171-
public void writeJsonBTypeAssignments() {
176+
public void writeJsonBTypeAssignments(List<MethodReader> jsonMethods) {
172177

173-
for (final MethodReader methodReader : jsonBMethodList) {
178+
for (final MethodReader methodReader : jsonMethods) {
174179
// body types
175180
writeBodyJsonType(methodReader);
181+
182+
if (methodReader.isVoid()) {
183+
continue;
184+
}
185+
176186
writeReturnJsonType(methodReader);
177187
}
178188
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public NimaProcessor() {
2020
}
2121
}
2222

23-
public NimaProcessor(boolean b) {
24-
jsonB = b;
23+
public NimaProcessor(boolean useJsonb) {
24+
jsonB = useJsonb;
2525
}
2626

2727
@Override

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.avaje.http.api.Post;
1010
import io.avaje.http.api.Produces;
1111
import io.avaje.http.api.Put;
12+
import io.helidon.nima.webserver.http.ServerResponse;
1213

1314
@Controller
1415
public class HelloController {
@@ -20,6 +21,11 @@ byte[] testBytes() {
2021
return "not really an image but ok".getBytes();
2122
}
2223

24+
@Get("/void")
25+
void testVoid(Person p, ServerResponse res) {
26+
res.send("success");
27+
}
28+
2329
@Get("hello")
2430
String helloWorld() {
2531
return "Hello world";

0 commit comments

Comments
 (0)