From 4c6336e3bc692a498ee601358cad2f39a9dd5f15 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Thu, 20 Jun 2024 22:12:10 +1200 Subject: [PATCH] #453 - Compile error with nested enum type not imported in generated server code e.g. ``` public class Foo { public enum NestedEnum { A, B, C } } ``` ``` @Controller class TestController { @Post void foo(Foo.NestedEnum value) { System.out.println(value); } ``` --- .../io/avaje/http/generator/core/TypeMap.java | 4 +-- .../io/avaje/http/generator/core/UType.java | 12 +++++++ .../avaje/http/generator/core/UTypeTest.java | 9 +++++ .../example/myapp/web/HelloController.java | 7 ++++ .../java/org/example/myapp/web/other/Foo.java | 7 ++++ .../src/main/resources/public/openapi.json | 35 +++++++++++++++++++ 6 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/other/Foo.java diff --git a/http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java b/http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java index 1594766f..037b6950 100644 --- a/http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java +++ b/http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java @@ -316,12 +316,12 @@ static class EnumHandler extends ObjectHandler { @Override public String toMethod() { - return "(" + type.shortType() + ") toEnum(" + type.shortType() + ".class, "; + return "(" + type.shortTypeNested() + ") toEnum(" + type.shortTypeNested() + ".class, "; } @Override public String asMethod() { - return "(" + type.shortType() + ") asEnum(" + type.shortType() + ".class, "; + return "(" + type.shortTypeNested() + ") asEnum(" + type.shortTypeNested() + ".class, "; } } diff --git a/http-generator-core/src/main/java/io/avaje/http/generator/core/UType.java b/http-generator-core/src/main/java/io/avaje/http/generator/core/UType.java index f22658b2..24738fdb 100644 --- a/http-generator-core/src/main/java/io/avaje/http/generator/core/UType.java +++ b/http-generator-core/src/main/java/io/avaje/http/generator/core/UType.java @@ -31,6 +31,13 @@ static UType parse(String type) { */ String shortType(); + /** + * Return the short type taking nested type into account. + */ + default String shortTypeNested() { + return shortType(); + } + /** * Return the short name. */ @@ -152,6 +159,11 @@ public String shortType() { return Util.shortName(rawType); } + @Override + public String shortTypeNested() { + return Util.shortName(rawType, true); + } + @Override public String shortName() { return Util.initLower(shortType()).replace(".", "$"); diff --git a/http-generator-core/src/test/java/io/avaje/http/generator/core/UTypeTest.java b/http-generator-core/src/test/java/io/avaje/http/generator/core/UTypeTest.java index 12dea5d6..51e79c70 100644 --- a/http-generator-core/src/test/java/io/avaje/http/generator/core/UTypeTest.java +++ b/http-generator-core/src/test/java/io/avaje/http/generator/core/UTypeTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; class UTypeTest { @@ -17,4 +18,12 @@ void isJavaLangPackage_expect_false() { assertFalse(UType.isJavaLangPackage("java.lang.other.Foo")); assertFalse(UType.isJavaLangPackage("not.lang.Foo")); } + + @Test + void parseNestedEnum() { + UType uType = UType.parse("my.pack.Foo.NestedEnum"); + assertThat(uType.mainType()).isEqualTo("my.pack.Foo.NestedEnum"); + assertThat(uType.shortType()).isEqualTo("Foo.NestedEnum"); + assertThat(uType.shortTypeNested()).isEqualTo("NestedEnum"); + } } diff --git a/tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/HelloController.java b/tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/HelloController.java index 18f4df26..fb6d8ecf 100644 --- a/tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/HelloController.java +++ b/tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/HelloController.java @@ -25,6 +25,7 @@ import io.javalin.http.Context; import io.swagger.v3.oas.annotations.Hidden; import jakarta.inject.Inject; +import org.example.myapp.web.other.Foo; /** * Hello resource manager. @@ -183,4 +184,10 @@ String controlStatusCode(Context ctx) { ctx.status(201); return "controlStatusCode"; } + + @Produces(value = "text/plain") + @Get("takesNestedEnum") + String takesNestedEnum(Foo.NestedEnum myEnum) { + return "takesNestedEnum-" + myEnum; + } } diff --git a/tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/other/Foo.java b/tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/other/Foo.java new file mode 100644 index 00000000..f3ed0507 --- /dev/null +++ b/tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/other/Foo.java @@ -0,0 +1,7 @@ +package org.example.myapp.web.other; + +public class Foo { + public enum NestedEnum { + A, B, C + } +} diff --git a/tests/test-javalin-jsonb/src/main/resources/public/openapi.json b/tests/test-javalin-jsonb/src/main/resources/public/openapi.json index bb9b03bd..549828b2 100644 --- a/tests/test-javalin-jsonb/src/main/resources/public/openapi.json +++ b/tests/test-javalin-jsonb/src/main/resources/public/openapi.json @@ -646,6 +646,41 @@ } } }, + "/hello/takesNestedEnum" : { + "get" : { + "tags" : [ + + ], + "summary" : "", + "description" : "", + "parameters" : [ + { + "name" : "myEnum", + "in" : "query", + "schema" : { + "type" : "string", + "enum" : [ + "A", + "B", + "C" + ] + } + } + ], + "responses" : { + "200" : { + "description" : "", + "content" : { + "text/plain" : { + "schema" : { + "type" : "string" + } + } + } + } + } + } + }, "/hello/withMatrix/{year_segment}/{other}" : { "get" : { "tags" : [