Skip to content

Commit f3bf0bc

Browse files
authored
Merge pull request #166 from SentryMan/enum-form
Support Enum Query/Form Parameters
2 parents 1f47591 + e071f53 commit f3bf0bc

File tree

5 files changed

+87
-5
lines changed

5 files changed

+87
-5
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import static io.avaje.http.generator.core.ParamType.RESPONSE_HANDLER;
44

5+
import java.util.Optional;
6+
57
import javax.lang.model.element.Element;
8+
import javax.lang.model.element.ElementKind;
69
import javax.lang.model.element.TypeElement;
710

811
import io.avaje.http.generator.core.openapi.MethodDocBuilder;
@@ -42,7 +45,19 @@ public class ElementReader {
4245
this.rawType = rawType;
4346
this.shortType = Util.shortName(rawType);
4447
this.contextType = ctx.platform().isContextType(rawType);
45-
this.typeHandler = TypeMap.get(rawType);
48+
49+
if (type != null
50+
&& (defaultType == ParamType.FORMPARAM || defaultType == ParamType.QUERYPARAM)
51+
&& Optional.ofNullable(ctx.typeElement(type.mainType()))
52+
.map(TypeElement::getKind)
53+
.filter(ElementKind.ENUM::equals)
54+
.isPresent()) {
55+
56+
this.typeHandler = TypeMap.enumParamHandler(type);
57+
} else {
58+
59+
this.typeHandler = TypeMap.get(rawType);
60+
}
4661
this.formMarker = formMarker;
4762
this.varName = element.getSimpleName().toString();
4863
this.snakeName = Util.snakeCase(varName);

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ static TypeHandler get(String type) {
4343
return types.get(type);
4444
}
4545

46+
static TypeHandler enumParamHandler(UType type) {
47+
return new EnumHandler(type);
48+
}
49+
4650
static class StringHandler extends JavaLangType {
4751
StringHandler() {
4852
super("String");
@@ -169,7 +173,7 @@ static class BoolHandler extends Primitive {
169173
}
170174
}
171175

172-
static abstract class JavaLangType implements TypeHandler {
176+
abstract static class JavaLangType implements TypeHandler {
173177

174178
final String shortName;
175179

@@ -193,7 +197,7 @@ public String importType() {
193197
}
194198
}
195199

196-
static abstract class Primitive implements TypeHandler {
200+
abstract static class Primitive implements TypeHandler {
197201

198202
private final String type;
199203

@@ -277,8 +281,27 @@ static class LocalDateTimeHandler extends ObjectHandler {
277281
}
278282
}
279283

284+
static class EnumHandler extends ObjectHandler {
285+
private final UType type;
286+
287+
EnumHandler(UType type) {
288+
289+
super(type.mainType(), type.shortName());
290+
this.type = type;
291+
}
292+
293+
@Override
294+
public String toMethod() {
295+
return type.shortType() + ".valueOf(";
296+
}
297+
298+
@Override
299+
public String asMethod() {
300+
return "java.util.Objects.toString(";
301+
}
302+
}
280303

281-
static abstract class ObjectHandler implements TypeHandler {
304+
abstract static class ObjectHandler implements TypeHandler {
282305

283306
private final String importType;
284307
private final String shortName;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.example.myapp.web.test;
2+
3+
public enum ServerType {
4+
PROXY,
5+
HIDE_N_SEEK,
6+
FFA
7+
}

tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/test/TestController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,4 @@ void neo(
129129
CompletableFuture<HelloDto> getAllAsync() {
130130
return CompletableFuture.supplyAsync(() -> new HelloDto(12, "Jim", "asd"));
131131
}
132-
133132
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.example.myapp.web.test;
2+
3+
import org.example.myapp.web.HelloDto;
4+
5+
import io.avaje.http.api.Controller;
6+
import io.avaje.http.api.Default;
7+
import io.avaje.http.api.Form;
8+
import io.avaje.http.api.FormParam;
9+
import io.avaje.http.api.Get;
10+
import io.avaje.http.api.Path;
11+
import io.avaje.http.api.Post;
12+
import io.avaje.http.api.QueryParam;
13+
14+
@Path("test/")
15+
@Controller
16+
public class TestController2 {
17+
18+
@Form
19+
@Get("/enumForm")
20+
String enumForm(String s, ServerType type) {
21+
return type.name();
22+
}
23+
24+
@Get("/enumFormParam")
25+
String enumFormParam(@FormParam String s, @FormParam ServerType type) {
26+
return type.name();
27+
}
28+
29+
@Get("/enumQuery")
30+
String enumQuery(@QueryParam @Default("FFA") ServerType type) {
31+
return type.name();
32+
}
33+
34+
@Post("/enumQueryImplied")
35+
String enumQueryImplied(HelloDto s, ServerType type) {
36+
return type.name();
37+
}
38+
}

0 commit comments

Comments
 (0)