diff --git a/kubernetes-model-generator/openapi/generator/pkg/openapi/openapi-gen-processors.go b/kubernetes-model-generator/openapi/generator/pkg/openapi/openapi-gen-processors.go index 7da40cc8d0a..a03592b5ea7 100644 --- a/kubernetes-model-generator/openapi/generator/pkg/openapi/openapi-gen-processors.go +++ b/kubernetes-model-generator/openapi/generator/pkg/openapi/openapi-gen-processors.go @@ -230,8 +230,9 @@ func (v *IstioEnumExtractor) Visit(node ast.Node) ast.Visitor { ast.Inspect(valueSpec, func(valueNode ast.Node) bool { switch valueNode.(type) { case *ast.KeyValueExpr: - unquoted, _ := strconv.Unquote(valueNode.(*ast.KeyValueExpr).Key.(*ast.BasicLit).Value) - v.values = append(v.values, unquoted) + enumKey, _ := strconv.Unquote(valueNode.(*ast.KeyValueExpr).Key.(*ast.BasicLit).Value) + enumValue := valueNode.(*ast.KeyValueExpr).Value.(*ast.BasicLit).Value + v.values = append(v.values, enumKey+"("+enumValue+")") } return true }) diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/model/ClassInformation.java b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/model/ClassInformation.java index bf090e93db0..a5a87042207 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/model/ClassInformation.java +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/java/io/fabric8/kubernetes/schema/generator/model/ClassInformation.java @@ -55,7 +55,12 @@ public class ClassInformation implements ImportManager { kubernetesListType = apiVersion == null ? null : schemaUtils.kubernetesListType(this, classSchema); inRootPackage = getPackageName().equals(schemaUtils.getSettings().getPackageName()); isEnum = SchemaUtils.isEnum(classSchema); - enumValues = isEnum ? String.join(",\n ", SchemaUtils.enumValues(classSchema)) : null; + if (isEnum) { + addImport("com.fasterxml.jackson.annotation.JsonCreator"); + enumValues = String.join(",\n ", SchemaUtils.enumValues(classSchema)); + } else { + enumValues = null; + } isInterface = SchemaUtils.isInterface(classSchema); isHasMetadata = apiVersion != null && kubernetesListType == null && schemaUtils.isHasMetadata(classSchema); isNamespaced = apiVersion != null && apiVersion.isNamespaced(); diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/resources/templates/model.mustache b/kubernetes-model-generator/openapi/maven-plugin/src/main/resources/templates/model.mustache index 79021f76ae0..18d761773b4 100644 --- a/kubernetes-model-generator/openapi/maven-plugin/src/main/resources/templates/model.mustache +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/resources/templates/model.mustache @@ -26,9 +26,7 @@ import {{.}}; */{{/hasDescription}} {{> model_class_annotations}}public {{classInformation.classType}} {{classInformation.classSimpleName}} {{classInformation.implementsExtends}} { -{{#classInformation.enumValues}} - {{.}}; -{{/classInformation.enumValues}} +{{>model_enum_body}} {{>model_fields}} {{#hasFields}} diff --git a/kubernetes-model-generator/openapi/maven-plugin/src/main/resources/templates/model_enum_body.mustache b/kubernetes-model-generator/openapi/maven-plugin/src/main/resources/templates/model_enum_body.mustache new file mode 100644 index 00000000000..95601bb6dcd --- /dev/null +++ b/kubernetes-model-generator/openapi/maven-plugin/src/main/resources/templates/model_enum_body.mustache @@ -0,0 +1,42 @@ +{{! + Copyright (C) 2015 Red Hat, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +}} +{{#classInformation.isEnum}} +{{#classInformation.enumValues}} + {{.}}; +{{/classInformation.enumValues}} + private final int value; + private {{classInformation.classSimpleName}}(int value) { + this.value = value; + } + + @JsonCreator + public static {{classInformation.classSimpleName}} fromValue(Object value) { + if (value instanceof String) { + for ({{classInformation.classSimpleName}} e : {{classInformation.classSimpleName}}.values()) { + if (e.name().equalsIgnoreCase(value.toString())) { + return e; + } + } + } else if (value instanceof Integer) { + for ({{classInformation.classSimpleName}} e : {{classInformation.classSimpleName}}.values()) { + if (value.equals(e.value)) { + return e; + } + } + } + return null; + } +{{/classInformation.isEnum}}