diff --git a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java index a000494c4..2daecf8d8 100644 --- a/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java +++ b/light-rest-4j/src/main/java/com/networknt/codegen/rest/OpenApiGenerator.java @@ -168,7 +168,9 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep String key = entry.getKey(); Map value = entry.getValue().asMap(); String type = null; + String enums = null; boolean isEnum = false; + boolean isEnumClass = false; Map properties = null; List required = null; @@ -177,6 +179,11 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep type = entrySchema.getValue().toString(); if("enum".equals(type)) isEnum = true; } + if("enum".equals(entrySchema.getKey())) { + isEnumClass = true; + enums = entrySchema.getValue().asList().toString(); + enums = enums.substring(enums.indexOf("[") + 1, enums.indexOf("]")); + } if("properties".equals(entrySchema.getKey())) { properties = entrySchema.getValue().asMap(); // transform properties @@ -304,6 +311,10 @@ public void generate(String targetPath, Object model, Any config) throws IOExcep if(!overwriteModel && checkExist(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java")) { continue; } + if (isEnumClass) { + transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.enumClass.template(modelPackage, modelFileName, enums)); + continue; + } transfer(targetPath, ("src.main.java." + modelPackage).replace(".", separator), modelFileName + ".java", templates.rest.pojo.template(modelPackage, modelFileName, classVarName, props)); } } diff --git a/light-rest-4j/src/main/resources/templates/rest/enumClass.rocker.raw b/light-rest-4j/src/main/resources/templates/rest/enumClass.rocker.raw index 59e831aa7..e735b9796 100644 --- a/light-rest-4j/src/main/resources/templates/rest/enumClass.rocker.raw +++ b/light-rest-4j/src/main/resources/templates/rest/enumClass.rocker.raw @@ -1,34 +1,8 @@ -@import com.jsoniter.any.Any -@import java.util.Map -@import java.util.List -@args (Map prop) - @with (v = prop.get("nameWithEnum") + ".values()", value = prop.get("value").asList()) { - public enum @prop.get("nameWithEnum") { - @for((i, item) : value) { - @with(u = item.toString().toUpperCase().replaceAll("-", "_")) {@if (i.index() < value.size() - 1) {@u ("@item"),}@if(i.index() == value.size() - 1) {@u ("@item");}} - } +@args (String modelPackage, String className, String enums) +package @modelPackage; +import java.util.Enumeration; - private final @prop.get("type") value; +public enum @className { + @enums; - @prop.get("nameWithEnum")(@prop.get("type") value) { - this.value = value; - } - - @@Override - public String toString() { - return String.valueOf(value); - } - - public static @prop.get("nameWithEnum") fromValue(String text) { - for (@prop.get("nameWithEnum") b : @v) { - if (String.valueOf(b.value).equals(text)) { - return b; - } - } - return null; - } - } - - private @prop.get("nameWithEnum") @prop.get("name"); - - } \ No newline at end of file +} diff --git a/light-rest-4j/src/main/resources/templates/rest/enumInline.rocker.raw b/light-rest-4j/src/main/resources/templates/rest/enumInline.rocker.raw new file mode 100644 index 000000000..59e831aa7 --- /dev/null +++ b/light-rest-4j/src/main/resources/templates/rest/enumInline.rocker.raw @@ -0,0 +1,34 @@ +@import com.jsoniter.any.Any +@import java.util.Map +@import java.util.List +@args (Map prop) + @with (v = prop.get("nameWithEnum") + ".values()", value = prop.get("value").asList()) { + public enum @prop.get("nameWithEnum") { + @for((i, item) : value) { + @with(u = item.toString().toUpperCase().replaceAll("-", "_")) {@if (i.index() < value.size() - 1) {@u ("@item"),}@if(i.index() == value.size() - 1) {@u ("@item");}} + } + + private final @prop.get("type") value; + + @prop.get("nameWithEnum")(@prop.get("type") value) { + this.value = value; + } + + @@Override + public String toString() { + return String.valueOf(value); + } + + public static @prop.get("nameWithEnum") fromValue(String text) { + for (@prop.get("nameWithEnum") b : @v) { + if (String.valueOf(b.value).equals(text)) { + return b; + } + } + return null; + } + } + + private @prop.get("nameWithEnum") @prop.get("name"); + + } \ No newline at end of file diff --git a/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw b/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw index 98cf836b1..7bf185bb0 100644 --- a/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw +++ b/light-rest-4j/src/main/resources/templates/rest/pojo.rocker.raw @@ -10,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; public class @className { @for (prop: props) { - @if(prop.get("isEnum").toBoolean()) {@templates.rest.enumClass.template(prop)} else {private @prop.get("type") @prop.get("name");} + @if(prop.get("isEnum").toBoolean()) {@templates.rest.enumInline.template(prop)} else {private @prop.get("type") @prop.get("name");} } public @className () {