Skip to content
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

#453 - Compile error with nested enum type not imported in generated … #455

Merged
merged 1 commit into from
Jun 20, 2024
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 @@ -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, ";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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(".", "$");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example.myapp.web.other;

public class Foo {
public enum NestedEnum {
A, B, C
}
}
35 changes: 35 additions & 0 deletions tests/test-javalin-jsonb/src/main/resources/public/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : [
Expand Down