Skip to content

Commit fae2f90

Browse files
committed
#4 - When @Form is on the method then non-path parameters default to form parameters
1 parent 855d353 commit fae2f90

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/main/java/io/dinject/javalin/generator/MethodParam.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class MethodParam {
77

88
private final ElementReader elementParam;
99

10-
MethodParam(VariableElement param, ProcessingContext ctx) {
11-
this.elementParam = new ElementReader(param, ctx, ParamType.QUERYPARAM);
10+
MethodParam(VariableElement param, ProcessingContext ctx, ParamType defaultParamType) {
11+
this.elementParam = new ElementReader(param, ctx, defaultParamType);
1212
}
1313

1414
void buildCtxGet(Append writer, Set<String> pathParams) {

src/main/java/io/dinject/javalin/generator/MethodReader.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.dinject.javalin.generator;
22

33
import io.dinject.controller.Delete;
4+
import io.dinject.controller.Form;
45
import io.dinject.controller.Get;
56
import io.dinject.controller.Patch;
67
import io.dinject.controller.Post;
@@ -27,6 +28,8 @@ class MethodReader {
2728
private WebMethod webMethod;
2829
private String webMethodPath;
2930

31+
private boolean formMarker;
32+
3033
/**
3134
* Holds enum Roles that are required for the method.
3235
*/
@@ -39,6 +42,8 @@ class MethodReader {
3942
this.element = element;
4043
this.isVoid = element.getReturnType().toString().equals("void");
4144
this.methodRoles = Util.findRoles(element);
45+
46+
readMethodAnnotation();
4247
}
4348

4449
void read() {
@@ -49,17 +54,19 @@ void read() {
4954
}
5055
}
5156

57+
// non-path parameters default to form or query parameters based on the
58+
// existence of @Form annotation on the method
59+
ParamType defaultParamType = (formMarker) ? ParamType.FORMPARAM : ParamType.QUERYPARAM;
60+
5261
for (VariableElement p : element.getParameters()) {
53-
MethodParam param = new MethodParam(p, ctx);
62+
MethodParam param = new MethodParam(p, ctx, defaultParamType);
5463
params.add(param);
5564
param.addImports(bean);
5665
}
5766
}
5867

5968
void addRoute(Append writer) {
6069

61-
readMethodAnnotation();
62-
6370
if (webMethod != null) {
6471

6572
String fullPath = Util.combinePath(beanPath, webMethodPath);
@@ -122,6 +129,11 @@ private boolean isReturnJson() {
122129

123130
private boolean readMethodAnnotation() {
124131

132+
Form form = element.getAnnotation(Form.class);
133+
if (form != null) {
134+
this.formMarker = true;
135+
}
136+
125137
Get get = element.getAnnotation(Get.class);
126138
if (get != null) {
127139
return setWebMethod(WebMethod.GET, get.value());

0 commit comments

Comments
 (0)