From 7c652b0e81da29241a19fee50bd3e072501824b9 Mon Sep 17 00:00:00 2001 From: Andrea Peruffo Date: Mon, 17 Jul 2023 11:13:01 +0100 Subject: [PATCH] [crd-gen] More principled filtering of enum constants for CRD generation (#5323) * [crd-gen] More principled filtering of enum constants for CRD generation * review * review again * removing the $ ignored convention --------- Co-authored-by: Steven Hawkins --- CHANGELOG.md | 1 + .../crd/generator/AbstractJsonSchema.java | 8 ++++---- .../crd/example/annotated/AnnotatedSpec.java | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83dcdf85283..31259879f69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * Fix #5221: Empty kube config file causes NPE * Fix #5281: Ensure the KubernetesCrudDispatcher's backing map is accessed w/lock * Fix #5293: Ensured the mock server uses only generic or JsonNode parsing +* Fix #4225: [crd-generator] Principled generation of enum values instead of considering more properties #### Improvements * Fix #5166: Remove opinionated messages from Config's `errorMessages` and deprecate it 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 db3ea390cc2..e0b54dfacf2 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 @@ -45,6 +45,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -584,7 +585,7 @@ private String extractUpdatedNameFromJacksonPropertyIfPresent(Property property) .anyMatch(a -> a.getClassRef().getFullyQualifiedName().equals(ANNOTATION_JSON_IGNORE)); if (ignored) { - return "$" + property.getName(); + return null; } else { return property.getAnnotations().stream() // only consider JsonProperty annotation @@ -691,10 +692,9 @@ 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())) + .filter(Property::isEnumConstant) .map(this::extractUpdatedNameFromJacksonPropertyIfPresent) - .filter(n -> !n.startsWith("$")) + .filter(Objects::nonNull) .map(JsonNodeFactory.instance::textNode) .toArray(JsonNode[]::new); return enumProperty(enumValues); 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 efbf8a7ffcb..023530c4df1 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 @@ -110,5 +110,21 @@ public enum AnnotatedEnum { public String getAbbreviation() { return abbreviation; } + + public static AnnotatedEnum SIM = es; + + public AnotherEnum one = AnotherEnum.ONE; + + public AnotherEnum getOne() { + return one; + } + + public void setOne(AnotherEnum one) { + this.one = one; + } + } + + public enum AnotherEnum { + ONE } }