Skip to content

Commit b885da3

Browse files
authored
#453 - Compile error with nested enum type not imported in generated server code (#455)
e.g. ``` public class Foo { public enum NestedEnum { A, B, C } } ``` ``` @controller class TestController { @post void foo(Foo.NestedEnum value) { System.out.println(value); } ```
1 parent 38370c2 commit b885da3

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ static class EnumHandler extends ObjectHandler {
316316

317317
@Override
318318
public String toMethod() {
319-
return "(" + type.shortType() + ") toEnum(" + type.shortType() + ".class, ";
319+
return "(" + type.shortTypeNested() + ") toEnum(" + type.shortTypeNested() + ".class, ";
320320
}
321321

322322
@Override
323323
public String asMethod() {
324-
return "(" + type.shortType() + ") asEnum(" + type.shortType() + ".class, ";
324+
return "(" + type.shortTypeNested() + ") asEnum(" + type.shortTypeNested() + ".class, ";
325325
}
326326
}
327327

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ static UType parse(String type) {
3131
*/
3232
String shortType();
3333

34+
/**
35+
* Return the short type taking nested type into account.
36+
*/
37+
default String shortTypeNested() {
38+
return shortType();
39+
}
40+
3441
/**
3542
* Return the short name.
3643
*/
@@ -152,6 +159,11 @@ public String shortType() {
152159
return Util.shortName(rawType);
153160
}
154161

162+
@Override
163+
public String shortTypeNested() {
164+
return Util.shortName(rawType, true);
165+
}
166+
155167
@Override
156168
public String shortName() {
157169
return Util.initLower(shortType()).replace(".", "$");

http-generator-core/src/test/java/io/avaje/http/generator/core/UTypeTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import static org.assertj.core.api.Assertions.assertThat;
56
import static org.junit.jupiter.api.Assertions.*;
67

78
class UTypeTest {
@@ -17,4 +18,12 @@ void isJavaLangPackage_expect_false() {
1718
assertFalse(UType.isJavaLangPackage("java.lang.other.Foo"));
1819
assertFalse(UType.isJavaLangPackage("not.lang.Foo"));
1920
}
21+
22+
@Test
23+
void parseNestedEnum() {
24+
UType uType = UType.parse("my.pack.Foo.NestedEnum");
25+
assertThat(uType.mainType()).isEqualTo("my.pack.Foo.NestedEnum");
26+
assertThat(uType.shortType()).isEqualTo("Foo.NestedEnum");
27+
assertThat(uType.shortTypeNested()).isEqualTo("NestedEnum");
28+
}
2029
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.javalin.http.Context;
2626
import io.swagger.v3.oas.annotations.Hidden;
2727
import jakarta.inject.Inject;
28+
import org.example.myapp.web.other.Foo;
2829

2930
/**
3031
* Hello resource manager.
@@ -183,4 +184,10 @@ String controlStatusCode(Context ctx) {
183184
ctx.status(201);
184185
return "controlStatusCode";
185186
}
187+
188+
@Produces(value = "text/plain")
189+
@Get("takesNestedEnum")
190+
String takesNestedEnum(Foo.NestedEnum myEnum) {
191+
return "takesNestedEnum-" + myEnum;
192+
}
186193
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.example.myapp.web.other;
2+
3+
public class Foo {
4+
public enum NestedEnum {
5+
A, B, C
6+
}
7+
}

tests/test-javalin-jsonb/src/main/resources/public/openapi.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,41 @@
646646
}
647647
}
648648
},
649+
"/hello/takesNestedEnum" : {
650+
"get" : {
651+
"tags" : [
652+
653+
],
654+
"summary" : "",
655+
"description" : "",
656+
"parameters" : [
657+
{
658+
"name" : "myEnum",
659+
"in" : "query",
660+
"schema" : {
661+
"type" : "string",
662+
"enum" : [
663+
"A",
664+
"B",
665+
"C"
666+
]
667+
}
668+
}
669+
],
670+
"responses" : {
671+
"200" : {
672+
"description" : "",
673+
"content" : {
674+
"text/plain" : {
675+
"schema" : {
676+
"type" : "string"
677+
}
678+
}
679+
}
680+
}
681+
}
682+
}
683+
},
649684
"/hello/withMatrix/{year_segment}/{other}" : {
650685
"get" : {
651686
"tags" : [

0 commit comments

Comments
 (0)