Skip to content

Commit 9bf2c50

Browse files
committed
support form parameters
1 parent 022a54f commit 9bf2c50

File tree

5 files changed

+89
-84
lines changed

5 files changed

+89
-84
lines changed

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.avaje.http.generator.core.Append;
77
import io.avaje.http.generator.core.MethodParam;
88
import io.avaje.http.generator.core.MethodReader;
9+
import io.avaje.http.generator.core.ParamType;
910
import io.avaje.http.generator.core.PathSegments;
1011
import io.avaje.http.generator.core.ProcessingContext;
1112
import io.avaje.http.generator.core.WebMethod;
@@ -30,30 +31,17 @@ class ControllerMethodWriter {
3031

3132
void writeRule() {
3233
final var fullPath = method.getFullPath();
33-
// final String bodyType = method.getBodyType();
34-
// if (bodyType != null) {
35-
// writer.append(" rules.%s(\"%s\", Handler.create(%s.class, this::_%s));",
36-
// webMethod.name().toLowerCase(), fullPath, bodyType, method.simpleName()).eol();
37-
// } else if (method.isFormBody()) {
38-
// writer.append(" rules.%s(\"%s\", Handler.create(%s.class, this::_%s));",
39-
// webMethod.name().toLowerCase(), fullPath, "FormParams", method.simpleName()).eol();
40-
// } else {
4134
writer
4235
.append(
4336
" rules.%s(\"%s\", this::_%s);",
4437
webMethod.name().toLowerCase(), fullPath, method.simpleName())
4538
.eol();
46-
// }
4739
}
4840

4941
void writeHandler(boolean requestScoped) {
5042
writer.append(" private void _%s(ServerRequest req, ServerResponse res", method.simpleName());
5143

5244
writer.append(") {").eol();
53-
// if (!method.isVoid()) {
54-
// writeContextReturn();
55-
// }
56-
5745
final var bodyType = method.getBodyType();
5846
if (bodyType != null) {
5947
if (useJsonB) {
@@ -82,9 +70,10 @@ void writeHandler(boolean requestScoped) {
8270
writer.append(");").eol();
8371
});
8472
}
85-
} // else if (method.isFormBody()) {
86-
// writer.append(", %s %s", "FormParams", "formParams");
87-
// }
73+
} else if (method.getParams().stream()
74+
.anyMatch(p -> p.isForm() || ParamType.FORMPARAM.equals(p.getParamType()))) {
75+
writer.append(" var formParams = req.content().as(Parameters.class);").eol();
76+
}
8877

