diff --git a/CHANGELOG.md b/CHANGELOG.md index 5da2ff40ca5..6b0f90f8ca5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ### 5.8-SNAPSHOT #### Bugs +* Fix #3442: make sure new CRDGenerator instances start with a clean generation context + #### Improvements #### Dependency Upgrade #### New Features diff --git a/crd-generator/api/src/main/java/io/fabric8/crd/generator/CRDGenerator.java b/crd-generator/api/src/main/java/io/fabric8/crd/generator/CRDGenerator.java index d850c704911..77b85f881e7 100644 --- a/crd-generator/api/src/main/java/io/fabric8/crd/generator/CRDGenerator.java +++ b/crd-generator/api/src/main/java/io/fabric8/crd/generator/CRDGenerator.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; +import io.fabric8.crd.generator.utils.Types; import io.fabric8.crd.generator.v1.CustomResourceHandler; import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.client.CustomResource; @@ -55,6 +56,7 @@ public class CRDGenerator { public CRDGenerator() { resources = new Resources(); + Types.resetGenerationContext(); // make sure the new generator starts up with a clean slate } public CRDGenerator inOutputDir(File outputDir) { diff --git a/crd-generator/api/src/main/java/io/fabric8/crd/generator/utils/Types.java b/crd-generator/api/src/main/java/io/fabric8/crd/generator/utils/Types.java index 333041b54d1..d38f630a14d 100644 --- a/crd-generator/api/src/main/java/io/fabric8/crd/generator/utils/Types.java +++ b/crd-generator/api/src/main/java/io/fabric8/crd/generator/utils/Types.java @@ -20,44 +20,35 @@ import io.sundr.adapter.api.AdapterContext; import io.sundr.adapter.api.Adapters; import io.sundr.builder.TypedVisitor; -import io.sundr.model.ClassRef; -import io.sundr.model.ClassRefBuilder; -import io.sundr.model.PrimitiveRef; -import io.sundr.model.Property; -import io.sundr.model.PropertyBuilder; -import io.sundr.model.TypeDef; -import io.sundr.model.TypeDefBuilder; -import io.sundr.model.TypeParamDef; -import io.sundr.model.TypeParamRef; -import io.sundr.model.TypeRef; -import io.sundr.model.VoidRef; -import io.sundr.model.WildcardRef; +import io.sundr.model.*; import io.sundr.model.functions.GetDefinition; import io.sundr.model.repo.DefinitionRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class Types { private Types() { throw new IllegalStateException("Utility class"); } + private static final Logger LOGGER = LoggerFactory.getLogger(Types.class); private static final String NAMESPACED = Namespaced.class.getName(); public static final String JAVA_LANG_VOID = "java.lang.Void"; public static final AdapterContext REFLECTION_CONTEXT = AdapterContext.create(DefinitionRepository.getRepository()); + /** + * Make sure the generation context is reset so that types can be properly introspected if classes have changed since the last generation round. + */ + public static void resetGenerationContext() { + DefinitionRepository.getRepository().clear(); + } + public static TypeDef typeDefFrom(Class clazz) { return unshallow(Adapters.adaptType(clazz, REFLECTION_CONTEXT)); } @@ -166,24 +157,24 @@ private static Set projectSuperClasses(TypeDef definition) { private static List projectProperties(TypeDef typeDef) { final String fqn = typeDef.getFullyQualifiedName(); return Stream.concat( - typeDef.getProperties().stream().filter(p -> { - // enums have self-referential static properties for each enum case so we cannot ignore them - if(typeDef.isEnum()) { - final TypeRef typeRef = p.getTypeRef(); - if (typeRef instanceof ClassRef && fqn.equals(((ClassRef) typeRef).getFullyQualifiedName())) { - // we're dealing with an enum case so keep it - return true; + typeDef.getProperties().stream().filter(p -> { + // enums have self-referential static properties for each enum case so we cannot ignore them + if (typeDef.isEnum()) { + final TypeRef typeRef = p.getTypeRef(); + if (typeRef instanceof ClassRef && fqn.equals(((ClassRef) typeRef).getFullyQualifiedName())) { + // we're dealing with an enum case so keep it + return true; + } } - } - // otherwise exclude all static properties - return !p.isStatic(); - }), - typeDef.getExtendsList().stream() - .filter(e -> !e.getFullyQualifiedName().startsWith("java.")) - .flatMap(e -> projectProperties(projectDefinition(e)) - .stream() - .filter(p -> filterCustomResourceProperties(e).test(p))) - ) + // otherwise exclude all static properties + return !p.isStatic(); + }), + typeDef.getExtendsList().stream() + .filter(e -> !e.getFullyQualifiedName().startsWith("java.")) + .flatMap(e -> projectProperties(projectDefinition(e)) + .stream() + .filter(p -> filterCustomResourceProperties(e).test(p))) + ) .collect(Collectors.toList()); }