From 939834e1d66e87ee0dc7975e2ef2250de866a3c0 Mon Sep 17 00:00:00 2001 From: Andrea Leo Date: Fri, 24 Jun 2022 17:45:30 +0200 Subject: [PATCH] fix: crd-gen includes only enum values and discard enum fields --- CHANGELOG.md | 1 + .../crd/generator/AbstractJsonSchema.java | 23 +++++++++++++++++-- .../crd/example/annotated/AnnotatedSpec.java | 16 ++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee53e74a72b..2d4a8b4a50e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ #### Bugs * Fix #5218: No export for `io.fabric8.tekton.triggers.internal.knative.pkg.apis.duck.v1beta1` in tekton v1beta1 triggers model * Fix #5224: Ensuring jetty sets the User-Agent header +* Fix #4225: Enum fields written in generated crd yaml #### Improvements diff --git a/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java b/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java index 6a33da24cd9..3ec8f84118c 100644 --- a/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java +++ b/crd-generator/api/src/main/java/io/fabric8/crd/generator/AbstractJsonSchema.java @@ -23,12 +23,29 @@ import io.fabric8.kubernetes.api.model.IntOrString; import io.fabric8.kubernetes.api.model.Quantity; import io.sundr.builder.internal.functions.TypeAs; -import io.sundr.model.*; +import io.sundr.model.AnnotationRef; +import io.sundr.model.ClassRef; +import io.sundr.model.Method; +import io.sundr.model.PrimitiveRefBuilder; +import io.sundr.model.Property; +import io.sundr.model.TypeDef; +import io.sundr.model.TypeRef; import io.sundr.utils.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import static io.sundr.model.utils.Types.BOOLEAN_REF; import static io.sundr.model.utils.Types.DOUBLE_REF; @@ -664,6 +681,8 @@ private T internalFromImpl(String name, TypeRef typeRef, Set visited, In // check if we're dealing with an enum if (def.isEnum()) { final JsonNode[] enumValues = def.getProperties().stream() + .filter(property -> property.isStatic() && property.isPublic() + && def.getFullyQualifiedName().equals(property.getTypeRef().toString())) .map(this::extractUpdatedNameFromJacksonPropertyIfPresent) .filter(n -> !n.startsWith("$")) .map(JsonNodeFactory.instance::textNode) diff --git a/crd-generator/api/src/test/java/io/fabric8/crd/example/annotated/AnnotatedSpec.java b/crd-generator/api/src/test/java/io/fabric8/crd/example/annotated/AnnotatedSpec.java index 85ddfcfc428..efbf8a7ffcb 100644 --- a/crd-generator/api/src/test/java/io/fabric8/crd/example/annotated/AnnotatedSpec.java +++ b/crd-generator/api/src/test/java/io/fabric8/crd/example/annotated/AnnotatedSpec.java @@ -95,10 +95,20 @@ public void setEmptySetter2(boolean emptySetter2) { } public enum AnnotatedEnum { - non, + non("N"), @JsonProperty("oui") - Yes, + es("O"), @JsonIgnore - Maybe + Maybe("Maybe"); + + private final String abbreviation; + + AnnotatedEnum(String abbreviation) { + this.abbreviation = abbreviation; + } + + public String getAbbreviation() { + return abbreviation; + } } }