8978
final var segments = method.getPathSegments();
9079
if (!segments.isEmpty()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ControllerWriter extends BaseControllerWriter {
2727
reader.addImportType("io.avaje.jsonb.Jsonb");
2828
reader.addImportType("io.avaje.jsonb.JsonType");
2929
}
30-
// reader.addImportType("io.helidon.common.http.FormParams");
30+
reader.addImportType("io.helidon.common.parameters.Parameters");
3131
reader.addImportType("io.helidon.nima.webserver.http.HttpRules");
3232
reader.addImportType("io.helidon.nima.webserver.http.HttpRouting");
3333
reader.addImportType("io.helidon.nima.webserver.http.ServerRequest");

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

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

3+
import java.util.List;
4+
35
import io.avaje.http.generator.core.Append;
46
import io.avaje.http.generator.core.ControllerReader;
57
import io.avaje.http.generator.core.ParamType;
68
import io.avaje.http.generator.core.PlatformAdapter;
79

8-
import java.util.List;
9-
1010
class NimaPlatformAdapter implements PlatformAdapter {
1111

1212
static final String NIMA_REQ = "io.helidon.nima.webserver.http.ServerRequest";
1313
static final String NIMA_RES = "io.helidon.nima.webserver.http.ServerResponse";
14-
static final String HELIDON_FORMPARAMS = "io.helidon.common.http.FormParams";
14+
static final String HELIDON_FORMPARAMS = "io.helidon.common.parameters.Parameters";
1515

1616
@Override
1717
public boolean isContextType(String rawType) {
@@ -79,9 +79,7 @@ public void writeReadParameter(Append writer, ParamType paramType, String paramN
7979
case COOKIE:
8080
writer.append("req.headers().cookies().first(\"%s\").orElse(null)", paramName);
8181
break;
82-
case BODY:
83-
case BEANPARAM:
84-
case FORM:
82+
case BODY, BEANPARAM, FORM:
8583
default:
8684
writer.append("null // TODO req.%s().param(\"%s\")", paramType.getType(), paramName);
8785
}
Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,84 @@
11
package org.example;
22

3-
import java.util.List;
4-
import java.util.Map;
5-
import java.util.Set;
6-
73
import io.avaje.http.api.Controller;
8-
import io.avaje.http.api.Get;
4+
import io.avaje.http.api.Form;
95
import io.avaje.http.api.Post;
10-
import io.avaje.http.api.Produces;
11-
import io.avaje.http.api.Put;
12-
import io.helidon.nima.webserver.http.ServerResponse;
136

147
@Controller
158
public class HelloController {
169

17-
@Produces("image/png")
18-
@Get("/get")
19-
byte[] testBytes() {
20-
21-
return "not really an image but ok".getBytes();
10+
// @Produces("image/png")
11+
// @Get("/get")
12+
// byte[] testBytes() {
13+
//
14+
// return "not really an image but ok".getBytes();
15+
// }
16+
//
17+
// @Get("/void")
18+
// void testVoid(Person p, ServerResponse res) {
19+
// res.send("success");
20+
// }
21+
//
22+
// @Get("/helidon")
23+
// void testHelidon(ServerRequest req, ServerResponse res) {
24+
//
25+
// res.send("success");
26+
// }
27+
//
28+
// @Get("hello")
29+
// String helloWorld() {
30+
// return "Hello world";
31+
// }
32+
//
33+
// @Get("person/{name}/{sortBy}")
34+
// Person person(String name, String sortBy) {
35+
// final var p = new Person();
36+
// p.setId(42);
37+
// p.setName(name + " hello" + " sortBy:" + sortBy);
38+
// return p;
39+
// }
40+
//
41+
// @Get("person/{sortBy}/list")
42+
// List<Person> personList(String sortBy) {
43+
// final var p = new Person();
44+
// p.setId(42);
45+
// return List.of(p, p);
46+
// }
47+
//
48+
// @Get("person/{sortBy}/set")
49+
// Set<Person> personSet(String sortBy) {
50+
// final var p = new Person();
51+
// p.setId(42);
52+
// return Set.of(p, p);
53+
// }
54+
//
55+
// @Get("person/{sortBy}/map")
56+
// Map<String, Person> personMap(String sortBy) {
57+
// final var p = new Person();
58+
// p.setId(42);
59+
// return Map.of(sortBy, p);
60+
// }
61+
//
62+
// @Post("person/update")
63+
// String add(Person newGuy) {
64+
//
65+
// return "New Guy Added";
66+
// }
67+
//
68+
// @Put("person/update")
69+
// String addMultiple(List<Person> newGuys) {
70+
// return "New Guys Added";
71+
// }
72+
@Form
73+
@Post("form")
74+
String form(String name, String email, String url) {
75+
return name;
2276
}
2377

24-
@Get("/void")
25-
void testVoid(Person p, ServerResponse res) {
26-
res.send("success");
78+
@Form
79+
@Post("formBean")
80+
String formBean(MyForm form) {
81+
return form.email;
2782
}
2883

29-
@Get("hello")
30-
String helloWorld() {
31-
return "Hello world";
32-
}
33-
34-
@Get("person/{name}/{sortBy}")
35-
Person person(String name, String sortBy) {
36-
final var p = new Person();
37-
p.setId(42);
38-
p.setName(name + " hello" + " sortBy:" + sortBy);
39-
return p;
40-
}
41-
42-
@Get("person/{sortBy}/list")
43-
List<Person> personList(String sortBy) {
44-
final var p = new Person();
45-
p.setId(42);
46-
return List.of(p, p);
47-
}
48-
49-
@Get("person/{sortBy}/set")
50-
Set<Person> personSet(String sortBy) {
51-
final var p = new Person();
52-
p.setId(42);
53-
return Set.of(p, p);
54-
}
55-
56-
@Get("person/{sortBy}/map")
57-
Map<String, Person> personMap(String sortBy) {
58-
final var p = new Person();
59-
p.setId(42);
60-
return Map.of(sortBy, p);
61-
}
62-
63-
@Post("person/update")
64-
String add(Person newGuy) {
65-
66-
return "New Guy Added";
67-
}
68-
69-
@Put("person/update")
70-
String addMultiple(List<Person> newGuys) {
71-
72-
return "New Guys Added";
73-
}
7484
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.example;
2+
3+
public class MyForm {
4+
5+
public String name;
6+
public String email;
7+
public String url;
8+
}

0 commit comments

Comments
 (0)