Skip to content

Commit

Permalink
[crd-gen] More principled filtering of enum constants for CRD generat…
Browse files Browse the repository at this point in the history
…ion (fabric8io#5323)

* [crd-gen] More principled filtering of enum constants for CRD generation

* review

* review again

* removing the $ ignored convention

---------

Co-authored-by: Steven Hawkins <[email protected]>
  • Loading branch information
andreaTP and shawkins authored Jul 17, 2023
1 parent 72e427a commit 7c652b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -691,10 +692,9 @@ private T internalFromImpl(String name, TypeRef typeRef, Set<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 7c652b0

Please sign in to comment.