Skip to content

Support Enum Query/Form Parameters #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

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

import java.util.Optional;

import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;

import io.avaje.http.generator.core.openapi.MethodDocBuilder;
Expand Down Expand Up @@ -42,7 +45,19 @@ public class ElementReader {
this.rawType = rawType;
this.shortType = Util.shortName(rawType);
this.contextType = ctx.platform().isContextType(rawType);
this.typeHandler = TypeMap.get(rawType);

if (type != null
&& (defaultType == ParamType.FORMPARAM || defaultType == ParamType.QUERYPARAM)
&& Optional.ofNullable(ctx.typeElement(type.mainType()))
.map(TypeElement::getKind)
.filter(ElementKind.ENUM::equals)
.isPresent()) {

this.typeHandler = TypeMap.enumParamHandler(type);
} else {

this.typeHandler = TypeMap.get(rawType);
}
this.formMarker = formMarker;
this.varName = element.getSimpleName().toString();
this.snakeName = Util.snakeCase(varName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ static TypeHandler get(String type) {
return types.get(type);
}

static TypeHandler enumParamHandler(UType type) {
return new EnumHandler(type);
}

static class StringHandler extends JavaLangType {
StringHandler() {
super("String");
Expand Down Expand Up @@ -169,7 +173,7 @@ static class BoolHandler extends Primitive {
}
}

static abstract class JavaLangType implements TypeHandler {
abstract static class JavaLangType implements TypeHandler {

final String shortName;

Expand All @@ -193,7 +197,7 @@ public String importType() {
}
}

static abstract class Primitive implements TypeHandler {
abstract static class Primitive implements TypeHandler {

private final String type;

Expand Down Expand Up @@ -277,8 +281,27 @@ static class LocalDateTimeHandler extends ObjectHandler {
}
}

static class EnumHandler extends ObjectHandler {
private final UType type;

EnumHandler(UType type) {

super(type.mainType(), type.shortName());
this.type = type;
}

@Override
public String toMethod() {
return type.shortType() + ".valueOf(";
}

@Override
public String asMethod() {
return "java.util.Objects.toString(";
}
}

static abstract class ObjectHandler implements TypeHandler {
abstract static class ObjectHandler implements TypeHandler {

private final String importType;
private final String shortName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.myapp.web.test;

public enum ServerType {
PROXY,
HIDE_N_SEEK,
FFA
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,4 @@ void neo(
CompletableFuture<HelloDto> getAllAsync() {
return CompletableFuture.supplyAsync(() -> new HelloDto(12, "Jim", "asd"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.example.myapp.web.test;

import org.example.myapp.web.HelloDto;

import io.avaje.http.api.Controller;
import io.avaje.http.api.Default;
import io.avaje.http.api.Form;
import io.avaje.http.api.FormParam;
import io.avaje.http.api.Get;
import io.avaje.http.api.Path;
import io.avaje.http.api.Post;
import io.avaje.http.api.QueryParam;

@Path("test/")
@Controller
public class TestController2 {

@Form
@Get("/enumForm")
String enumForm(String s, ServerType type) {
return type.name();
}

@Get("/enumFormParam")
String enumFormParam(@FormParam String s, @FormParam ServerType type) {
return type.name();
}

@Get("/enumQuery")
String enumQuery(@QueryParam @Default("FFA") ServerType type) {
return type.name();
}

@Post("/enumQueryImplied")
String enumQueryImplied(HelloDto s, ServerType type) {
return type.name();
}
}